# Thread: More Chapter 5 practice problems!

1. ## 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.)

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. 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. 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. 4. Then you have to initialize i to input before the while(1) loop and print just i.
Kurt 5. 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.
Dan 6. I've told you, you are adding input2 to an ininitialized variable
try
Code:
```    int i = input;  // initialize i
while(1) {
....```
Kurt 7. Originally Posted by ZuK I've told you, you are adding input2 to an ininitialized variable
try
Code:
```    int i = input;  // initialize i
while(1) {
....```
Kurt
again, thanks,
dan 8. Originally Posted by tsdad Make a flowchart, and be sure to include variables in it. 9. Originally Posted by tsdad 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. 10. 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. >>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"; 12. Thanks Elysia
Dan 13. 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. 14. My time will come.
Dan Popular pages Recent additions 