What's new

Need help with 'if' statement

joel_029

Lead Guitarist
Ok, here's the problem. I'm writing a program for school and I need to write a specific if statement. You have to pick a number between 1 and 100, and I need a statement for if the user enters a letter. I know that it's probably very simple, but for whatever reason it is eluding me. So any help would be appreciated.
 

vleespet

The decent one
I'd say use following piece of code (expand it yourself):

#include <iostream.h>

int main()
{
int a;
cin >> a;
if (a == 1)
cout << "statement 1";
if (a == 2)
cout << "statement 2";
return 0;
}
 
OP
J

joel_029

Lead Guitarist
sorry, forgot to mention that this is for C++. I need it to test an input to see whether it is a number or not.

something like

if(answer != int)

something to that effect, but 'int' doesn't work in this case. the code goes like

cout << "Please enter a number : ";
cin >> guess;

and the variable guess is an 'int' and i need the program to give a proper error message if the user inputs anything other than a number.
 

[vEX]

niechift.com
Assume x is your variable
Code:
if ((x >= 1) && (x <= 100)) { code goes here }
else { error handling code here }

That could would probably work in a zillion and one programming languages so I'm sure you could adapt it to the language you're using.
 
OP
J

joel_029

Lead Guitarist
already tried that earlier. but the problem is that when a character is entered, the program just completely loops the error message and just about everything that follows it for like eternity.
 

[vEX]

niechift.com
Seems like you posted a reply while I was writing mine. Since I don't know C or C++ for that matter I can't help you, someone else probably can. Good luck.
 

NeTo

Emu_64 HiP Coder
You could try:

Code:
#include <iostream.h>

int main()
{
	int guess;

	guess=-1;

	cin >> guess;
	if (guess != 0) cout << "yup, this is an int";
	else cout << "not an int :(";

	return 0;
}
 
Last edited:

Malcolm

Not a Moderator
Heres another example:
PHP:
#include <stdio.h>

int main(){
    char input[10];
    int numInput;

    printf("Enter something: ");
    scanf("%s", &input);
    
    numInput = atoi(input);
    
    if(numInput == 0){
        printf("You entered the string: %s\n", input);
    }else if(numInput >= 1 && numInput <= 100){
        printf("Your number was %d\n", numInput);
    }else{
        printf("Your entry was beyond scope :%d:\n",numInput);
    }
    
    system("pause");
}
 

NeTo

Emu_64 HiP Coder
Your example is better than mine :p. I think you need to include stdlib.h for that to work on c++
 

aprentice

Moderator
PHP:
#include "iostream.h"
#include "stdio.h"

int checkNumber(char *input)
{
     for(int i=0;i<strlen(input);i++)
     {
           if((input[i]<0x30) && (input[i]>0x39)) return 1;
     }
      return 0;
}

void main()
{
     char input[256];
     cin.getline(input,sizeof(input));
     
     if(checkNumber(input)) printf("Input is a string");
     else printf("Input is a number"); 
}

I havent tested this code, but it should work in theory.
 

Gigahurtz

Feel the speed
I remember doing something like this in highschool. To check if the input is a character (non-numerical) you must include in the code the header file <ctype.h>. Then, in this header file is a command called isalpha(input) and this function will either return a one which means the input is a alpha character or zero to indicate it's an integer.

something like
if (isalpha(input) == 0)
cout << "You have an integer"
else
cout << "This is an alphabet character"
 

aprentice

Moderator
Gigahurtz said:
I remember doing something like this in highschool. To check if the input is a character (non-numerical) you must include in the code the header file <ctype.h>. Then, in this header file is a command called isalpha(input) and this function will either return a one which means the input is a alpha character or zero to indicate it's an integer.

something like
if (isalpha(input) == 0)
cout << "You have an integer"
else
cout << "This is an alphabet character"

my checkNumber function does exactly this, but if isalpha exists, then its much easier to use instead :p
 
Yeah i like ren's function, a simple if on a function with a boolean result is the cleanest way to do things imo.
 

Top