![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5
![]() |
I have to write a program that will read a list of numbers, adding positive and negative numbers into two different totals, and zero to quite.
I was able to do sth, if I only add > 0 the result will be right, but neg value will get some wired number and if I only add neg numbers the results will be correct but the value of the pos number will be wrong... though I don’t add any. If I add pos and neg the same time... One of them will cout the correct result. #include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int pos,neg,number;
cout << "Enter your number list then Enter 0" <<endl;
cin >> number;
if (number == 0) return 0;
if (number > 0)
pos = number;
else if (number < 0)
neg = number;
while (number!=0)
{
cin >> number;
if (number == 0 ) break;
else if (number > 0 )
pos = number + pos;
else if (number < 0 )
neg = number + neg;
}
cout << "Total of all Positive number is:" << pos <<endl;
cout << "Total of all Negative number is:" << neg <<endl;
system("PAUSE");
return 0;
}
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 5
![]() |
Your problem is the either pos or neg varialbe is initialised depending on the first number entered. You need to initialise them both first. An easy way to make sure of this is by doing the following:
int pos = 0; int neg = 0; int number = 0; Number in this case doesnt need to be initilised since it is equated to the used input where as with pos and neg you start adding values to them before the have been set to a value (or initialised). #include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int pos = 0;
int neg = 0;
int number;
do
{
cout << "Enter your number list then Enter 0" <<endl;
cin >> number;
if ( number > 0 )
pos += number;
else
neg += number;
}
while ( number != 0 );
cout << "Total of all Positive number is:" << pos <<endl;
cout << "Total of all Negative number is:" << neg <<endl;
system("PAUSE");
return 0;
}
__________________
Ravilj's OpenGL Terrain aka WinTerrain Last Updated: 17/01/2005! |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5
![]() |
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 5
![]() |
lol yeah i know, I just like trying these progs that people post about.
hence why there is the code there... :/
__________________
Ravilj's OpenGL Terrain aka WinTerrain Last Updated: 17/01/2005! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|