【发布时间】:2021-12-24 01:10:34
【问题描述】:
我添加了一些说明的照片。
但是我的教授想把整数变成字符。我将如何做? 这是我的代码。我在编程中发挥了我的全部潜力,但这个程序让我失望。我使用了我可能找到的所有资源,但我没有得到正确的代码
#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: ->
【问题讨论】:
-
要求中的“或链表”部分让我想知道这项作业的教学水平。
-
可以添加缩进更好的代码吗? (请):)
-
<stdlib.h>应该是<cstdlib>。全局数组不好。using namespace std;是不好的做法。功能的设计还有很多不足之处。提示应该在对数组执行操作的函数之外,而应该将值传递给大多数这些函数。你也误解了你的要求。不需要或不需要转换。这可能是由于对数组是什么以及如何使用它们的理解不足。 -
另外,图片清楚地显示菜单选择是由字母和而不是数字组成的。
标签: c++ arrays linked-list