Thread: Need help with the if statement

  1. #1
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    Question Need help with the if statement

    I made this calculator program here the beginning of it.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
    
    	float FirstNumber;
    	char Sign;
    	float SecondNumber;
    	
    	cout << ">";
    	cin >> FirstNumber >> Sign >> SecondNumber;
    	
    	
    	
    	return(0);
    
    }
    I need to know if there a way to use the if statement so if FirstNumber or SecondNumber = a char then it will break or spit out a error message.....

    in my head it looks like this...... Ya it doesn't work because if it did I wouldn't be posting this...

    Code:
    if ( ( FirstNumber == (char) || SecondNumber == ( char ) ) {
    
    something....
    
    }
    Thanks....
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What you want to check is whether the input succeeded. std::cin will go into a fail state if it didn't, and you can test for it.

    Code:
    if (cin >> FirstNumber >> Sign >> SecondNumber) {
       //input should be fine
    }
    else {
        //input must have failed somewhere
    }
    Otherwise a float simply can't contain characters (e.g you can't input 'N' into a float).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    What you want to check is whether the input succeeded. std::cin will go into a fail state if it didn't, and you can test for it.

    Code:
    if (cin >> FirstNumber >> Sign >> SecondNumber) {
       //input should be fine
    }
    else {
        //input must have failed somewhere
    }
    Otherwise a float simply can't contain characters (e.g you can't input 'N' into a float).
    You could of course do
    Code:
    float f = 'A';
    which is identical to
    Code:
    float f = 65;
    Some compilers MAY warn for it, but I'm pretty sure you don't get an error for it, even in C++.

    However, reading in a float will fail, not set it to strange values.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    std::string Input;
    float f;
    std::getline(std::cin, Input);
    try
    {
        f = boost::lexical_cast<float>(Input);
    }
    catch(boost::bad_lexical_cast)
    {
        // Error; input not a float.
    }
    Rince and repeat. My favorite way.
    Requires boost, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM