使用scanf读入n,但是程序卡在下面怎么办

P1957 口算练习题

AnthonyEric @ 2022-04-08 22:47:15

#include<bits/stdc++.h>
using namespace std;
string g;
char s[1000000];
char ans[100000];
int main(){
    int n,a,b,c,k=0;
    char las;
    scanf("%d",n);
    while(n--){
        fgets(s, sizeof(s), stdin);
        if(s[0] == 'a' || s[0] == 'b' || s[0] == 'c')
            las = s[0], s[0] = ' ';
         sscanf(s, "%d %d", &a, &b);
         switch(las){
            case'a': c=a+b; sprintf(ans, "%d+%d=%d", a, b, c); break;
            case'b': c=a-b; sprintf(ans, "%d-%d=%d", a, b, c); break;
            case'c': c=a*b; sprintf(ans, "%d*%d=%d", a, b, c); break;
         }
         printf("%s\n%d\n", ans, strlen(ans));

    }
    return 0;
}

是不是有什么奇怪的特性,还是我哪里错了,希望有高人指教


by __qlzxlyc41__ @ 2022-04-08 22:53:08

scanf("%d",n);

scanf("%d",&n);

才对,大哥


by __qlzxlyc41__ @ 2022-04-08 22:53:17

@AnthonyEric


by 王梓涵 @ 2022-04-08 22:53:18

@AnthonyEric 你 n 没加取值符 &n

scanf("%d", n);

改为

scanf("%d", &n);

by AnthonyEric @ 2022-04-08 22:54:58

@王梓涵 但是为啥输入样例的时候下面会换行然后输出一个0


by AnthonyEric @ 2022-04-08 22:55:18

@qlzxlyc41 但是为啥输入样例的时候下面会换行然后输出一个0


|