I ran the code through gdb couldn't make sense out of it, complained about line 13 though and kept showing random numbers
Originally Posted by
Salem
Seriously, compile with warnings and fix them before running code.
Code:
$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:63:15: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%d", input);
^
foo.c:26:10: warning: unused variable ‘newgame’ [-Wunused-variable]
char newgame;
^
foo.c:24:9: warning: unused variable ‘i’ [-Wunused-variable]
int i;
^
$
> if (input == num)
No, it's the completely borked scanf attempt which fails to pass a pointer which generates the actual fault.
And even if you get no warnings, you do this.
Run the code in the debugger, and it will tell you exactly the line where the problem originated.
Code:
$ gcc -g -Wall foo.c
$ gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) run
<<snipped>>
What sequence was inserted?: 333333
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a6cde5 in _IO_vfscanf_internal (s=<optimised out>, format=<optimised out>, argptr=argptr@entry=0x7fffffffdd58, errp=errp@entry=0x0) at vfscanf.c:1902
1902 vfscanf.c: No such file or directory.
(gdb) bt
#0 0x00007ffff7a6cde5 in _IO_vfscanf_internal (s=<optimised out>, format=<optimised out>, argptr=argptr@entry=0x7fffffffdd58, errp=errp@entry=0x0) at vfscanf.c:1902
#1 0x00007ffff7a785df in __isoc99_scanf (format=<optimised out>) at isoc99_scanf.c:37
#2 0x000000000040081d in main () at foo.c:63
(gdb) frame 2
#2 0x000000000040081d in main () at foo.c:63
63 scanf("%d", input);
Thanks Salem for the post. Look at this code (this program finds a succesful match for '10' inserted
Code:
#include <stdio.h>
int main()
{
int a = 10;
int b;
printf("Insert number: ");
scanf("%d", &b);
if(b == a)
{
printf("correct answer\n");
}
else
{
printf("Error, no match\n");
}
return 0;
}
Here's my code again that Salem commented on, with a few commented out additions. I'm not getting correct program output for if (input == num). Also, Salem, I reviewed your comment about running cc (gcc) with -Wall switch (I am on Debian Linux) and I believe it is now time to use gdb I have been using C + vim + cc for 2 years Thanks for finding the above typo, the missing '&' on the &input for scanf() function. Please, what am I missing in comparing my code to these two programs?
Code below, similar if statement functions, program not finding match (always returning "else" function)
Code:
/* Chapter 4 Exercise from C Programming book 2/14/2020 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
void delay (int seconds) // delay function i got this source code from google
{
unsigned long int count=333333333, i, j;
for(i = 0;i<seconds;i++)
for(j = 0; j < count; j++);
}
int main (void)
{
int program_execution = 0;
int game_loop = 0;
int limit = 9; // max digits to generate for random number
int count, num = 0;
char c = ' '; // fill console with blank space to clear screen
int input;
time_t t;
char newgame;
int successful_tries = 0;
srand((unsigned) time(&t));
for(;;)
//for ( ; game_loop > 0; --game_loop) // count down how many tries left until program ends
{
printf("\n");
for (count = 1; count <= 9; count++) // generate random number loop digit from 0 -> 9
{
num = rand() % limit;
printf("%d", num);
}
printf("\n");
delay(1); // create 1 second program delay before clear screen with 'X's
// clear screen with x's
for (int ctr_1 = 1; ctr_1 < 7500; ctr_1++)
{
printf("%c", c);
}
printf("\n\n");
printf("What sequence was inserted?: ");
scanf(" %d", &input);
if (input == num) // corrected answer was inserted, increase sequence length. Go to beginning of progrem (program_begin--), sequence_length++);
{
printf("Correct Answer!\n");
/*
while (input == num)
{
printf("Error! Answer was Incorrect!\n");
printf("Do you want a New Game? (Y or N)\n");
scanf("%c", &newgame);
if(tolower(newgame) == 'y') // look for any sign of no (not case sensitive)
{
break;
}
else
{
printf("Error!\n");
}
}
*/
}
else
{
//++successful_tries;
printf("Error, Not Correct!\n");
}
}
}
// The program will the calculate a score based on the number of successful tries and the time taken and invite the player to play again.
//printf("You had %d successful try. Program execution took %d seconds.\n", successful_tries, program_execution);