Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Need help with this c++ code, I believe Idk how to use while

+11 votes
asked Nov 17, 2022 by Alan Corleto (370 points)
/* I want  to repeat the code by pressing especifically  's' and  'n' to exit but I can't do it I was told that with a while loop would work but I believe Idk how to do it */
/* if the user use any other letter print a message saying error and try again, I believe I know how to do that part

but it's just confusing

*/
// I just need to close it with n

// thank you before hand

#include <iostream>
#include <stdlib.h>

int main()

{

using namespace std;

char repeat;
int age;

do{

cout << "ENTER YOUR AGE " << endl;
cin >> age;
system("clear");

cout << "would you like to repeat?  (Y) or (N)" << endl;
cin >> continuar;
system("clear");

}while(repeat== 'y' || repeat== 'Y');

   return 0;
}

2 Answers

0 votes
answered Nov 17, 2022 by Nathan Morgan (140 points)

Line 30: Change continuar to "repeat" so it looks like this: 

cin >> repeat;

Not sure why you had continuar in there.

Let me know if you need help with the 'n' 'N' part or the error enter again part

+1 vote
answered Nov 18, 2022 by Peter Minarik (86,040 points)

If you get your variable name right (continuar, repeat, etc), then your code actually works fine:

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    char repeat;
    int age;
    do
    {
        cout << "ENTER YOUR AGE " << endl;
        cin >> age;
        system("clear");
        
        cout << "would you like to repeat?  (Y) or (N)" << endl;
        cin >> repeat;
        system("clear");

    } while (repeat == 'y' || repeat == 'Y');

   return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...