Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c++, need help with conditions

Status
Not open for further replies.

stu74uk

Technical User
Jun 10, 2002
8
GB
Hi, I know this is supposed to be simple but I can't figure what variable(s) I would use if I was writing a program that prompts a user to enter a character, the program then displays a message if the user entered an integer or a letter. I know the program uses an if-else statement but I don't know what variable to use or what condition to use to test if the user entered an integer or not, help would be appriciated.
 
Use itoa(..) or atoi(..) or vice versa like this
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
char szTemp[10];
scanf(&quot;%s&quot;,&szTemp);
printf(&quot;\nYou eneterd - %s\n&quot;,szTemp);
if(atoi(szTemp)==0)
printf(&quot;which is not a number&quot;);
else
printf(&quot;which is a number&quot;);
return 0;
}
 
You can use function &quot;isalpha&quot; to test if it is a character between a~z or A~Z.
You can use function &quot;isdigit&quot;to test if it is a decimal-digit character between 0~9.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top