[英]How to concatenate characters in an array in C
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.我想从键盘获取一个值(1、2 或 3 位数字),并想在控制台上打印出该值。
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输入:120
Output: The number you got is 120 Output:你得到的数字是120
Input: 12A输入:12A
Output: Error! Output:错误!
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";
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.