简体   繁体   中英

Why does my program only loop 2 times? (Beginner C++)

My problem is with the if/else if statement at the end of my code.

  • The if statement is supposed to loop infinitely as long as Y is entered, although it only runs twice and does not ask the user to run it again on the second run

  • The else if statement should close the program entirely if the user enters N

  • The else statement should keep prompting the user to enter a character until a valid character is entered.

-

 #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
}

}

There were some mistakes in your code:

  1. First of all you initialize validData with false , and it your loop you check it to see if it's still false then do it again, but your goal here is to continue as far as data is valid or user enter Y , So change your initialization to true and change your while loop condition to check if it is true .
  2. You called getData and isDataValid functions at first, there is no need to do that, you are should call them in loop, and by the way you are calling isDataValid in getData , So why call it again?
  3. You forgot to call printBox function inside the loop.
  4. in function printBox you get input from user one time and if is Y you start an infinitive loop! This shouldn't be your goal here, you want to ask user each time right?

So if you got your mistakes you can change main like this :

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;
}

and change printBox like this :

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;
    }

}

Here, is the basic problem. In order to get a repeated controlled output, certain things has to be inside the while loop (use while loop as it is easy for these cases).

  1. while loop must have an input statement, ie, you must be able to input some data inside the loop (otherwise you will get an infinite loop).

now, in order to get your program ask for multiple input, put getData inside the while loop.

  1. it must display the intended result

so, in your case put printData() inside while loop after getdata() (because output comes after input)

  1. finally, ask user if he wants to continue or not and make sure to give a boolean statement (example . bool repeat = true) and then set it to false if the user does not wish to continue.

your final code should look like this

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
}
}
}

and update your printData function to this

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;
}

}

the rest is unnecessary and not required

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM