Loop

Hey all,

I'm new here, but I'm in college for C++ programming, unfortunately I'm only in my first C++ class and we haven't went over the loop statement yet.

But we do have to turn in programs, and I get tired of having to re run the program every time I do my desk checks.

So I was wondering if someone could show me how to add the loop statement here so it will run 4 times.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;
int main()
{
    char student = ' ';
    int hr = 0;
    int tuition = 0;
    
    cout << "Are you a in-state student? (y/n)";
    cin >> student;
    cout << "Enter class credit hours: ";
    cin >> hr;
    
    student = tolower(student);
    
    if (student == 'y')
    {
                tuition = 50 * hr;
                if ( tuition > 500 )
                {
                     tuition = 500;
                }
    }
    else 
    {
         tuition = 400 * hr;
         if ( tuition > 6000)
         {
              tuition  = 6000;
         }
    }
    
    cout << "Your tuition is: " << tuition << "$" << endl;
    system("PAUSE");
    return 0;
}


Thanks! :)
Last edited on
Um, which part of code needs to be run 4 times? I guess if and else?

To do so you use for loop. It should look like this.

1
2
3
for(int i = 0; i<4; i++){
     //Your code goes here. :-)
}


S.
i don't think the whole program can be rerunned with new code. once you added a new piece to your program you have to recompile your cpp file to let the program work again. since u can only compile it when your program isn't running this looks quite impossible to do so.
A bool statement is how I usually do it... a goto to make it fast(I hear goto is the devil though, I just haven't found a better way yet)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
     bool restart = false;
     char input;

     while (restart==false)
       {
         
        //your code 

        cout << would you like to restart? y/n? << endl;
         cin >> input;

         if(input=='n')
               restart = true; //ends the while statement because statement only runs when restart is true
         else
               restart = false;
        }//while loop
}// main function


The goto is cleaner though

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{

     char input;

beginning:  //make sure to put the colon after and this statement should not be indented.

        //your code 

         cout << would you like to restart? y/n? << endl;
         cin >> input;

         if(input=='y')
               goto beginning;
         else
              return 0;
}// main function 
OK thank you eyesofhope and ssegota.
@eyesofhope: You can use break; and continue;:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    #include <iostream>
    
    using namespace std;

    int main()
    {
        char choice;
        
        while(true)
        {
            // Stuff
            
            cout << "Are you finished? y/n: ";
            cin >> choice;
            
            if(choice == 'y'){
                break; // will exit the loop.
            } else {
                continue; // will immediately go to the start of the loop.
            }
            
            // Code here wouldn't be run because of continue.
        }
        
        cout << "You broke out of the loop. Ending Program.";
    }
Last edited on
Topic archived. No new replies allowed.