Click here to Skip to main content
15,904,934 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to get this C++ program to work so that the program reruns itself when 'y' is entered and when 'n' is entered, it stops running and just asks the user to enter any key to end it. I do not know what error I have though.

What I have tried:

C++
tnt main() {

    char again; // to hold y or n input

    if (again == 'y'){
    // Asks user if they want to play again
       cout << "Would you like to play again? (y/n):";
       cin >> again;
    } else if (again == 'n') {
      cout << "Press any key to continue." << endl;
      cin.ignore(1);
    }
}
Posted
Updated 14-Dec-22 19:06pm
v2

Quote:
reruns itself when 'y' is entered and when 'n' is entered,
Sure, for this you make it run again, and again, and again, until the condition (inputing 'n') is met.

How do we make a program a group of statements again and again? Use the guide that you are reading to learn about these structures in C++. There are good practices and bad practices, try them out and if you don't understand, come back to us and we will help you. :-)

You are looking for a do...while statement here to perform a set of actions until the condition is true. The condition, in this case, is that the user inputs 'y'.

do-while loop - cppreference.com[^]

Oh, and there is a missing closing brace (unless you missed copying the last line of your program).
 
Share this answer
 
v2
Comments
randomp103 9-Apr-20 1:18am    
I've been trying to figure this out for an hour now that's why I decided to post it on here to see if anyone can give me any clues on how to fix it. @Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan 9-Apr-20 10:13am    
Please study the C++ control structures, the one you are looking for is a do...while to perform an input capture and then re-execute the code until the user continues to provide 'y' in the input.
The first thing you need is a loop construct: there are three basic ones: for, while, and do
In this case, I'd suggest a do - this is a loop that ends with a test:
C++
do
   {
   ... your code here ...
   cout << "Would you like to play again? (y/n):";
   cin >> again;
   } while (again == 'y')
 
Share this answer
 
Quote:
I am trying to get this C++ program to work so that the program reruns itself when 'y' is entered

Never try to rerun a program itself, it don't work.
You have to use a loop: C++ do...while loop - Tutorialspoint[^]
your code have a few problems:
C++
tnt main() {           // Error here: first word is int
    char again;        // The variable is not initialized
    if (again == 'y'){ // At this place, again is unknown
       cout << "Would you like to play again? (y/n):";
       cin >> again;
    } else if (again == 'n') {
      cout << "Press any key to continue." << endl;
      cin.ignore(1);
    }
}
 
Share this answer
 
#include<iostream>
using namespace std;
int main() {

    char again; 
    start:
    cout << "Would you like to play again? (y/n):"; // to hold y or n input
 
    cin >> again;
    
    if (again == 'y'){
       system("CLS"); // to clear the screen
       goto start; // it will go back to asking if they wants to play again or not
      
      } else if (again == 'n') {
      cout << "Press any key to continue." << endl;
      cin.ignore(1);
     
    }
}
 
Share this answer
 
v3
Comments
CHill60 15-Dec-22 4:25am    
Reason for my downvote: C++ is a modern language that comes with many structured programming constructs such as do ... while. Using goto leads to Spaghetti code[^] and is considered bad practice in all but a (very) few occasions. This is not one of those occasions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900