新人求助
想把用户输入的字符串转化为整形,并且以-为分割符,但是为什么第二个数字后面会多一个0??求解#include<stdio.h>int main()
{
char ch;
char c[] = "",d[] = "";
int i,s1,s2;
printf("请输入数字:");
for(i = 0;(ch = getchar()) != '-';i++){
c = ch;
}
s1 = atoi(c);
for(i = 0;(ch = getchar()) != '\n';i++){
d = ch;
}
s2 = atoi(d);
printf("%d-%d",s1,s2);
} 这样即可:
#include<stdio.h>
#include <stdlib.h>
int main()
{
char ch;
char c[] = "",d[] = "";
int i,s1,s2;
printf("请输入数字:");
for(i = 0;(ch = getchar()) != '-';i++){
c = ch;
}
c = '\0';
s1 = atoi(c);
for(i = 0;(ch = getchar()) != '\n';i++){
d = ch;
}
d = '\0';
s2 = atoi(d);
printf("%d-%d",s1,s2);
}
页:
[1]