Hi need your help again ...
Code:
#include <stdio.h>
#include <string.h>
typedef struct
{
char phone[30];
char name[30];
}
Phonebook;
Phonebook people[2];
int showfile(void);
int main()
{
//open file & user input
FILE *file = fopen("phonebook.txt", "w");
for(int i = 0; i < 2; i++)
{
printf("Name [%d] ?: ", i+1);
scanf(" %[^\n]", people[i].name);
printf("Phone [%d] ?: ", i+1);
scanf("%s", people[i].phone);
fprintf(file, "Name: %s\t Phone: %s\n", people[i].name, people[i].phone);
}
fclose(file);
//ask user to read file
char decision;
printf("\nShow File ? (y/n): ");
scanf("%c", &decision);
if(decision == 'y')
showfile();
else if(decision == 'n')
printf("Quit\n");
return 0;
}
//// FUNCTIONS ////
///////////////////
int showfile(void)
{
FILE *fh_output = fopen("phonebook.txt", "r");
if(fh_output != NULL)
{
char c;
while((c = fgetc(fh_output)) != EOF)
putchar(c);
}
fclose(fh_output);
return 0;
}
The problem was that the code always stopped after
//ask user to read file
printf("\nShow File ? (y/n): ");
scanf("%c", &decision);
It didnt let me type in anything if I didnt have a backspace in front of "%c" in the scanf function but if I remove the code above I dont need a backspace, why is that the case ? Whats the deal with the backspace in front of it ? Someone explained it to me once but I still dont get it.
Thanks in advance.