how do i loop this?

This is a discussion on how do i loop this? within the C++ Programming forums, part of the General Programming Boards category; How do i get this code to loop infinitely until the user tells it to stop? # include <iostream> # ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    how do i loop this?

    How do i get this code to loop infinitely until the user tells it to stop?

    # include <iostream>
    # include <cmath>
    using namespace std;

    double average, sd, s1, s2, s3, s4, deviation;

    int main()
    {
    cout << "Press enter after entering a value.\n";
    cout << endl;
    cout << "Enter score one: ";
    cin >> s1;
    cout << "\nEnter score two: ";
    cin >> s2;
    cout << "\nEnter score three: ";
    cin >> s3;
    cout << "\nEnter score four: ";
    cin >> s4;

    average =(s1+s2+s3+s4)/4;
    deviation =(((s1-average)*(s1-average))+((s2-average)*(s2-average))+((s3-average)*(s3-average))+((s4-average)*(s4-average)))/3;
    sd = sqrt(deviation);

    cout << "\nThe average of your four scores is "<<average<<".\n";
    cout << "The standard deviation of your scores is "<<sd<<".\n";

    return 0;
    }



    what i mean is that i would like the user to be able to repeat the program. i would like the user to be able to input different scores each time. This would happen over and over again until the user wanted to stop. For instance,

    average
    mean

    "would you like to run the program again? y or n"

    then they would hit "y" as long as they wanted to use the program. when they want to quit, they would hit "n"

  2. #2
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Hi,

    You would need to put it in a while loop
    Code:
    # include <iostream>
    # include <cmath>
    #include <conio.h> //for use of getch()
    
    using namespace std;
    
    double average, sd, s1, s2, s3, s4, deviation;
    
    char yesno = 'y'; //while loop control variable
    
    int main()
    {
    while (yesno!='n'&&yesno!='N') //will execute as long as yesno is not equal to 'n' or 'N'
    {
        cout << "Press enter after entering a value.\n";
        cout << endl;
        cout << "Enter score one: ";
        cin >> s1;
        cout << "\nEnter score two: ";
        cin >> s2;
        cout << "\nEnter score three: ";
        cin >> s3;
        cout << "\nEnter score four: ";
        cin >> s4;
        
        average =(s1+s2+s3+s4)/4;
        deviation =(((s1-average)*(s1-average))+((s2-average)*(s2-    average))+((s3-average)*(s3-average))+((s4-average)*(s4-    average)))/3;
        sd = sqrt(deviation);
    
        cout << "\nThe average of your four scores is "<<average<<".\n";
        cout << "The standard deviation of your scores is "<<sd<<".\n";
    
        cout<<"Would you like to run the program again?";
        cin>>yesno;
    }
    
    cout<<"Thankyou bye";
    getch();
    return 0;
    }
    I hope this isn't too confusing, you seem to know what you are on about.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    14
    the compiler i am using is mvisual 6.0. Would that work with my compiler?

  4. #4
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    I,m reasonably sure it would but if you want you can get rid of the conio.h and getch(); bits so then I'm positive it would,

    where I have put cout<<"Thankyou bye": and then getch();

    this was just to signify the end of the loop, if the user enters 'y' or 'Y' for yes then it will loop back to the top because 'y' and 'Y' are not equal to 'n' or 'N' if the user enters 'n' or 'N' the that makes the condition in the while statement false so the loop will exit.

    While loop only execute the code in the parantheses if its condition is true 'y' is not equal to 'n' or 'N' so that is true.

    I hope this is a good enough explanation.
    Last edited by UnclePunker; 10-21-2002 at 08:01 AM.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  5. #5
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    I've just tested it, it definitely does work, get rid of the

    #include <conio.h> at the top and then get rid of the getch();
    at the bottom because your compiler basically does that for you anyway/
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21