C++ loop

Discussion in 'Software' started by laggy, Aug 10, 2010.

  1. laggy

    laggy Private E-2

    Does anone know how to loop this ,I made the code myself but have no idea with looping.it has to repeat the line Enter your test score: ,

    Code:
    #include <iostream>
    using namespace std;
    int main(void)
    {
       int testScore;
       cout << "Enter your test score: ";
       cin >> testScore;
       if (testScore >= 101 )
       cout << "Lol Impossible" << endl;
       else if (testScore >= 90 )
       cout << "Your grade is an A1" << endl;
       else if (testScore >= 85 )
       cout << "Your grade is an A2" << endl;
       else if (testScore >= 80 )
       cout << "Your grade is a B1" << endl;
       else if (testScore >= 75 )
       cout << "Your grade is a B2" << endl;
       else if (testScore >= 70 )
       cout << "Your grade is a B3" << endl;
       else if (testScore >= 65 )
       cout << "Your grade is a C1" << endl;
       else if (testScore >= 60 )
       cout << "Your grade is a C2" << endl;
       else if (testScore >= 55 )
       cout << "Your grade is a C3" << endl;
       else if (testScore >= 50 )
       cout << "Your grade is a D1" << endl;
       else if (testScore >= 45 )
       cout << "Your grade is a D2" << endl;
       else if (testScore >= 40)
       cout << "Your grade is a D3" << endl;
       else if (testScore >= 25 )
       cout << "Your grade is an E" << endl;
       else if (testScore >= 10 )
       cout << "Your grade is an F" << endl;
       else  
       cout << "No Grade ,well done ^-^ " << endl;
       system("pause");
       return 0;
    }
    
    Its a basic code cause I have only started learning c++
     
  2. Wyatt_Earp

    Wyatt_Earp MajorGeek

    You probably want to look at a while loop.
    Code:
    cin>>;
    While (the input is valid)
    {
        // Print out the score
        cin>>;
    }
     
  3. Kodo

    Kodo SNATCHSQUATCH

    I can't help but think that a dictionary object and looking up the values in the dictionary based on what the entered value was i.e. find Key Value Pair in Dictionary where VALUE == KEY, would be more efficient and easier to read.

    Would require a dictionary containing 0-100 and each grade associated, but would eliminate that mega if/then/else conditional logic.
     
  4. coder1

    coder1 Private E-2

    Declare a string variable, then put your statements all inside a while loop, and ask the user if they would like to run it again right before the end of the loop. String variable will be equal to "y".
     
  5. Fleming

    Fleming Guest

    hello guys.....
    If you want to make something to loop forever use

    either

    1
    2
    3


    for(;;){
    //put the code you want to loop forever here.
    }
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds