Poniżej znajduje się prosty kod, który powinien akceptować liczbę całkowitą wprowadzaną przez konsolę. Działa poprawnie, dopóki nie zostanie podane nieprawidłowe wejście (nie integer).Dziwny błąd C w prostym programie (praca domowa)
1 #include <stdio.h>
2
3 int main()
4 {
5 int x = 0;
6 int inp = 0;
7 int nums[20];
8 int idx = 0;
9 for(;;){
10 printf("Enter an integer: ");
11 inp=scanf("%d", &x);
12 if(inp == 0){
13 printf("Error: not an integer\n");
14 }
15 else{
16 if(idx<20){
17 nums[idx] = x;
18 idx++;
19 }
20 }
21 }
22 return 0;
23 }
Oto wyjście z gdb, który pokazuje mi intensywniejszej poprzez program po wprowadzeniu wartości „g”. Sprawdź, jak przeskakuje do linii 18, a następnie nie szuka dalszych informacji od użytkownika.
Starting program: /Users/jeffersonhudson/xxxx/hw1
Enter an integer: g
Breakpoint 1, main() at hw1.c:12
12 if(inp == 0){
(gdb) n
13 printf("Error: not an integer\n");
(gdb) n
Error: not an integer
0x0000000100000ee4 18 idx++;
(gdb) n
10 printf("Enter an integer: ");
(gdb) n
11 inp=scanf("%d", &x);
(gdb) n
Breakpoint 1, main() at hw1.c:12
12 if(inp == 0){
(gdb)
Jest to wyjście z programu po nadać mu złe wejście:
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
Enter an integer: Error: not an integer
^C
Czy ktoś mógłby mi pomóc dowiedzieć się, co robię źle tutaj?
sądząc po wyjściu i śladu, Nie sądzę, żebyś wykonywał opublikowany kod. – LoztInSpace
Zostawiłeś coś z przykładowego kodu, lub coś pomieszałeś w procesie odkażania - 'args' nie jest zdefiniowany. – duskwuff
Przeprosiny. Zmieniłem argumenty na inp –