I have an assignment I'm trying to finish. I'm new to C, and things aren't working as well as I'd hoped.
Our assignment is to write a small program to take input from a user through the command line, asking their name and then to take a guess at the magic number. We have a .txt document that has three names with associated numbers. Its three lines, on each line has a name, followed by a space, then the number. If the user's name doesn't equal one of those three, the magic number defaults to 12345.
After each guess, the program should say whether the number is too high or too low, and then quit (yeah, I don't know why either).
I've been debugging by printf statements, but can't figure out why it won't go into the comparison part of the code.
Any help or hints is greatly appreciated!
#include <stdio.h>
#include <string.h>
typedef struct {
char list_name[20];
int list_num;
}list;
char user_name[20];
int user_magic_num;
main(){
FILE *fp;
int i;
list answers;
//
fp = fopen("answers.txt", "rb");
if(fp == NULL)
{
perror("Error opening file");
return(-1);
}
printf("\n Hi, what's your name? \n \n");
scanf("%s",user_name);
printf("\n Hello, %s! \n \n What's the magic number? \n \n",user_name);
scanf("%d", &user_magic_num);
printf("You guessed %d.\n\n", user_magic_num);
for (i=0; i<=4;i++)
{
fgets(answers.list_name, 20, fp);
puts(answers.list_name);
if (strcmp(answers.list_name,user_name)==0)
{
printf("entered first if statement");
if(user_magic_num==answers.list_num)
{
printf("You guess correctly! %d is the magic number!",
answers.list_num);
break;
}
else if(user_magic_num>answers.list_num)
{
printf("Too high.");
}
else if(user_magic_num<answers.list_num)
{
printf("Too low");
}
}
else
{
user_magic_num = 12345;
}
}
return 0;
}
**************************************EDIT: #include #include
typedef struct list{
char list_name[20];
int list_num;
}list;
char user_name[20];
int user_magic_num;
main(){
FILE *fp;
int i;
list answers;
//
fp = fopen("answers.txt", "rb");
if(fp == NULL)
{
perror("Error opening file");
return(-1);
}
printf("\n Hi, what's your name? \n \n");
scanf("%s",user_name);
printf("\n Hello, %s! \n \n What's the magic number? \n \n",user_name);
scanf("%d", &user_magic_num);
printf("You guessed %d.\n\n", user_magic_num);
while (fscanf(fp, "%s %d",answers.list_name, &answers.list_num) != EOF)
{
//printf("%s\n", answers.list_name);
if(strcmp(user_name, answers.list_name)==0)
{
if (user_magic_num == answers.list_num)
{
printf("\nYes! %d is the magic number!\n", user_magic_num);
break;
}
else if (user_magic_num > answers.list_num)
{
printf("\nNope. Too high.\n");
break;
}
else
{
printf("\nNope. Too low.\n");
break;
}
}
else
{
answers.list_num = 12345;
printf("\nUser not recognized. Default magic number set.\n");
if (user_magic_num == answers.list_num)
{
printf("Yes! %d is the magic number!", user_magic_num);
break;
}
else if (user_magic_num > answers.list_num)
{
printf("\nNope. Too high.\n");
break;
}
else
{
printf("\nNope. Too low.\n");
break;
}
}
}
return 0; }
I just did this. It works now, but only for the first name in the txt file. The text file looks like this:
Bob 123
Mike 23
Rachel 345
But it only works if I type in Bob and then 123. But if I try Mike or Rachel, it treats it like on unknown and resets the magic number to 12345. I'm assuming this is something with fscanf that I'm doing wrong.