checking input from user

homework problem, loop not array

Page 1 of 1

2 Replies - 1888 Views - Last Post: 24 October 2007 - 08:19 AM Rate Topic: -----

#1 nsivrts   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-October 07

checking input from user

Post icon  Posted 23 October 2007 - 05:57 PM

Hi to you expert programmers.

I am having trouble with this homework assignment. I have to ask user for five numbers, both positive and negative. Got that.
check user input and loop, 'm outputting from user input highest positive number, lowest positive number, highest negative number, and lowest negative number. got that.

where I'm stuck is if user inputs five negative numbers or five positive numbers, I'm supposed to output "none." Kind of like a flag, or boolean of some kind. I have the code for everything except the all negatives or all positives. here's my code so far
cout << "Enter five numbers, both positive and negative, no zeros, please." << endl;	 
		   for (count = 1; count <= 5; count++) 
		  {
				 
		   cin >> number;
		  //cin >> number2;
		  //cin >> number3;
		  //cin >> number4;
		  //cin >> number5; 
		  cin.ignore(INT_MAX, '\n');
		  //cout << number1 << "  " << number2 << " " << number3 << "  " << number4 << "  " << number5 << endl;
					
		  
		  if ((number > 0) && (number > highestpositive))
				highestpositive = number; 
		  if ((number > 0) && (number < lowestpositive))
				lowestpositive = number;
		  if ((number < 0) && (number > highestnegative))
				highestnegative = number;
		  if ((number < 0) && (number < lowestnegative))
				lowestnegative = number;		 
				//cout << lowestpositive << endl;
		  if (number = 0)
			   cout << "none." << endl;	 
			  
		  }
		  if ((
		  cout << highestpositive << endl;
		  cout << lowestpositive << endl;
		  cout << highestnegative << endl;
		  cout << lowestnegative << endl;							   
					 
	getchar();
	return 0;
}


I've declared variables, bunches of variables, because I'm trying to get out of declaring all five variables for number input and doing all those booleans to check for highest and lowest, would prefer to do INT_MAX and INT_MIN. so do I have to
write all that extra code? It works so well, except for the input of all positive or all negative numbers. will keep working on it.
thanks nancys

Is This A Good Question/Topic? 0
  • +

Replies To: checking input from user

#2 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: checking input from user

Posted 24 October 2007 - 02:31 AM

View Postnsivrts, on 24 Oct, 2007 - 06:27 AM, said:

Hi to you expert programmers.

I am having trouble with this homework assignment. I have to ask user for five numbers, both positive and negative. Got that.
check user input and loop, 'm outputting from user input highest positive number, lowest positive number, highest negative number, and lowest negative number. got that.

where I'm stuck is if user inputs five negative numbers or five positive numbers, I'm supposed to output "none." Kind of like a flag, or boolean of some kind. I have the code for everything except the all negatives or all positives. here's my code so far

cout << "Enter five numbers, both positive and negative, no zeros, please." << endl;
for (count = 1; count <= 5; count++)
{

cin >> number;
//cin >> number2;
//cin >> number3;
//cin >> number4;
//cin >> number5;
cin.ignore(INT_MAX, '\n');
//cout << number1 << " " << number2 << " " << number3 << " " << number4 << " " << number5 << endl;


if ((number > 0) && (number > highestpositive))
highestpositive = number;
if ((number > 0) && (number < lowestpositive))
lowestpositive = number;
if ((number < 0) && (number > highestnegative))
highestnegative = number;
if ((number < 0) && (number < lowestnegative))
lowestnegative = number;
//cout << lowestpositive << endl;
if (number = 0)
cout << "none." << endl;

}
if ((
cout << highestpositive << endl;
cout << lowestpositive << endl;
cout << highestnegative << endl;
cout << lowestnegative << endl;

getchar();
return 0;
}

I've declared variables, bunches of variables, because I'm trying to get out of declaring all five variables for number input and doing all those booleans to check for highest and lowest, would prefer to do INT_MAX and INT_MIN. so do I have to
write all that extra code? It works so well, except for the input of all positive or all negative numbers. will keep working on it.
thanks nancys



0 is neither positive nor negative. so initialize highestpositive, lowestpositive, highestnegative, lowestnegative to zero first

now if highest negative or highest positive is zero after coming out of loop then all numbers were either positives or negatives.

so condition will be :
if (highestpositive != 0 && highestnegative != 0)
{
		  cout << highestpositive << endl;
		  cout << lowestpositive << endl;
		  cout << highestnegative << endl;
		  cout << lowestnegative << endl;
}


Was This Post Helpful? 0
  • +
  • -

#3 nsivrts   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-October 07

Re: checking input from user

Posted 24 October 2007 - 08:19 AM

View PostAmitTheInfinity, on 24 Oct, 2007 - 02:31 AM, said:

View Postnsivrts, on 24 Oct, 2007 - 06:27 AM, said:

Hi to you expert programmers.

I am having trouble with this homework assignment. I have to ask user for five numbers, both positive and negative. Got that.
check user input and loop, 'm outputting from user input highest positive number, lowest positive number, highest negative number, and lowest negative number. got that.

where I'm stuck is if user inputs five negative numbers or five positive numbers, I'm supposed to output "none." Kind of like a flag, or boolean of some kind. I have the code for everything except the all negatives or all positives. here's my code so far

cout << "Enter five numbers, both positive and negative, no zeros, please." << endl;
for (count = 1; count <= 5; count++)
{

cin >> number;
//cin >> number2;
//cin >> number3;
//cin >> number4;
//cin >> number5;
cin.ignore(INT_MAX, '\n');
//cout << number1 << " " << number2 << " " << number3 << " " << number4 << " " << number5 << endl;


if ((number > 0) && (number > highestpositive))
highestpositive = number;
if ((number > 0) && (number < lowestpositive))
lowestpositive = number;
if ((number < 0) && (number > highestnegative))
highestnegative = number;
if ((number < 0) && (number < lowestnegative))
lowestnegative = number;
//cout << lowestpositive << endl;
if (number = 0)
cout << "none." << endl;

}
if ((
cout << highestpositive << endl;
cout << lowestpositive << endl;
cout << highestnegative << endl;
cout << lowestnegative << endl;

getchar();
return 0;
}

I've declared variables, bunches of variables, because I'm trying to get out of declaring all five variables for number input and doing all those booleans to check for highest and lowest, would prefer to do INT_MAX and INT_MIN. so do I have to
write all that extra code? It works so well, except for the input of all positive or all negative numbers. will keep working on it.
thanks nancys



0 is neither positive nor negative. so initialize highestpositive, lowestpositive, highestnegative, lowestnegative to zero first

now if highest negative or highest positive is zero after coming out of loop then all numbers were either positives or negatives.

so condition will be :
if (highestpositive != 0 && highestnegative != 0)
{
		  cout << highestpositive << endl;
		  cout << lowestpositive << endl;
		  cout << highestnegative << endl;
		  cout << lowestnegative << endl;
}




thank you thank you thank you thank you thank you :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1