2 Replies - 252 Views - Last Post: 03 October 2012 - 09:32 PM Rate Topic: -----

#1 ThinkFast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

I want to make the user use only numbers.

Posted 03 October 2012 - 08:41 PM

I made this code:
The user can find a side, a hypotenuse, or to exit.
If I press a letter instead of a number my program breaks and the infinite loop never stops.
How do I make it that the user can use only numbers?

This is for my own knowledge, not homework!!!
Thank you!!



#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	char in = 's'; 'x'; 'h'; // User choise.
	double s1, s2, h;
	

	do {
		cout << "Choose what to find: a side, a hypotenuse, or exit the program. [s/h/x]: "; // Prompt to user.
		cin >> in;

		switch (in){

			case 'x': //To exit.
				cout << "Thank you for using my program! \n";
				exit(0);
				break;



			case 'h': //To find hypotenous.
				cout << "To find the hypotenuse I want you to give me the two sides. \n";
				cout << "Give me Side1: ";
				cin >> s1;
				cout << "Give me Side2: ";
				cin >> s2;
			if (s1 <= 0 || s2 <= 0)
				{cout << "Negative numbers and zero are not allowed. \n" <<endl;
				break;}
			else
				h=sqrt((s1*s1)+(s2*s2));
				cout << "Your hypotenuse is: " << sqrt(h*h) <<endl <<endl;
				break;



			case 's': //To find side.
				cout << "To find the other side I want you to give me a side and your hypotenuse. \n";
				cout << "Give me your side: ";
				cin >> s1;
				cout << "Give me your hypotenus: ";
				cin >> h;
			if (s1 <= 0 || h <= 0) 
				{cout << "Negative numbers and zero are not allowed. \n" <<endl; 
				break;}
			else
				s2 = sqrt((h*h)-(s1*s1));
				cout << "Your other side is: " << sqrt(s2*s2) <<endl <<endl;
				break;
				
			}
		
	} while (1);
	
	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: I want to make the user use only numbers.

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: I want to make the user use only numbers.

Posted 03 October 2012 - 09:14 PM

What is happening is that your input stream is going into an error state so to fix this problem you will need to clear() the errors and manually remove any remaining characters from the stream. Normally this is accomplished by testing the stream after your entry to see that the input was valid and then clear() the errors and remove the garbage.

For example with cin:
if(!cin)
{ // If an error has occurred.
   cin.clear() // Clear the error.
   cin.ignore(200, '\n'); // See the note below.
   // now tell the user to enter the proper type of variable and re-enter the data.
}



Note: the 200 in the ignore tells the program to remove up 200 characters from the stream but you can stop if you encounter the new line character. You may want to see the documentation for ignore() for more information. You may want to use the numeric_limits<streamsize>::max() class to insure that you remove all the characters.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 ThinkFast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

Re: I want to make the user use only numbers.

Posted 03 October 2012 - 09:32 PM

Thank you Jim for your response. I am new to programming and i don't exactly get it. How would you write this on my code?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1