Results 1 to 11 of 11

Thread: The Control of C++

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    The Control of C++

    I am trying to learn C++ on my own right now from a book "C++ for dummies". It does not explain some things very well.
    So, I am confused about this:

    int sumSequence(void)
    {
    // loop forever
    int accumulator = 0;
    for(;
    {
    // fetch another number
    int value = 0;
    cout << "Enter next number: ";
    cin >> value;

    // if it's negative...
    if (value < 0)
    {
    // ...then exit from the loop
    break;
    }

    // ...otherwise add the number to the
    // accumulator
    accumulator= accumulator+ value;
    }// break goes here

    // return the accumulated value
    return accumulator;
    }

    int main(int arg, char* pszArgs[])
    {
    cout << "This program sums multiple series\n"
    << "of numbers. Terminate each sequence\n"
    << "by entering a negative number.\n"
    << "Terminate the series by entering two\n"
    << "negative numbers in a row\n";

    // accumulate sequences of numbers...
    int accumulatedValue;
    do
    {
    // sum a sequence of numbers entered from
    // the keyboard
    cout << "\nEnter next sequence\n";
    accumulatedValue = sumSequence();

    // now output the accumulated result
    cout << "\nThe total is "
    << accumulatedValue
    << "\n";

    // ...until the sum returned is 0
    } while (accumulatedValue != 0);
    cout << "Program terminating\n";
    return 0;
    }


    I ran this little program and found that int main showed up first. But before main finished it jumps up to the beginning and then back down below main. Here:

    cout << "\nThe total is "
    << accumulatedValue
    << "\n";

    Why?

  2. #2
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    int main is the starting function of your program and any program not using MFC, AFX or Window API. Thats why it started there. It jumps up because of accumulatedValue = sumSequence(); sumSequence() call the sumSequence function which is above main. Hope this helps some.

    I have never looked at the dummies book for C, but it doesn't seem like its giving you a good foundation to start with. You might want to try a different book. I like the Beginning Visual C++ by Ivor Horton the best. If you search around I bet you could find more recommend books and sites on the forum.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  3. #3

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    But then it jumps down below I guess because it got the input from the loops and then jumps back down there to finish. Is that right? C++ is that smart?

  4. #4
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Right once the function sumSequence is done it returns to where it left off in main
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  5. #5

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Ok, now my eyes are starting to open. I am used to Batch files.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    ouch.

    Do you know the call batch instruction, which lets you execute another batch file and then continue the current where you left? Like
    Code:
    dir /a /o
    call some.bat
    cd \
    Functions work just like that, only that they also have parameters and a return value (you can pass information to them and get information back).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    I didn't know about that batch command. But I am glad I am learning C++. Or maybe I'll choose C#. . .can't decide yet.

    Hope everything pays off marvolously (spell check please).

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    If you already started out with C++ then learn it. C# syntax is (with a few exceptions) a subset of C++ syntax, so once you know C++ learning C# is very easy.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    I think I will stick with C++ for now. Thank you for all your help.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You were welcome and you will be in the future too.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Thank you, CornedBee.

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