Help creating a do-while loop.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Help creating a do-while loop.

  1. #1
    Join Date
    Apr 2014
    Posts
    1

    Help creating a do-while loop.

    I need help starting a do-while loop in my program that runs up to 10 time that asks the user to enter up to 10 scores, which include the slope and rating for the course, and calculates the handicap for each score. Any help is greatly appreciated. Here's what I have so far:


    //This program calculates a golfers handicap.
    #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {

    int score, slope;
    double rating, handicap;
    string name;


    cout << "This program calculates a golfer's handicap.\n";

    //Have the user to input their name, score, slope, and handicap.
    cout << "Enter your name: ";
    cin >> name;
    cout << "Hello " << name << endl;
    cout << "Please enter your score: ";
    cin >> score;
    cout << "Please enter the course slope: ";
    cin >> slope;
    cout << "Please enter the course rating: ";
    cin >> rating;

    //Calculate the golfers handicap
    handicap = (score-rating) * 113 / slope ;
    cout << "Your handicap is " << handicap << endl;


    return 0;

    }

  2. #2
    Join Date
    Aug 2009
    Posts
    439

    Re: Help creating a do-while loop.


  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,461

    Re: Help creating a do-while loop.

    When postimg code, please use code tags. Go Advanced, select the code and click '#'.

    up to 10 time that asks the user to enter up to 10 scores,
    What is meant by 'up to 10'. How will the program know when the user has finished inputting data if less than 10 are required to be input?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    May 2014
    Posts
    2

    Re: Help creating a do-while loop.

    Have a counter that counts how many times a user enters a score, then you can do something like:

    Code:
    while (counter > 0)
    {
    //do whatever you want
    counter--;
    }
    This could also be done with a for loop, which in my opinion would be more appropriate.

    Code:
    for (int i = counter; i < 0; i--)
    {
    //do whatever you want
    }

  5. #5
    Join Date
    May 2014
    Posts
    2

    Re: Help creating a do-while loop.

    Quote Originally Posted by zeroday View Post
    Have a counter that counts how many times a user enters a score, then you can do something like:

    Code:
    while (counter > 0)
    {
    //do whatever you want
    counter--;
    }
    This could also be done with a for loop, which in my opinion would be more appropriate.

    Code:
    for (int i = counter; i < 0; i--)
    {
    //do whatever you want
    }
    oops, in my for loop it should be "i > 0" not what i have, sorry i dont know how to edit posts

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center