Maybe there is no problem in running the first code
However,it will always default after I enter comment's data
char answer;
do{
printf("Do you want to add new comment?Y/N: ");
scanf("%c",&answer);
fflush(stdin);
switch(tolower(answer))
{
case 'y':
comment();
break;
case 'n':
main();
break;
default:
printf("Wrong choice !\n\n");
break;
}
}while(tolower(answer)!=='y'||'n');
there is the code of comment(),I guess the problem at here.
FILE*fp=fopen("comment.txt","a+");
if(fp == NULL)
{
printf("FIle not Found");
exit(1);
}
else
{
printf("Please enter your name: ");
gets(c.name);
printf("Pleas enter the date: ");
gets(c.date);
printf("Please enter the movie name: ");
gets(c.movie);
printf("Please enter your comment in 100 words:\n");
printf("Please press [Tab]and[Enter] to submit your comment\n");
scanf("%[^\t]",c.comment);
fprintf(fp,"%s %s\n%s\n%s\n\n",c.name,c.date,c.movie,c.comment);
}
fclose(fp);
Can someone help me?Thx!!
try using strlwr() instead of tolower()
Compiler gives you no error for while condition?
Maybe you want to write:
while(tolower(answer)!='y'||tolower(answer)!='n');
Inside do{}while you are reading input buffer which has a character you entered and a enter key. You need to clear that. Try scanf("%c%*c",&answer); .which will read that extra character from the buffer
A couple of problems in your code.
scanf("%c",&answer);
When you enter a character for the first time and if the character if either y or n then in next iteration this scanf() will read the stray \n (newline) character from the input buffer. To overcome this problem, add a space character before %, like this:
scanf(" %c",&answer);
Another problem is this statement:
while(tolower(answer)!=='y'||'n');
^^ ^
The compiler must be giving you both error and warning in this statement.
Change this to:
while(tolower(answer)=='y'||tolower(answer)=='n');
With this the loop to be run till user give input either y or n and for any other character, the loop will exit.
If you don't want loop to exit for any input character other then y or n but just print the message Wrong choice ! message then you can do:
}while(tolower(answer)!='y'||tolower(answer)!='n');
Related
i have put down below a small function to check which char the user type. the 'y' and 'n' works fine but i am a bit confused as to why my c code repeats twice before allowing me to type again when i input a different character which i put as my default statement.
int any_size_array () {
int a=19;
int numb_array [] = {a,22,13,132,153,600};
printf("do you want to add more numbers to the array?\t");
char user;
scanf("%c", &user);
switch(user) {
case 'y':
printf("user typed %c\n", user);
break;
case 'n':
printf("user typed %c\n", user);
break;
default:
printf("please try again\n");
any_size_array();
}
Because \n is a character, and %c does not consume it. If you want to skip whitespace, try scanf(" %c", &user).
Also, you must always check the value returned by scanf. If scanf does not modify the value of user, then your program is invoking undefined behavior by attempting to read from an uninitialized variable. Try:
if( scanf(" %c", &user) == 1 ) { ... }
Do not use recursion. Every time you call the any_size_array() function all variables are created again and are not related. Use loops for that.
Your input has more chars than only the character entered. (new line for example)
If the user would enter an invalid input, the program will ask the question again until the user enters a valid input. The code below performs it however, it executes the command twice:
void error_checking(){
char input;
printf("Enter letters from a to e");
scanf("%c", &input);
if((input<102) || (input>96)){
printf("Valid input");
} else {
error_checking():
}
int main(){
error_checking();
}
Use
scanf(" %c", &input);
^^^
instead of
scanf("%c", &input);
^^
Otherwise the function scanf will read the new line character '\n' that corresponds to the pressed key Enter.
Also you firgot the closing brace of the function.
And it is a bad idea to use magic numbers like
if((input<102) || (input>96)){
And at least you have ti write
if((input<102) && (input>96)){
I tried to calculate the tot(total fee) in the do-while loop, but all I get is tot=0.00?! Why is this happening? And after that I get a message: it said the variable fee is not being initialised?
#include<stdio.h>
int main(void)
{
int cofno;
float tot=0.0;
float fee;
char option;
do{
printf("Enter cofno: ");
scanf("%d",&cofno);
if(cofno>0)
{
printf("Key in (h/c): ");
scanf("%c",&option);
getchar();
switch(option)
{case 'h':fee=cofno*1.80;
break;
case 'c': fee=cofno*2.50;
break;
}
tot=tot+fee;
//the program will repeat until the user key in negative value or zero
}
}while(cofno>0);
printf("\ntot=RM%.2f \n\n",tot);
return 0;
}
scanf(" %c",&option); This will solve the problem for you. The reason the ' ' is provided in the scanf so that it can consume the white space characters.
What happened earlier was that your character input got the \n from previous input.
To check the thing that it inputted \n try outputting the option like this
printf("[%c]",option); you will see output
[
]
Also the break statement you provide is breaking for the case staement. Not the while loop. You have infinite loop now. You can solve this with a added condition.
...
tot=tot+fee;
if(option == 'c' || option =='h')
break;
...
Even more simply, you could have changed the while condition overall and make it like this
while(cofno<=0);
this conforms to your idea the program will repeat until the user key in negative value or zero more suitably.
In the following code when the user enters a space at address the program falls into a infinite loop?
example: street,town, city would crash the program, how can I replace the spaces with a ','? or at least stop this from happening
printf("\nEnter address:\n");
scanf("%s", newNode->address);
printf("\nEnter department:\n");
scanf("%d", &newNode->depart);
while(validDate == 0){
printf("\nEnter Data Of Join(dd/mm/yy):\n");
if (scanf("%d%*[-/. ]%d%*[-/. ]%d", &newNode->day, &newNode->mounth, &newNode->year) != 3){
printf("Wrong format! Please enter a date and exclude the slashes! eg. 15 01 95");
}
else{
// break the loop
validDate = 1;
}
}// date validation end
try
gets()
instead of scanf(). The scanf will stop reading the input when it encounters a space character. And the remaining string data will stay in the input buffer and that may causing the issue.
I'm trying to implement a simple while loop to let the user enter multiple marks without having to reload the application, for some reason no matter what i seem to enter it always loops.
I've looked using the debugger and it doesn't seem to accept the final scanf() asking whether to repeat itself or not.
int mark = 0;
char grade;
char choice = 'y';
while(choice == 'y')
{
//Request input
printf("enter a mark: ");
scanf("%d", &mark);
//Assess mark
grade = assess(mark);
//Output result
printf("That equals ");
printf("%c", grade);
printf(" when graded\n");
//Repeat?
printf("Again?...\n");
fflush(stdin);
scanf("&c", &choice);
}
I've also tried it with a do - while loop and still no joy, any idea where the problem may lie?
At least two problems:
fflush(stdin);
is undefined - you can only flush output streams. And:
scanf("&c", &choice);
should be:
scanf("%c", &choice);
I think last line should be
scanf("%c", &choice);
instead of
scanf("&c", &choice);
fflush() is only defined for output streams. The comp.lang.c FAQ does not recommend using it for stdin.
Also, as others have noted, use scanf("%c", &choice); to read the choice.
Try scanf("%c", &choice);.
Note that scanf returns the number of inputs matched, so you should really check the return value. If the input does not, for som reason, map to a character, your variable might be unchanged. Before the call to scanf, set choice to something != 'y', so that you only continue if a y is input.