我的问题是我的代码末尾的if / else if语句。
只要输入Y,if语句应该无限循环,尽管它只运行两次并且不会要求用户在第二次运行时再次运行它
如果用户输入N
else语句应该一直提示用户输入一个字符,直到输入有效字符为止。
-
#include <iostream>
using namespace std;
bool getData(int & width, int & height);
bool isDataValid(int & width, int & height);
void printBox(int & width, int & height);
int main()
{
int width = 0;
int height = 0;
bool validData = false;
getData(width, height);
isDataValid(width, height);
printBox(width, height);
while (validData == false)
{
validData = getData(width, height);
}
system("pause");
return 0;
}
bool getData(int & width, int & height)
{
bool validData = true;
cout << "This program will draw a rectangular box in stars." << endl << endl << "The size of the box will be determined by the width and height" << endl << "that you specify. " << endl << endl << "Enter integer values, because the width represents the " << endl << "numbe of columns, and the hieght represents the number of rows." << endl << endl << "The width should not exceed 79, because 80 is the " << "maximum screen " << endl << "width. Both width and height must be " << "at least 1. " << endl << endl;
cout << "Please enter a width: ";
cin >> width;
cout << "Please enter a height: ";
cin >> height;
cout << endl << endl;
return validData = isDataValid(width, height);
}
bool isDataValid(int & width, int & height)
{
if (width > 0 && width < 80 && height > 0)
{
return true;
}
else
{
cout << "Incorrect entry.\n\n";
return false;
}
}
void printBox(int & width, int & height)
{
const int ROWS = height;
const int COLS = width;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
cout << '*';
}
cout << endl;
}
char choice;
cout << endl << endl;
cout << "Do it again? (Y/N)" << endl;
cin >> choice;
while (toupper(choice == 'Y'))
{
// repeat the program
}
if (toupper(choice == 'N'))
{
cout << "Goodbye.\n\n";
// close program
}
}
答案 0 :(得分:0)
您的代码中存在一些错误:
validData
初始化false
,然后通过循环检查它是否仍然false
然后重新执行,但您的目标是要在数据有效或用户输入Y
时继续,请将初始化更改为true
并更改while循环条件以检查它是否为true
。getData
和isDataValid
函数,没有必要这样做,您应该在循环中调用它们,并且在isDataValid
中调用getData
{1}},为什么要再打电话呢?printBox
函数。printBox
中,您可以从用户那里获得一次输入,如果是Y
,则会启动一个不定式循环!这不应该是你的目标,你想每次都询问用户吗?因此,如果您犯了错误,可以像这样更改main
:
int main()
{
int width = 0;
int height = 0;
bool validData = true;
char choice = 'Y';
while (validData == true && (choice == 'Y' || choice == 'y'))
{
validData = getData(width, height);
printBox(width, height);
cout << endl << endl;
cout << "Do it again? (Y/N)" << endl;
cin >> choice;
if (choice == 'N' || choice == 'n') {
cout << "GoodBye" << endl;
break;
}
}
system("pause");
return 0;
}
并像这样更改printBox
:
void printBox(int & width, int & height)
{
const int ROWS = height;
const int COLS = width;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
cout << '*';
}
cout << endl;
}
}
答案 1 :(得分:0)
这里是基本问题。为了获得重复的受控输出,某些事情必须在while循环中(使用while循环,因为这些情况很容易)。
现在,为了让你的程序要求输入多个输入,将getData放在while循环中。
所以,在你的情况下把putData()放在getdata()之后的循环中(因为输出后输出)
您的最终代码应如下所示
int main()
{
int width = 0;
int height = 0;
bool repeat = true;
char ch;
while(repeat==true)
{
getData(width, height);
printBox(width, height);
isDataValid(width, height);
cout<<"\n\ndo you wish to continue?(y/n)\n"<<endl;
cin>>ch;
if(ch=='Y'||ch=='y')
continue;
else
{
cout<<"Goodbye!"<<;
break; //you can also do repeat = false
}
}
}
并将printData函数更新为此
void printBox(int & width, int & height)
{
const int ROWS = height;
const int COLS = width;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
cout << '*';
}
cout << endl;
}
}
其余的是不必要的,不是必需的