Help - Search - Members - Calendar
Full Version: Novice looking for help with an if statement.
CyberiaPC.com Community > Technical Zone > Programming (Java SE/EE, VB, C#)
Balethorn
Greets and thank you for your time,

I am writing a program for my C++ class and I am having trouble with an error-checking function. The idea of the function is to ensure the user enters a number greater than 0. My problem is checking to see if they entered a character/letter instead of an integer.

I tried using if (variable = char) {...} but I quickly discovered that is incorrect. The professor did not specify checking for letters, but I like to be thorough.

This class is a beginner class for C++.
usr.c
Hi, My first thought would be to convert it to ASCII and then check if it falls within a set range (alphabet chars). I don't really know how to do that.

Is there a is_string() or something similar in C++?
Aberdeen
CODE
while (scan != 1) {
            printf("Enter a number: ");
            scan = scanf("%i", &userInput);
            // invalid input
            if (scan != 1) reportError(1, userInput);
            flushInput();
}


This defines the user input as an integer(%i). If an integer is not found the reportError() is called. reportError would be your own function that could report back on different error codes.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.