我正在尝试创建一个菜单,但我的代码一直在循环
I'm trying to create a menu but my code keeps looping
我正在尝试创建一个菜单,用户可以在其中选择一个数字来执行某个功能,但我的代码一直在循环。我的二维数组甚至在 for 循环之外也保持循环语句。我找到了一种使用
停止循环的方法
cout << endl;
cout << "Enter another number or press -1 to exit: ";
cin >> userNum;
但后来我用一个简单的 cout 语句(usernum ==3 部分)测试了它,它也循环了。我不确定我做错了什么?我认为这与我的 while 循环有关,但我不确定。我将不胜感激!
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
// function #1, finds the minimum value in a array
int findMin(int a[], int size){
int i;
int min;
min = a[0];
for (i = 1; i < size; ++i) {
if(min > a[i]){
min = a[i];
}
}
return min;
}
// will write other functions later
int main()
{
int userNum = 0;
int arraySize = 4;
int array[arraySize];
int i;
int j;
int rowSize = 3;
const int colSize = 4;
int array2[rowSize][colSize];
cout << "MENU" << endl;
cout << "-1: exit" << endl;
cout << "1: minimum value of integer array" << endl;
cout << "2: computes the average of all elements in a 2D array" << endl;
cout << "3: Is it a leap year?" << endl;
cout << "4: calculates addition, subtraction, multiplation, divison" << endl;
cout << "5: read a text file and store each word into a vector of string" << endl;
cout << "6: count the frequency of each word" << endl;
会
cout << "7: remove all the stop words from words" << endl;
cout << "Please input a number from the menu:" << endl;
cin >> userNum;
while (userNum != -1) {
if (userNum == 1) {
cout<<"Enter 4 array elements: ";
for(i=0; i< arraySize; i++){
cin>>array[i];
}
cout << "minimum value of integer array: " << findMin(array, arraySize) << endl;
cout << "Enter another number or press -1 to exit: ";
cin >> userNum;
}
else if (userNum == 2) {
cout << "here are the elements in the 2d array.";
for (i=0; i < rowSize; i++ ){
cout<< endl;
for (j =0; j < colSize; j=j+1){
array2[i][j] = 1+(rand()%9);
cout << array2[i][j] << " ";
}
}
cout << endl;
cout << "Enter another number or press -1 to exit: ";
cin >> userNum;
}
else if (userNum == 3){
cout << "hahah" << endl;
}
l
else {
cout << "Enter another number or press -1 to exit: ";
cin >> userNum;
}
}
cout << "exited";
return 0;
}
你的 userNum == 3
案例没有读入新的 userNum
,所以这就是它循环的原因。一旦 userNum
为 3,它就再也不会改变了。
修复它的一种方法是将您的 "get user input" 代码移动到一个单独的函数中,然后在循环结束时调用它。这样您就不必到处重复代码,并且在实现新案例时也不会忘记添加它。
像这样:
int getMenuItem()
{
cout << "MENU" << endl;
cout << "-1: exit" << endl;
cout << "1: minimum value of integer array" << endl;
cout << "2: computes the average of all elements in a 2D array" << endl;
cout << "3: Is it a leap year?" << endl;
cout << "4: calculates addition, subtraction, multiplation, divison" << endl;
cout << "5: read a text file and store each word into a vector of string" << endl;
cout << "6: count the frequency of each word" << endl;
cout << "7: remove all the stop words from words" << endl;
cout << "Please input a number from the menu:" << endl;
int input;
cin >> input;
return input;
}
int main()
{
...
userNum = getMenuItem(); // <- Before the loop, to get the initial selection
while (userNum != -1) {
if (userNum == 1) {
...
}
else if (userNum == 2) {
...
}
else if (userNum == 3) {
...
}
userNum = getMenuItem(); // <- At the end of the loop, to get the next
}
cout << "exited";
return 0;
}
我正在尝试创建一个菜单,用户可以在其中选择一个数字来执行某个功能,但我的代码一直在循环。我的二维数组甚至在 for 循环之外也保持循环语句。我找到了一种使用
停止循环的方法 cout << endl;
cout << "Enter another number or press -1 to exit: ";
cin >> userNum;
但后来我用一个简单的 cout 语句(usernum ==3 部分)测试了它,它也循环了。我不确定我做错了什么?我认为这与我的 while 循环有关,但我不确定。我将不胜感激!
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
// function #1, finds the minimum value in a array
int findMin(int a[], int size){
int i;
int min;
min = a[0];
for (i = 1; i < size; ++i) {
if(min > a[i]){
min = a[i];
}
}
return min;
}
// will write other functions later
int main()
{
int userNum = 0;
int arraySize = 4;
int array[arraySize];
int i;
int j;
int rowSize = 3;
const int colSize = 4;
int array2[rowSize][colSize];
cout << "MENU" << endl;
cout << "-1: exit" << endl;
cout << "1: minimum value of integer array" << endl;
cout << "2: computes the average of all elements in a 2D array" << endl;
cout << "3: Is it a leap year?" << endl;
cout << "4: calculates addition, subtraction, multiplation, divison" << endl;
cout << "5: read a text file and store each word into a vector of string" << endl;
cout << "6: count the frequency of each word" << endl;
会
cout << "7: remove all the stop words from words" << endl;
cout << "Please input a number from the menu:" << endl;
cin >> userNum;
while (userNum != -1) {
if (userNum == 1) {
cout<<"Enter 4 array elements: ";
for(i=0; i< arraySize; i++){
cin>>array[i];
}
cout << "minimum value of integer array: " << findMin(array, arraySize) << endl;
cout << "Enter another number or press -1 to exit: ";
cin >> userNum;
}
else if (userNum == 2) {
cout << "here are the elements in the 2d array.";
for (i=0; i < rowSize; i++ ){
cout<< endl;
for (j =0; j < colSize; j=j+1){
array2[i][j] = 1+(rand()%9);
cout << array2[i][j] << " ";
}
}
cout << endl;
cout << "Enter another number or press -1 to exit: ";
cin >> userNum;
}
else if (userNum == 3){
cout << "hahah" << endl;
}
l
else {
cout << "Enter another number or press -1 to exit: ";
cin >> userNum;
}
}
cout << "exited";
return 0;
}
你的 userNum == 3
案例没有读入新的 userNum
,所以这就是它循环的原因。一旦 userNum
为 3,它就再也不会改变了。
修复它的一种方法是将您的 "get user input" 代码移动到一个单独的函数中,然后在循环结束时调用它。这样您就不必到处重复代码,并且在实现新案例时也不会忘记添加它。
像这样:
int getMenuItem()
{
cout << "MENU" << endl;
cout << "-1: exit" << endl;
cout << "1: minimum value of integer array" << endl;
cout << "2: computes the average of all elements in a 2D array" << endl;
cout << "3: Is it a leap year?" << endl;
cout << "4: calculates addition, subtraction, multiplation, divison" << endl;
cout << "5: read a text file and store each word into a vector of string" << endl;
cout << "6: count the frequency of each word" << endl;
cout << "7: remove all the stop words from words" << endl;
cout << "Please input a number from the menu:" << endl;
int input;
cin >> input;
return input;
}
int main()
{
...
userNum = getMenuItem(); // <- Before the loop, to get the initial selection
while (userNum != -1) {
if (userNum == 1) {
...
}
else if (userNum == 2) {
...
}
else if (userNum == 3) {
...
}
userNum = getMenuItem(); // <- At the end of the loop, to get the next
}
cout << "exited";
return 0;
}