Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 9th, 2004, 9:49 AM   #1
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5 TecBrain is on a distinguished road
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.
TecBrain is offline   Reply With Quote
Old Oct 9th, 2004, 11:14 AM   #2
Ravilj
Programmer
 
Ravilj's Avatar
 
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 5 Ravilj is on a distinguished road
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!
Ravilj is offline   Reply With Quote
Old Oct 9th, 2004, 12:02 PM   #3
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5 TecBrain is on a distinguished road
int pos = 0;
int neg = 0;
int number;

Has solved the problem.

Thanks.
__________________
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.
TecBrain is offline   Reply With Quote
Old Oct 9th, 2004, 7:17 PM   #4
Ravilj
Programmer
 
Ravilj's Avatar
 
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 5 Ravilj is on a distinguished road
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!
Ravilj is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:39 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC