我在将整数更改为字符方面遇到问题

I have problem in terms of changing integer into character

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: ->  

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

因为你的程序正在使用 std::coutstd::cin (并且它们都有不同的重载获取一个字符或一个整数)你真的不需要改变除了默认值之外的任何东西(-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;
}