Like Tree2Likes
  • 1 Post By ZuK
  • 1 Post By laserlight

More Chapter 5 practice problems!

This is a discussion on More Chapter 5 practice problems! within the C++ Programming forums, part of the General Programming Boards category; Group, I am new to programming so bear with me here. I am going through the ebook and am having ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    15

    More Chapter 5 practice problems!

    Group,
    I am new to programming so bear with me here. I am going through the ebook and am having a problem with a practice problem in chapter 5. I am almost getting the results that I want but the number is not correct. Please advise. (Please, if it is a simple noobie mistake, be kind.)
    tsdad

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
    int input;
    int input2;
    
    cout << "Hey! Give me a number other than zero!" << "\n";
    cin >> input;
    
    if ( input != 0 )
        {
    
        while ( 1 )
        {
            cout << "Hey! Put in another number and I will add them up. But don't put in a zero or you will be called a Bonehead!" << "\n";
            cin >> input2;
    
            if ( input2 == 0 )
            {
    
                break;
            }
            int i;
    
            i = i + input2;
    
            cout << "The total of the numbers you gave me is " << i + input << " !" << "\n";
    
        }
    
        }
    
        cout << "Hey, Bonehead! I said not to put in a 0. Go away!" << "\n";
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    i is not initialized so it coud be any value.
    You don't need i anyway
    just print
    Code:
     cout << "The total of the numbers you gave me is " << input + input2 << " !" << "\n";
    Kurt

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    15
    Thank you Zuk but that would work if I only wanted to go through the sequence once. I want to be able to add repeated inputs together, as long as they are not 0.
    tdad

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Then you have to initialize i to input before the while(1) loop and print just i.
    Kurt

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    15
    you have to run this to understand the problem i am having. i am able to add multiple inputs to each other. the problem is that 2 plus 2 is 1977749862. than i add the next 2 and get 1977749864. the next 2 is 1977749866 and so on, correctly adding two to this odd beginning number. if i put a 0 in at any time it correctly goes to the end. it is the odd beginning number that is the problem.
    thanks for all your help,
    Dan

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I've told you, you are adding input2 to an ininitialized variable
    try
    Code:
        int i = input;  // initialize i
        while(1) {
            ....
    Kurt
    Elysia likes this.

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    15
    Quote Originally Posted by ZuK View Post
    I've told you, you are adding input2 to an ininitialized variable
    try
    Code:
        int i = input;  // initialize i
        while(1) {
            ....
    Kurt
    Thank you for your help. it works. i will think about it and ask a question about why this works, probably tomorrow
    again, thanks,
    dan

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    Quote Originally Posted by tsdad View Post
    Thank you for your help. it works. i will think about it and ask a question about why this works, probably tomorrow
    Make a flowchart, and be sure to include variables in it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by tsdad View Post
    you have to run this to understand the problem i am having.
    No we don't. His understanding is correct.
    If your program was 10 times more complicated many of us would likely still be able to understand it with a one minute glance over the code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    May 2013
    Posts
    15
    Thanks Kurt,
    Your first response, now that I have had time to absorb, was the best advice. I did not need i. The learning was to have an increment other than the common i++. Below is the final version that gives me the result I was looking for.
    Again, thanks,
    Dan

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
    int input;
    int input2;
    
    cout << "Hey! Give me a number other than zero!" << "\n";
    cin >> input;
    
    if ( input != 0 )
        {
    
        while ( 1 )
        {
            cout << "Hey, Dude! Put in another number and I will add them up. But don't put in a zero or you will be called a Bonehead!" << "\n";
            cin >> input2;
    
            if ( input2 == 0 )
            {
    
                break;
            }
    
            input = input + input2;
    
            cout << "The total of the numbers you gave me is " << input << " !" << "\n";
    
        }
    
        }
    
        cout << "Hey, Bonehead! I said not to put in a 0. Go away!" << "\n";
    }

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    >>input = input + input2;
    Can be shortened to
    input += input2;

    And if you want to be fancy, you can also do
    cout << "The total of the numbers you gave me is " << (input += input2) << " !" << "\n";
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    May 2013
    Posts
    15
    Thanks Elysia
    Dan

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by Elysia
    And if you want to be fancy, you can also do
    cout << "The total of the numbers you gave me is " << (input += input2) << " !" << "\n";
    But I suggest that you don't be fancy.
    King Mir likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    May 2013
    Posts
    15
    My time will come.
    Dan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chapter 5 practice program 2 difficulties
    By DaveHubbard in forum C++ Programming
    Replies: 15
    Last Post: 05-17-2013, 01:24 PM
  2. Chapter 2 practice problems
    By Kyle Spalding in forum C++ Programming
    Replies: 14
    Last Post: 03-19-2013, 06:12 AM
  3. Replies: 18
    Last Post: 03-07-2013, 05:55 AM
  4. Practice Problems
    By Eultza in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 11:13 AM
  5. Practice C Problems, with answers
    By john123 in forum C Programming
    Replies: 3
    Last Post: 12-01-2002, 05:46 PM

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