473,326 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

If-else statement

I'm trying to use if-else statements to write a program that will print out sum of positive and negative numbers that are input into the program. It prints out extremely huge number when i run the program. Any idea why?
Sep 13 '07 #1
15 2134
sicarie
4,677 Expert Mod 4TB
There could be a few different reasons for this, but I'm guessing that a variable you use is undefined, or goes through undefined behavior. Can we see the code inside your main statement?
Sep 13 '07 #2
i figured id just post the whole code....

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.   double num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, sum, sumpositive=0, sumpositive1, sumpositive2, sumpositive3, sumpositive4, sumpositive5, sumpositive6, sumpositive7, sumpositive8, sumpositive9, sumpositive10, sumnegative=0, sumnegative1, sumnegative2, sumnegative3, sumnegative4, sumnegative5, sumnegative6, sumnegative7, sumnegative8, sumnegative9, sumnegative10, mean;
  7.   cout<< "Enter 10 Numbers:";
  8.   cin>> num1;
  9.   cin>> num2;
  10.   cin>> num3;
  11.   cin>> num4;
  12.   cin>> num5;
  13.   cin>> num6;
  14.   cin>> num7;
  15.   cin>> num8;
  16.   cin>> num9;
  17.   cin>> num10;
  18.   sum=num1+num2+num3+num4+num5+num6+num7+num8+num9+num10;
  19.   cout<< "The sum of the numbers is: "<<sum<<".\n";
  20.   mean=sum/10.000;
  21.   cout.setf(ios::fixed);
  22.   cout.setf(ios::showpoint);
  23.   cout.precision(3);
  24.   cout<<"The mean of the sum is: "<<mean<<".\n";
  25.   {
  26.    if (num1 >= 0)
  27.      sumpositive1=sumpositive+num1;
  28.    else 
  29.      sumnegative1=sumnegative+num1;
  30.    if (num2 >= 0)
  31.      sumpositive2=sumpositive1+num2;
  32.    else
  33.      sumnegative2=sumnegative1+num2;
  34.    if (num3 >= 0)     
  35.      sumpositive3=sumpositive2+num3;
  36.    else     
  37.      sumnegative3=sumnegative2+num3;
  38.    if (num4 >= 0)
  39.      sumpositive4=sumpositive3+num4;
  40.    else
  41.      sumnegative4=sumnegative3+num4;
  42.    if (num5 >= 0)
  43.      sumpositive5=sumpositive4+num5;
  44.    else
  45.      sumnegative5=sumnegative4+num5;
  46.    if (num6 >= 0)
  47.      sumpositive6=sumpositive5+num6;
  48.    else
  49.      sumnegative6=sumnegative5+num6;
  50.    if (num7 >= 0)
  51.      sumpositive7=sumpositive6+num7;
  52.    else
  53.      sumnegative7=sumnegative6+num7;
  54.    if (num8 >= 0)
  55.      sumpositive8=sumpositive7+num8;
  56.    else
  57.      sumnegative8=sumnegative7+num8;
  58.    if (num9 >= 0)
  59.      sumpositive9=sumpositive8+num9;
  60.    else
  61.      sumnegative9=sumnegative8+num9;
  62.    if (num10 >= 0)
  63.      sumpositive10=sumpositive9+num10;
  64.    else
  65.      sumnegative10=sumnegative9+num10;
  66.  
  67.    cout<<"The sum of the positive numbers is: "<<sumpositive10<<".\n";
  68.  
  69.    cout<<"The sum of the negative numbers is: "<<sumnegative10<<".\n";
  70.  
  71.   }
  72.  
  73.   return 0;
  74. }
Sep 13 '07 #3
Ganon11
3,652 Expert 2GB
Have you learned about looping yet? This is, to be honest, unnecessarily long, and could be simplified greatly with a loop.
Sep 13 '07 #4
our teacher started talking about looping but not enough so i could use it for this.
Sep 13 '07 #5
sicarie
4,677 Expert Mod 4TB
our teacher started talking about looping but not enough so i could use it for this.
Well, I was correct in my assumption at the beginning, you have undefined variables.

for instance, run this program
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main( )
  5. {
  6.    int i_myInt;
  7.    cout << i_myInt;
  8.    return 0;
  9. }
i_myInt will return a value, even though you never assigned it one. This is because when you declare it, it is given memory space, but it is not initialized (given a value). Therefore it takes on the value of whatever is in the memory space that was just allocated to it.

So when you do

sumnegative=sumnegative+num1

you are saying "put in 'sumpositive' the sum of what is already in 'sumpositive' and 'num1' which is giving you undefined behavior (which manifests itself this time as abnormally large numbers).

