[英]Why does my program only loop 2 times? (Beginner C++)
我的問題是代碼末尾的if / else if語句。
只要輸入了Y,就應該使if語句無限循環,盡管它只運行兩次,並且不要求用戶在第二次運行時再次運行它。
如果用戶輸入N,則else if語句應完全關閉程序
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
}
}
您的代碼中有一些錯誤:
false
初始化validData
,然后在循環中檢查它是否仍然為false
然后再次執行此操作,但是這里的目標是繼續執行數據有效或用戶輸入Y
,因此將初始化更改為true
並更改while循環條件以檢查是否為true
。 getData
和isDataValid
函數,沒有必要這樣做,您應該在循環中調用它們,並且通過在getData
中調用isDataValid
的方式,為什么還要再次調用它呢? 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;
}
}
在這里,是基本問題。 為了獲得重復的受控輸出,某些內容必須在while循環內(使用while循環,因為在這些情況下很容易使用)。
現在,為了讓您的程序請求多個輸入,請將getData放入while循環中。
因此,在您的情況下,在getdata()之后將printData()放入while循環內(因為輸出在輸入之后出現)
您的最終代碼應如下所示
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;
}
}
其余的都是不必要的,不是必需的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.