Thread: Using the for loop

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    3

    Using the for loop

    I am programming this test commentor and I am trying to get it to restart again but I can't figure it out.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int percentage, i;
        
        cout<<"Input your test percentage: ";
        cin>>percentage;
        
        cin.ignore();
        
        if (percentage<50){
                           cout<<"You failed. Study harder.\n";
                           }
        else if (percentage<60){
                                cout<<"You just passed. Study harder.\n";
                                }
        else if (percentage<70){
                                cout<<"You didn't do very well.\n";
                                }
        else if (percentage<80){
                                cout<<"You did quite well.\n";
                                }
        else if (percentage<90){
                                cout<<"You did excellent.\n";
                                }
        else if (percentage<100){
                                cout<<"You did very well.\n";
                                }
        else if (percentage>100){
                                cout<<"Who marked your test?\n";
                                }
        else if (percentage==100){
                                cout<<"You did perfect.\n";
                                 }
        cin.get();
    }
    What do I put in and where?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    while ( someCondition ) {
      // the stuff you want to repeat
    }

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    A for loop is generaly used when you have a set number of times the loop should be executed. (There are "tricks" you can use on them but that's another discusion)

    A while loop is generaly used to execute as long as a condtion is true, a do while loop is the same as a while loop however it will execute at least once.

    The syntex for a for loop is

    for (variable initialization; conditional; variable increment) // the varable initialization is normaly an int.

    Code:
    for example for ( int i =0; i<Max_Percents; i++)
    {
       the code you want to repeat
    }

    if you want to the user to enter data, indefinitly you have two options.

    1. You can have a Sentinel- Controlled Loop, which has the user enter a unique value to end the loop
    Code:
    do
    {
     your code
    }while (input != sentinal);
    2. You can put your input into a while statment, to end the loop you would press cntl+d. This is more benificial for reading from files, then user input though

    ex:
    while (cin>>percentage)

  4. #4
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    I just had to, i modified the code to the only way i know how to repeat a program,
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int percentage, i, x;
        x=1;
        
        while(x==1)
        {
    	
        cout<<endl;
        
        cout<<"Input your test percentage: ";
        cin>>percentage;
        
        cin.ignore();
        
        if (percentage<50){
                           cout<<"You failed. Study harder.\n";
                           }
        else if (percentage<60){
                                cout<<"You just passed. Study harder.\n";
                                }
        else if (percentage<70){
                                cout<<"You didn't do very well.\n";
                                }
        else if (percentage<80){
                                cout<<"You did quite well.\n";
                                }
        else if (percentage<90){
                                cout<<"You did excellent.\n";
                                }
        else if (percentage<100){
                                cout<<"You did very well.\n";
                                }
        else if (percentage>100){
                                cout<<"Who marked your test?\n";
                                }
        else if (percentage==100){
                                cout<<"You did perfect.\n";
                                 }
        cout<<"\n \n \n To try again press 1, or press any key to exit!\n";
        cin>>x;
        cin.ignore();
    	}
        
    }
    now i am sure there is another more "Clean" way of doing it, if so could anybody please explain?

    And would it not be better to use something like
    Code:
    else if(percentage<90&&percentage>79){
    Just askin as thats how i would do it
    Last edited by 74466; 01-31-2006 at 01:20 AM. Reason: replaced : with && silly mistake

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    First things first: you have to learn to indent things properly. Do a google search, and examine some sample code.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And would it not be better to use something like
    Code:
    else if(percentage<90&&percentage>79){
    You don't need to, because it's an else if, and the previous ifs have made sure that percentage is more than 79.

    Look at this program.
    Code:
    #include <iostream>
    #include <cctype>
    
    using std::cout;
    using std::cin;
    using std::tolower;
    
    int main(void) {
        char again;
    
        do {
            do_something();
    
            cout << "Do that again? ";
            cin >> again;
        } while(tolower(again) == 'y');
    }
    tolower() converts a letter to lowercase (there's also a toupper). These functions are in <cctype>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed