3 Replies - 986 Views - Last Post: 13 October 2012 - 11:54 AM Rate Topic: -----

#1 dcom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-October 12

while loop I am tryin to make this so that it will continue inputing

Posted 13 October 2012 - 04:30 AM

I am tryin to make this so that it will continue inputing numbers by the user, and adding them together untill a negative number is inputed..it works partially but i dont know what else i need to do to make it work?




[
# include <iostream>

using namespace std;

int main()
{
	int num3;
	int num1;
	int num2;
	

	
	
	cout <<"  Enter a positive number and i will add it to your last number.." << endl << endl;
	cout << "Enter a number " << endl << endl;
	cin >> num1,num2; 
	cout << " Enter another number " << endl << endl;
	cin >> num2;

	while (num1 || num2 < 0 )
	{
		cout << "You entered a negative number..Goodbye!!" << endl;
		return 0;
	}
	cout << " the sum of your numbers is " << num1 + num2 << endl << endl;
	
	cout << "enter one more number "<< endl << endl;
	cin num3;
	cout << " your new total is " num3 + num2 + num3 << endl;
	
	} 
	

	while (num1 || num2 < 0 )
	{
		cout << "You entered a negative number..Goodbye!!" << endl;
		return 0;
	}
	
}
]

This post has been edited by jimblumberg: 13 October 2012 - 05:32 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: while loop I am tryin to make this so that it will continue inputing

#2 peace_fixation  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 179
  • Joined: 01-November 11

Re: while loop I am tryin to make this so that it will continue inputing

Posted 13 October 2012 - 05:28 AM

Your while loops do not make sense. Think about what part of the algorithm you are trying to repeat. Your current code says "while either of the numbers is negative, exit the program", return 0 means you are finished.

What you want to say is "while a number is negative, input the number again" this way, when a positive number is entered, your while loop will stop and you can perform the sum.

Something like this, you'll need to play around with the code to make it work for your particular problem:

cout << "Enter first number"
cin num1;

num2 = -1; // initialize this number to a negative number

while (num2 < 0)
{
  cout << "Enter second number: ";
  cin num2;
}

// if the loop stops, num2 is positive, sum the two numbers
sum = num1 + num2;
cout << "The sum of num1 and num2 is ..";

Was This Post Helpful? 1
  • +
  • -

#3 Mrk  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 66
  • Joined: 03-December 09

Re: while loop I am tryin to make this so that it will continue inputing

Posted 13 October 2012 - 06:49 AM

You are using while loops for decision making when you should be using an if statement. While loops - or any kind of loop - is used when you need to repeat the same statements over and over. When a decision needs to be made, like if the program needs to end or continue on, an if statement is used.

 while (num1 || num2 < 0 )
	    {
	     cout << "You entered a negative number..Goodbye!!" << endl;
	     return 0;
	    }

//use this if statement instead:

if( num1 || num2 < 0 )
{
   cout << "You entered a negative number..Goodbye!!" << endl;
   return 0;
}


But wait - there is one mistake with the condition
statement ( num1 || num2 < 0 ). There is a certain order in which things get evaluated (precedence) and assuming num1 and num2 are both positive, then num2 < 0 gets evaluated first, which would be false (0) then num1 || 0 would be evaluated next and would be non zero or true. With this setup, the only time your while loop would NOT execute would be when num1 was 0 or less and num2 was 0 or more. You should have written it this way: (num1 < 0 || num2 < 0). Or, to make the precedence clearer, you could have written
( (num1 < 0 ) || (num2 < 0 ) ).

So remember, use loops to repeat a statement or block of statements over and over, and use if statements to direct the flow of your program (i.e. decision making).

This post has been edited by Mrk: 13 October 2012 - 06:54 AM

Was This Post Helpful? 0
  • +
  • -

#4 dcom  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-October 12

Re: while loop I am tryin to make this so that it will continue inputing

Posted 13 October 2012 - 11:54 AM

wow thanks for the help..you also explained things nicely heiping me to understand thid better..thanks again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1