[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

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
0
yvonnesoo
Asked:
yvonnesoo
  • 6
  • 2
1 Solution
 
nietodCommented:
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
 
nietodCommented:
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
 
nietodCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
nietodCommented:
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
 
boodabellyCommented:
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
 
nietodCommented:
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
 
yvonnesooAuthor Commented:
Thank you. i get the idea.
0
 
boodabellyCommented:
sorry nietod I didnt really pay attention that it was for an assignment.  I will pay more attention next time.
0
 
nietodCommented:
Great, please do.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now