chars and numbers in C++

This is a discussion on chars and numbers in C++ within the C++ Programming forums, part of the General Programming Boards category; hello, this is my first few weeks of coding, and i was hoping you fine people could give me some ...

  1. #1
    Unregistered
    Guest

    chars and numbers in C++

    hello, this is my first few weeks of coding, and i was hoping you fine people could give me some help (no one else wants to).

    i have a simple program to find the length of a hypotenuse, now the problem is that when the user inputs a letter or word instead of a number, the program loops continuously. how do i get the program to ignore letters and words and take only integers?

    here is the source if it helps:

    int main()
    {
    int x, y;
    cout<<"Please give me the two legs of the triangle."<<endl;
    cout<<"First leg: "<<endl;
    cin>>x;
    cout<<endl<<"Second leg: "<<endl;
    cin>>y;
    cout<<endl<<"one moment please..."<<endl;
    cout<<"the hypotenuse length is "<<sqrt(x*x+y*y)<<endl<<endl;
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    firstly, get used to putting your code between [ code ] and [ /code ] tags (without the spaces)...

    secondly.. here's a hint

    Code:
    #include <iostream.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main()
    {
    	char buffer[12];
    	int x, y;
    	
    	cout << "Please give me the two legs of the triangle." << endl;
    
    	do
    	{
    		cout << "First leg: " << endl;
    		cin.getline(buffer, sizeof(buffer));
    		x = atoi(buffer);
    	} while(x == 0);
    
    	do
    	{
    		cout<<endl<<"Second leg: "<<endl;
    		cin.getline(buffer, sizeof(buffer));
    		y = atoi(buffer);
    	} while(y == 0);
    
    	cout << endl << "one moment please..." << endl;
    	cout << "the hypotenuse length is " << sqrt(x * x + y * y) << endl << endl;
    	
    	return(0);
    }
    the program is not complete.. but it's nearly there.. i didn't want to finish it for you because you wouldn't learn anything from it!

    good luck
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Unregistered
    Guest
    Thanks for the help, i appreciate it! now i'm gonna figure out how to truncate the string to the size of the buffer so the error doesn't repeat....don't tell me yet! i'll get it....maybe. thanks again!

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    Code:
    int num;
    
    if(cin >> num)
    {
            cout << "It's a number!\n";
    }
    else
    {
             cout << "It's not a number!\n";
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ - Reading Char Numbers.
    By kevinawad in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2008, 12:16 AM
  2. need help with pointers to a list of chars
    By Brimak86 in forum C Programming
    Replies: 11
    Last Post: 01-14-2008, 08:15 AM
  3. chars and pointers
    By FoodDude in forum C++ Programming
    Replies: 9
    Last Post: 09-27-2005, 09:39 AM
  4. Getting a 2 byte number from an array of char's
    By kzar in forum C Programming
    Replies: 20
    Last Post: 04-10-2005, 10:18 PM
  5. Re-reading chars from stdin
    By Uncle Albert in forum C Programming
    Replies: 5
    Last Post: 04-01-2005, 10:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21