【发布时间】:2015-07-17 13:20:10
【问题描述】:
我有一个菜单,用户必须在其中输入一个数字,以下情况将对数组进行操作。但是菜单必须按顺序完成。例如,用户必须首先设置数组的大小,然后将元素传递到单独模块中的数组中。
- 1- 输入数组的大小 2- 输入数组元素 3- 排序
数组 4- 在数组中查找一个数字 5- 打印数组
6-反向打印数组 7-退出
请输入您的选择:3 您的数组尚未初始化!
到目前为止,这是我的代码。我在初始化数组和输入元素时遇到问题。一旦我完成了这些,我相信我可以做剩下的:
int sizeArray();
int *enterElements(int size, int *anArray);
int main(){
int choice;
int tempSize;
int mArray[100]; //initialize the Array
do{
printf("1 - Enter the size of the array\n");
printf("2 - Enter the array elements\n");
printf("3 - Sort the array\n");
printf("4 - Find a number within the array\n");
printf("5 - Print the Array\n");
printf("6 - Reverse print the array\n");
printf("7 - Quit\n");
printf("\n");
printf("Please Enter your choice: ");
scanf("%d", &choice);
switch (choice){
case 1:
int tempSize = sizeArray();
case 2:
if (tempSize != 0){
enterElements(tempSize);
}
else{
printf("You should first set the size of the array\n");
}
default:
if (choice < 1 || choice > 7)
printf("Invalid choice please choose again\n");
};
} while (choice != 7);
}
int sizeArray(){
int size = 0;
printf("What is the size of your array(1-20)? ");
scanf_s("%d", size);
if (size > 20 || size < 1){
printf("Array size should be between 1 and 20 ");
}
return size;
}
int *enterElements(int size){
int i = 0;
for (i = 0; i < size; i++)
{
printf("Enter Array Element %d :", i);
scanf_s("%d", &anArray[i]);
}
}
【问题讨论】:
-
int tempSize = sizeArray();:删除int,在此之后添加break;。 -
请不要破坏您的问题;该代码与回答此问题相关。
标签: c menu switch-statement case