Simple question...

This is a discussion on Simple question... within the C++ Programming forums, part of the General Programming Boards category; I would like to know what to include in my programs so that they go back to the start of ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up Simple question...

    I would like to know what to include in my programs so that they go back to the start of the program once its done so you can start again.

    example code:

    #include <iostream.h>
    #include <stdio.h>

    int main()
    {
    int number1 = 0;
    cout << "Enter a number";
    cin >> number1;

    int number2;
    cout << "Enter another number";
    cin >> number2;

    int total;
    total = (number1) + (number2);
    cout << "Total is:";
    cout << total;

    return 0;
    }


    What would you add to this example code to make it so that the program goes back to the beginning after it gives you the answer?

    Thanks
    -Chris
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    A do loop works nice

    #include <iostream>
    using namespace std;

    int main()
    {
    char response;
    do {
    int number1 = 0;
    cout << "Enter a number";
    cin >> number1;

    int number2;
    cout << "Enter another number";
    cin >> number2;

    int total;
    total = (number1) + (number2);
    cout << "Total is:";
    cout << total << endl;

    cout << "Add another number? (Y/N)";
    cin >> response;
    } while (response == 'y' || response == 'Y');

    return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    Look up control loops in your text or tutorial. They include the do/while loop, an example of which was provided, as well as while loops and for loops. Loops are a big thing in writing code so learning how to use them effectively will be good training for you. Basically the loops allow you to control how often to do something so you don't need to keep writing the same basic code over and over again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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