0

I've been programming for like, a week or something and I can't figure out how to make a program restart in C ++ once you've reached the end. Can someone help me?

I've written this begginer letter grade program that tells you based on your score, what letter grade you got. But as I said, once you reach the end and you're given the answer, the program ends, like expected, but I want it to restart and let me put in another number. Thanks!

#include <iostream>

using namespace std;

int main()
{

long int grade;
string x = " ";


cout << "Please, enter your grade. (0-100)" << endl;
cin >> grade;

int P =grade == 100;
int A =grade >= 90;
int B =grade >= 80;
int C =grade >= 70;
int D =grade >= 60;
int F =grade >=  0;

if(P){cout << "You have a perfect score." << endl << endl;}else{
if(A){cout <<"You have an A." << endl<< endl; }else{
    if(B){cout <<"You have a B." << endl << endl;}else{
        if(C){cout <<"You have a C." << endl;}else{
            if(D){cout <<"You have a D." << endl<< endl;}else{
                if(F){cout <<"You have an F."<< endl<< endl;}else{
                    cout << "Invalid input.";
                }
            }
        }
    }
 } 
}
  cout << "Enter x to finish the program" << endl << endl;
cin >> x;

}
Andrei Bercea
  • 13
  • 1
  • 2
  • 3
    What you need is a loop. A [good book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) can teach you about loops. – Greg Kikola Feb 12 '17 at 07:31
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Feb 12 '17 at 07:32
  • I think you should read more about the basics of structured programming in imperative languages. Even Wikipedia can help you here: https://en.wikipedia.org/wiki/Structured_programming. "Restart the program" sounds too much like "jump to the beginning", and that in turn sounds too much like the `goto` statement, which is the bane of non-structured programming. You need to learn why **loops** were invented in structured high-level programming languages, and use them. – Christian Hackl Feb 12 '17 at 09:28

1 Answers1

1

Put the entire code in a do while loop until the user enter 0 to stop the program.

like this:

int input = 0;
do
{
     //You need to put your entire code here!

cin >> input;
}while(input != 0)
Moshe D
  • 768
  • 1
  • 4
  • 13
  • _Thanks! Comments must be at least 15 characters in length... soo I had to write this in order to send the comment :)). Have a nice day! – Andrei Bercea Feb 12 '17 at 08:22