PDA

View Full Version : Need help with 'if' statement



joel_029
February 20th, 2004, 22:38
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
February 20th, 2004, 22:49
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;
}

The Khan Artist
February 21st, 2004, 00:07
I take it you want to test the variable to see if it is a numeral or not?

What programming language is this in?

joel_029
February 21st, 2004, 01:21
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]
February 21st, 2004, 01:24
Assume x is your variable

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.

joel_029
February 21st, 2004, 01:26
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]
February 21st, 2004, 01:32
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
February 21st, 2004, 02:37
You could try:



#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;
}

Malcolm
February 21st, 2004, 03:22
Heres another example:

#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
February 21st, 2004, 05:03
Your example is better than mine :p. I think you need to include stdlib.h for that to work on c++

Malcolm
February 21st, 2004, 06:05
Your example is better than mine :p. I think you need to include stdlib.h for that to work on c++
Works under Linux; minus the system pause stuff :)

NeTo
February 21st, 2004, 06:20
erhm, I meant: Needs stdlib to work on visual c++ :p

aprentice
February 21st, 2004, 06:31
#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
February 21st, 2004, 21:15
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
February 21st, 2004, 23:46
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

sytaylor
February 26th, 2004, 15:22
Yeah i like ren's function, a simple if on a function with a boolean result is the cleanest way to do things imo.