# it's correct?

i just wanna know whether my answer is correctly done or not.
for your information, this is my question.
=====================================
ask a user to enter 5 negative and 5 positive value. find the total and display the two results. but no positive value could be enter during negative value input and no negative value could be enter during positive value input.
==============================
int i,j,x;
int total=0;
for (int x=1;x <=5; x++)
{
cout <<"enter number";
cin >>i;
while (i>=0)
{
cout <<"try again";
cin >>i;
}
while (j<=0)
{cout <<"try again";
cin >>j;
}
total+=i;
total+=j;
}
cout <<"the total is"<<total;
}
==================
please correct me if i done something wrong. thanks
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
That's good.  except that it requires you to enter alternating negative and positive numbers, i.e -1, 1, -2, 2, -3, 3 etc.   Is that what you want?  Or do you want all the negative numbers and then all the possitive numbers?  (like -1,2, -3, -4, -6, 1, 2, 3, .... )

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Now some small things.

You have two x's.  One is declared before the for loop and one is declared inside the for loop.  For example

nt i,j,x;   // First x
int total=0;
for (int x=1;x <=5; x++)  // second x
{

I would almost call this a bug, it does no harm in your current program, but in other programs, that have more going on, it could be a problem.  (Actually sometimes it might even be on purpose, but most likely it is by mistake and could cause undesired side effects.)

What happens is that outside the for loop (before it starts and after it ends), "x" refers to that first x.  but inside the for loop "x" refers to the 2nd x.  Thus if you have code that is supposed to work with 1 "x" througout the procedure, it will work incorrectly because the for loop section works with a different "x".  Does that make sense?

Quick fix is to just remove the "x" before the for loop.
0
Commented:
In your for loop

for (int x=1;x <=5; x++)

x will start at 1 on the first iteration and go to 5 on the last iteration.  That is fine, it really doesn't matter waht value it has, all you care about is that the loop executes 5 times, the value of x during an iteration isn't important, you could have x go from 101 to 105, like

for (int x = 101; x <= 5; x++)

it would still work correctly.  But it is customary in C++ that when you have for loops where the counter ("x") is not really being used, to have the counter start at 0, not 1.  So most programs would do

for (int x=0;x <=4; x++)

However, EITHER WAY WORKS, its just a common practice.
0
Commented:
Now you also don't have to have both i, and j.  You only use them for a "short while" to "temporarily" store a value.  So you could get by with just one or the other, like

int i
int total=0;

for (int x=1;x <=5; x++)
{
cout <<"enter number";
cin >>i;
while (i>=0)
{
cout <<"try again";
cin >>i;
}
total+=i;  // Has to be moved here!
while (i<=0)
{
cout <<"try again";
cin >>i;
}
total+=i;
}
0
Commented:
See if this helps with trying to enter any number not in a specific order.  I only tested it a few times, but you should get the general idea.

#include <iostream.h>

main ()
{
int tempNum    = 0,
negCounter = 0,
posCounter = 0,
totalSum   = 0;

while ( ( negCounter < 5 ) || ( posCounter < 5 ) )
{
cout << "Enter number:";
cin >> tempNum;

if ( tempNum < 0 )
{
if ( negCounter > 4 )
{
cout << "Too many negative nums" << '\n';
}
else
{
totalSum += tempNum;
++negCounter;
}
}
else
{
if ( posCounter > 4 )
{
cout << "Too many positive nums" << '\n';
}
else
{
totalSum += tempNum;
++posCounter;
}
}
}
cout << "Total sum: " << totalSum << '\n';
}

0
Commented:
This is for an assignment.  You don't want to GIVE him/her the answer.  That is unethical.  You can only provide limited help.
0
Author Commented:
Thank you. i get the idea.
0
Commented:
sorry nietod I didnt really pay attention that it was for an assignment.  I will pay more attention next time.
0
Commented: