I want to get a value (1, 2, or 3 digits of number) from a Keypad, and want to print out the value on the console.
I have,
char val[i] // value from a Keypad from 1 through 120
// so, val[i] could be one, two or three digit number.
What I want to do is,
if (val[i] == 1)
printf("The number you got is %d", val[i]); // prints "The number you got is 1"
else if ((val[i] == 2)
printf("The number you got is %d", val[i]); // prints "The number you got is 2"
.
.
else if ((val[i] == 10)
printf("The number you got is %d", val[i]); // prints"The number you got is 10"
.
.
else if ((val[i] == 120)
printf("The number you got is %d", val[i]); // prints"The number you got is 108"
else printf("Error!"); // prints "Error!"
Please help me. Thank you, in advance, for your help.
It should work fine
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int isNumber(char* s)
{
for (int i = 0; i < strlen(s); i++)
if (isdigit(s[i]) == 0)
return 0;
return 1;
}
int main()
{
char * a = (char*) malloc(4);
scanf("%s",a);
int i = atoi(a);
if(isNumber(a) && i>= 1 && i <= 120)
{
printf("The number you got is %d\n", i);
} else printf("Error!");
}
In my machine:
Input: 120
Output: The number you got is 120
Input: 12A
Output: Error!
You may try below code
#include<iostream>
#include<string.h>
using namespace std;
int main(){
int num;
int state;
int a,b,data[num];
string element[2];
cout<<"Enter amount data: ";
cin>>num;
for(a=0;a<num;a++){
cout<<"Enter state: ";
cin>>state;
if(state==3){
element[0]="A";
} else if(state==10){
element[1]="B";
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.