CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  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
    440

    Re: Help creating a do-while loop.


  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  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
  •  





Click Here to Expand Forum to Full Width

Featured