I'm not sure if that's your only issue (you don't need the braces around the if-else chain, you can remove them with no effect), but that should get you going again.
Sep 13 '07 #6
im not quite sure what u meant.
Sep 13 '07 #7
sicarie
4,677 Expert Mod 4TB
im not quite sure what u meant.
Did you run the little example program that was posted?
Sep 13 '07 #8
the program didnt work when i ran it
Sep 13 '07 #9
sicarie
4,677 Expert Mod 4TB
the program didnt work when i ran it
Did you get an error?

You can do it with your program too. Just print out any of the variables you didn't set to anything. (there were two or three you actually did set to 0)
Sep 13 '07 #10
ilikepython
844 Expert 512MB
i figured id just post the whole code....

#include <iostream>
using namespace std;

int main()
{
double num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, sum, sumpositive=0, sumpositive1, sumpositive2, sumpositive3, sumpositive4, sumpositive5, sumpositive6, sumpositive7, sumpositive8, sumpositive9, sumpositive10, sumnegative=0, sumnegative1, sumnegative2, sumnegative3, sumnegative4, sumnegative5, sumnegative6, sumnegative7, sumnegative8, sumnegative9, sumnegative10, mean;
cout<< "Enter 10 Numbers:";
cin>> num1;
cin>> num2;
cin>> num3;
cin>> num4;
cin>> num5;
cin>> num6;
cin>> num7;
cin>> num8;
cin>> num9;
cin>> num10;
sum=num1+num2+num3+num4+num5+num6+num7+num8+num9+n um10;
cout<< "The sum of the numbers is: "<<sum<<".\n";
mean=sum/10.000;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
cout<<"The mean of the sum is: "<<mean<<".\n";
{
if (num1 >= 0)
sumpositive1=sumpositive+num1;
else
sumnegative1=sumnegative+num1;
if (num2 >= 0)
sumpositive2=sumpositive1+num2;
else
sumnegative2=sumnegative1+num2;
if (num3 >= 0)
sumpositive3=sumpositive2+num3;
else
sumnegative3=sumnegative2+num3;
if (num4 >= 0)
sumpositive4=sumpositive3+num4;
else
sumnegative4=sumnegative3+num4;
if (num5 >= 0)
sumpositive5=sumpositive4+num5;
else
sumnegative5=sumnegative4+num5;
if (num6 >= 0)
sumpositive6=sumpositive5+num6;
else
sumnegative6=sumnegative5+num6;
if (num7 >= 0)
sumpositive7=sumpositive6+num7;
else
sumnegative7=sumnegative6+num7;
if (num8 >= 0)
sumpositive8=sumpositive7+num8;
else
sumnegative8=sumnegative7+num8;
if (num9 >= 0)
sumpositive9=sumpositive8+num9;
else
sumnegative9=sumnegative8+num9;
if (num10 >= 0)
sumpositive10=sumpositive9+num10;
else
sumnegative10=sumnegative9+num10;

cout<<"The sum of the positive numbers is: "<<sumpositive10<<".\n";

cout<<"The sum of the negative numbers is: "<<sumnegative10<<".\n";

}

return 0;
}
Ok, anytime you declare a lot of variables with mostly the same name it can probably be done with an array:
Expand|Select|Wrap|Line Numbers
  1. double nums[10];
  2. int sumpositives[10];
  3. int sumnegatives[10];
  4.  
To initialize to 0:
Expand|Select|Wrap|Line Numbers
  1. double nums[10] = {0.0};
  2. int sumpositives[10] = {0};
  3. int sumnegatives[10] = {0};
  4.  
Also, like Ganon said, it will be much easier if you use loops.
An example:
Expand|Select|Wrap|Line Numbers
  1. cout << "Enter 10 numbers: ";
  2. for (int x = 0; x < 10; x++)
  3. {
  4.     cin >> nums[x];
  5. }
  6.  
Sep 13 '07 #11
Did you get an error?

You can do it with your program too. Just print out any of the variables you didn't set to anything. (there were two or three you actually did set to 0)

What other variables do i need to set to 0? I'm just not understanding anything about this.
Sep 13 '07 #12
my program is still coming out with really high numbers. What did you mean by print my other variables that aren't set to 0.
Sep 13 '07 #13
Ganon11
3,652 Expert 2GB
Really, you should set ALL of your variables to 0, just to make sure they don't have any garbage values in them. Basically, what's happening is this:

Your computer has memory. There can be a lot of random stuff in memory - something like "01010101001001000111000100110010010100100" is probably not uncommon in memory locations identified as de-allocated (that is, not in use).

Now, when your program initializes a variable (let's call it sum), that variable has to be stored in memory somewhere. So the computer looks for a place in memory that is de-allocated (that is, not in use) and says, "I declare this space allocated! This space will be used for sum!" For our sake, let's assume sum is an int, and this computer stores ints with 8 bits. Now, when the computer allocates this memory (that is, marks it as being used), it doesn't change the bits inside. So the memory could look like "0110 1110" (which equals 110 in decimal), even though you haven't set anything to sum yet. If you try and use sum before you initialize it to 0, you will get 110, which obviously isn't what you want.

This is all what happens when you type

Expand|Select|Wrap|Line Numbers
  1. int sum;
But, if you instead type

Expand|Select|Wrap|Line Numbers
  1. int sum = 0;
Now the computer allocatres memory AND sets it to 0, so that the memory looks like "0000 0000" (which is, of course, 0 in decimal). Now you have a value that you expect in sum.

This problem is happening multiple times in your own program, causing many values to be used that you had no control over. Also, in reality, computers use more than 8 bits to store an int (usually 16 to 32 bits), which means there is a possibility of a very large number being represented there before initialization. This is likely what is giving you your strange results.
Sep 13 '07 #14
that worked. But my negative number is coming out as 0 everytime i put a negative number in.
Sep 13 '07 #15
Try this, remove sumpositive1 - 10 and sumnegative1 - 10 and just use the single sumpositive and sumnegative that you initialized to 0, like this:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.      double num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, sum, sumpositive=0, sumnegative=0, mean;
  6.  
  7.      cout<< "Enter 10 Numbers:";
  8.      cin>> num1;
  9.      cin>> num2;
  10.      cin>> num3;
  11.      cin>> num4;
  12.      cin>> num5;
  13.      cin>> num6;
  14.      cin>> num7;
  15.      cin>> num8;
  16.      cin>> num9;
  17.      cin>> num10;
  18.      sum=num1+num2+num3+num4+num5+num6+num7+num8+num9+n  um10;
  19.      cout<< "The sum of the numbers is: "<<sum<<".\n";
  20.  
  21.      mean=sum/10.000;
  22.      cout.setf(ios::fixed);
  23.      cout.setf(ios::showpoint);
  24.      cout.precision(3);
  25.      cout<<"The mean of the sum is: "<<mean<<".\n";
  26.  
  27.      if (num1 >= 0)
  28.           sumpositive=sumpositive+num1;
  29.      else
  30.           sumnegative=sumnegative+num1;
  31.      if (num2 >= 0)
  32.           sumpositive=sumpositive+num2;
  33.      else
  34.           sumnegative=sumnegative+num2;
  35.      if (num3 >= 0)   
  36.           sumpositive=sumpositive+num3;
  37.      else     
  38.           sumnegative=sumnegative+num3;
  39.      if (num4 >= 0)
  40.           sumpositive=sumpositive+num4;
  41.      else
  42.           sumnegative=sumnegative+num4;
  43.      if (num5 >= 0)
  44.           sumpositive=sumpositive+num5;
  45.      else
  46.           sumnegative=sumnegative+num5;
  47.      if (num6 >= 0)
  48.           sumpositive=sumpositive+num6;
  49.      else
  50.           sumnegative=sumnegative+num6;
  51.      if (num7 >= 0)
  52.           sumpositive=sumpositive+num7;
  53.      else
  54.           sumnegative=sumnegative+num7;
  55.      if (num8 >= 0)
  56.           sumpositive=sumpositive+num8;
  57.      else
  58.           sumnegative=sumnegative+num8;
  59.      if (num9 >= 0)
  60.           sumpositive=sumpositive+num9;
  61.      else
  62.           sumnegative=sumnegative+num9;
  63.      if (num10 >= 0)
  64.           sumpositive=sumpositive+num10;
  65.      else
  66.           sumnegative=sumnegative+num10;
  67.  
  68.      cout<<"The sum of the positive numbers is: "<<sumpositive<<".\n";
  69.  
  70.      cout<<"The sum of the negative numbers is: "<<sumnegative<<".\n";
  71.  
  72.      return 0;
  73. }
If you want to add all of the positive numbers together and all the negatives together, then you only need 1 sum for each and just keep adding the next number to it.

The problem was that in each if/else statement you were using a variable that may or may not have been initialized yet. For example if the first number was positive and the second was negative then the first if/else statement would define sumpositive1 as equal to that number, but sumnegative1 would not be set equal to anything. So in the second if/else when you try to add sumnegative1 to the number and put them in sumnegative2 you have not yet initialized sumnegative1.
Sep 13 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ryan Lowe | last post by:
i thought id ask here before wirting a PEP, if people thought it would be a good enhancement to allow if clauses in regular for-statements like so: >>> for x in y if x < 10 : is semantically...
2
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an...
5
by: Angelina | last post by:
Hi, I need some help in writing a 'if-then-else' statement. I currently have wrote one that looks something like this.. If Combobox1 = xxx and textbox1 <> "" then 'run stored procedure 1 to...
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
73
by: Martin Johansen | last post by:
if(a); In this code, towhat type does the if statement cast the variable "a" to on comparizon?
19
by: wetherbean | last post by:
Hi group..I am writing a playlist management protocol where I have a file that holds all the playlists and a file that holds all the songs....before a playlist is created I need to check to see if...
2
by: William | last post by:
The script below runs correctly in ASP but not ASPX. Im not sure why? Please help. Description: An error occurred during the compilation of a resource required to service this request. Please...
8
by: fredda054 | last post by:
Hi everybody ! I have a little repeater/hyperlink issue I'm not sure how to solve. I use a repeater to list subjects available at a specific school. The datasource of this repeater is a...
0
by: Benny Ng | last post by:
Hi,All, When i deploy Enterprise library with my application ,i used XCOPY to deploy it into my test server. But when application runs, shown some error related registry. (But actually I haven't...
4
by: William | last post by:
Does anyone know what is causing the error below. The following scipt runs in .asp but not .aspx??? Please help. Description: An error occurred during the compilation of a resource required to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.