【问题标题】:I have problem in terms of changing integer into character我在将整数更改为字符方面遇到问题
【发布时间】:2021-12-24 01:10:34
【问题描述】:

This the Instruction

我添加了一些说明的照片。

但是我的教授想把整数变成字符。我将如何做? 这是我的代码。我在编程中发挥了我的全部潜力,但这个程序让我失望。我使用了我可能找到的所有资源,但我没有得到正确的代码

#include <stdlib.h>

#include <iostream>

using namespace std;

int array[10];

void DisplayArray() {
  for (int i = 0; i < 10; i++)
    cout << "Array [ " << i << " ] = " << array[i] << endl;
}

void SetDefaultValues() {
  cout << "Defalut Values :" << endl;
  for (int i = 0; i < 10; i++) {
    array[i] = -1;
    cout << "array [" << i << "]"
         << "= " << array[i] << endl;
  }
}

void InsertValues() {
  cout << "Enter 10 Values " << endl;
  for (int i = 0; i < 10; i++) {
    cin >> array[i];
  }
  cout << "\n\t\t\tArray Values Inserted...  Successfully " << endl;
}

void DeleteValues() {
  cout << "Enter the Index Number To Delete Value :";
  int index;
  cin >> index;
  if (index > 9 || index < 0) {
    cout << "Invalid Index Entered-> Valid Range(0-9)" << endl;
    DeleteValues();  // Recall The Function it self
  } else {
    array[index] = -1;
  }
  cout << "\n\t\t\tArray Value Deleted...  Successfully " << endl;
}

void UpdateValues() {
  cout << "Enter Index Number to Update Value :";
  int index;
  cin >> index;
  if (index > 9 || index < 0) {
    cout << "Invalid Index Entered-> Valid Range(0-9)" << endl;
    UpdateValues();  // Recall The Function it self
  } else {
    cout << "Enter the New Value For Index array[ " << index << " ] = ";
    cin >> array[index];
    cout << "\n\t\t\tArray Updated...  Successfully " << endl;
  }
}

int main() {
  char option;
  SetDefaultValues();

  do {
    cout << "\t\t\tEnter 1 to Enter  Values\n\t\t\tEnter 2 to Update "
            "Values\n\t\t\tEnter 3 to Delete Values\n\n\t\t\t or Enter E to "
            "EXIT\n\n\t\t\t  Enter Option: ->  ";
    cin >> option;
    if (option == '1') {
      cout << "Insert Function Called" << endl;
      InsertValues();
      cout << "Inserted Values :" << endl;
      DisplayArray();
    } else if (option == '2') {
      UpdateValues();
      cout << "Updated Array :" << endl;
      DisplayArray();
    } else if (option == '3') {
      DeleteValues();
      cout << "Array After Deleting Values :" << endl;
      DisplayArray();
    } else if (option != 'e' && option != 'E') {
      cout << "\n\n\t\t\tSelect A Valid Option From Below\n\n";
    }
  } while (option != 'e' && option != 'E');

  system("cls");  // To Clear The Screen
  cout << "\n\n\n\n\n\n\n\n\n\n\t\tProgram Ended Press Any Key To Exit "
          "Screen.....\n\n\n\n\n\n\n\n\n\n\n\n"
       << endl;
  return 0;
}

这是我当前程序的输出

Defalut Values :
array [0]= -1
array [1]= -1
array [2]= -1
array [3]= -1
array [4]= -1
array [5]= -1
array [6]= -1
array [7]= -1
array [8]= -1
array [9]= -1
                        Enter 1 to Enter  Values
                        Enter 2 to Update Values
                        Enter 3 to Delete Values

                          or Enter E to EXIT

                          Enter Option: ->  

【问题讨论】:

  • 要求中的“或链表”部分让我想知道这项作业的教学水平。
  • 可以添加缩进更好的代码吗? (请):)
  • &lt;stdlib.h&gt; 应该是 &lt;cstdlib&gt;。全局数组不好。 using namespace std; 是不好的做法。功能的设计还有很多不足之处。提示应该在对数组执行操作的函数之外,而应该将值传递给大多数这些函数。你也误解了你的要求。不需要或不需要转换。这可能是由于对数组是什么以及如何使用它们的理解不足。
  • 另外,图片清楚地显示菜单选择是由字母和而不是数字组成的。

标签: c++ arrays linked-list


【解决方案1】:

第一个真正必要的更改是将int array[10]; 移动到char array[10]; 这样我们就可以存储字符而不是存储整数。

由于您的程序使用std::coutstd::cin(并且它们都有不同的重载,得到一个char 或一个整数),除了默认值(-1)之外,您实际上不需要更改任何其他值(-1)无效字符。 您基本上可以使用一个点 ('.') 作为默认值或其他任何对您来说看起来不错的东西。

关于search option,您基本上可以循环遍历数组并打印匹配的位置。

void SearchCharacter(char a){
    bool printed_out = false;
    for(unsigned int i=0; i < sizeof(array)/sizeof(array[0]); ++i){
        if(array[i] == a){
            if(!printed_out){
                std::cout << "The character " << a << " is found in position: ";
                printed_out = true;
            }
            
            std::cout << i << " ";
        }
    }
    
    if(printed_out)
        std::cout << std::endl;
    else
        std::cout << "No matches found for character: " << a << std::endl;
}

【讨论】:

  • 我在声明时遇到问题
  • 你遇到了什么问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-06
  • 2019-12-05
  • 2014-11-01
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
  • 2015-03-08
相关资源
最近更新 更多