Thread: New to programming, need help

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    18

    New to programming, need help

    I got my first homework assignment for my computer science class, I am a major in financing so I appreciate any help possible.

    This is what must be written.

    Write a Pre-c++ program that keeps on inputting numbers until the number is zero is input, at which time the program stops. After each number (including zero) has been input, the program outpus to the screen (at the start of a new line) the words "Thnumber is ", followed by the actual number in question. After all the inputting has finished, the program outputs to the screen the word "Finished" at the start of a new line.

    I have part of it started, but im a little lost.

    Set up computer

    Void prompt ( );
    Main ()

    int x;
    prompt ();
    while (x!=0)
    [
    cout<<"The number is:"<<x<<endl;
    ]

    My problem is when the number 0 is inputted I still need to have that Statement saying "The number is:" 0 and then the finished statement after words. Is it possible to use an if statement after the while statement saying.
    If (x==0)
    cout<<"The number is:"<<x<<endl;
    cout <<finished<<endl;

    Any input is appreciate.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Couple things to start off:
    1. 'Void' should be 'void'
    2. 'Main()' should be 'int main()' and needs a '{' after it
    3. The '[' and ']' need to be '{' and '}'

    Now to the problem at hand. What you want is some loop construct which works as follows:
    execute block of code
    evaluate condition, go back to beginning if necessary

    More specifically to your problem:
    1. get x from user
    2. print "The number is: " + x
    3. if x != 0, go to 1


    C++ actually has a construct like this. It is the do...while loop. The block of code will always be executed before the condition is evaluated.
    Code:
    do {
      // some code... get input... print data... etc
    } while(condition);
    That should get you through the first problem. To the next!

    You only have to print "Finished" once, which implies that it really doesn't need to be looped over. Also, the code after your loop will be executed only on the condition that the loop has terminated, which implies that x == 0.

    Hope that helps ya.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    18

    Thanks for the reply

    I would like to use the do, while loop, but we just started this programming class and we haven't even covered what you had mentioned. Is their any other way to put it down.

    Im thinking

    int x;
    cout<<"Enter a number:"
    cin>>x;
    while (x!=0)

    cout<<"The number is:"<<x<<endl;


    cout<<"The number is:"<<x<<endl;
    cout<<"Finished"<<endl<<;

    Thanks for your help.
    I'm sure I will be asking of it the farther i get into the class.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yes I don't see why not as long as you put the curly braces where they belong :P
    Code:
    int x;
    cout<<"Enter a number:"
    cin>>x;
    while (x!=0)
    {
    cout<<"The number is:"<<x<<endl;
    cout<<"Enter a number:"
    cin>>x;
    }
    
    cout<<"The number is:"<<x<<endl;
    cout<<"Finished"<<endl<<;

    But a do while loop is pretty simple too
    Code:
    ...
    int x;
    do
    {
    cout<<"Enter a number:"
    cin>>x;
    } while(x!=0);
    ...
    Just eliminated a few lines
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What about using goto?

    The do while loop is good because the expression is only tested after each loop, so it executes at least once. Then you only have to write the input code once. Although, the performance difference would probably be minimal, depending on how good your computer is.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed