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?
15 2134
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?
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+num10;
-
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;
-
}
Have you learned about looping yet? This is, to be honest, unnecessarily long, and could be simplified greatly with a loop.
our teacher started talking about looping but not enough so i could use it for this.
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 -
#include <iostream>
-
using namespace std;
-
-
int main( )
-
{
-
int i_myInt;
-
cout << i_myInt;
-
return 0;
-
}
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.
im not quite sure what u meant.
im not quite sure what u meant.
Did you run the little example program that was posted?
the program didnt work when i ran it
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)
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: -
double nums[10];
-
int sumpositives[10];
-
int sumnegatives[10];
-
To initialize to 0: -
double nums[10] = {0.0};
-
int sumpositives[10] = {0};
-
int sumnegatives[10] = {0};
-
Also, like Ganon said, it will be much easier if you use loops.
An example: -
cout << "Enter 10 numbers: ";
-
for (int x = 0; x < 10; x++)
-
{
-
cin >> nums[x];
-
}
-
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.
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.
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
But, if you instead type
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.
that worked. But my negative number is coming out as 0 everytime i put a negative number in.
Try this, remove sumpositive1 - 10 and sumnegative1 - 10 and just use the single sumpositive and sumnegative that you initialized to 0, like this: - #include <iostream>
-
using namespace std;
-
int main()
-
{
-
double num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, sum, sumpositive=0, sumnegative=0, 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)
-
sumpositive=sumpositive+num1;
-
else
-
sumnegative=sumnegative+num1;
-
if (num2 >= 0)
-
sumpositive=sumpositive+num2;
-
else
-
sumnegative=sumnegative+num2;
-
if (num3 >= 0)
-
sumpositive=sumpositive+num3;
-
else
-
sumnegative=sumnegative+num3;
-
if (num4 >= 0)
-
sumpositive=sumpositive+num4;
-
else
-
sumnegative=sumnegative+num4;
-
if (num5 >= 0)
-
sumpositive=sumpositive+num5;
-
else
-
sumnegative=sumnegative+num5;
-
if (num6 >= 0)
-
sumpositive=sumpositive+num6;
-
else
-
sumnegative=sumnegative+num6;
-
if (num7 >= 0)
-
sumpositive=sumpositive+num7;
-
else
-
sumnegative=sumnegative+num7;
-
if (num8 >= 0)
-
sumpositive=sumpositive+num8;
-
else
-
sumnegative=sumnegative+num8;
-
if (num9 >= 0)
-
sumpositive=sumpositive+num9;
-
else
-
sumnegative=sumnegative+num9;
-
if (num10 >= 0)
-
sumpositive=sumpositive+num10;
-
else
-
sumnegative=sumnegative+num10;
-
-
cout<<"The sum of the positive numbers is: "<<sumpositive<<".\n";
-
-
cout<<"The sum of the negative numbers is: "<<sumnegative<<".\n";
-
-
return 0;
-
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
by: Martin Johansen |
last post by:
if(a);
In this code, towhat type does the if statement cast the variable "a" to on
comparizon?
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
|
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...
| |