I have been working on a college project. I want user to enter Y/N if he wants to continue or not, so I wrote following code
repeat:
pr=0;
q=0;
res=0;
printf("\nEnter the serial number of product you wish to purchase: ");
scanf("%d",&pr);
printf("Quantity: ");
scanf("%d",&q);
billing(pr,q);
printf("Continue Shopping? (y/n) ");
scanf("%d",&res);
if(res=='y')
goto repeat;
else
return 0;
}
The problem is entering y executes else statement. I tried replacing y with 1,2,3.... and it works but I want it to work with Y or y or yes.
There is a bug in this line
scanf("%d",&res);
It should be
scanf(" %c",&res);
The formatting placeholders for char is %c not %d
scanf("%d",&res);
should be
scanf(" %c",&res);
As suggested by multiple users do while should be used over goto, so better code would be
do{
pr=0;
q=0;
printf("\nEnter the serial number of product you wish to purchase: ");
scanf("%d",&pr);
printf("Quantity: ");
scanf("%d",&q);
billing(pr,q);
printf("Continue Shopping? (y/n) ");
scanf(" %c",&res);
}
while(*res == 'Y' || *res == 'y');
return 0;
}
Also res should also be declared char res;
Related
help me. I've been assign to make an application for some test result. but why can't I print out what user have inputted at case 2. I've tried other approach but it's not working. this is the closest I could get there. Any help would be appreciated. super thanks +_+
this is my code :
int main(){
int option;
char namamurid[30][15];
int i=0,j=0;
int listening[15];
int reading[15];
int essay[15];
int score[15];
do{ printf("\"Smart English\" Course Center\n********************************\n");
printf("1.Add new data\n2.View data\n3.View summary\n4.Exit\n\n");
printf("your option[1..4]: ");
scanf("%d",&option);
fflush(stdin);
switch(option){
case 1:
do{
printf("Input student's name[1..25 char]: ");
scanf("%[^\n]s",namamurid[i]);
fflush(stdin);
}while(strlen(namamurid[i])<1 || strlen(namamurid[i])>25);
do{
printf("Correct answer for listening section[0..20]: ");
scanf("%d",&listening[i]);
fflush(stdin);
}while(listening[i]<0 || listening[i]>20);
do{
printf("Correct answer for reading section[0..30]: ");
scanf("%d",&reading[i]);
fflush(stdin);
}while(reading[i]<0 || reading[i]>30);
do{
printf("Correct answer for essay section[0..25]: ");
scanf("%d",&essay[i]);
fflush(stdin);
}while(essay[i]<0 || essay[i]>25);
break;
case 2:
printf("Name\t\tListening\tReading\tEssay\tScore\tGrade\n");
for(j=0;j<i;j++)
{
printf("%-1s\t\t%d\t%d\t%d\t%d\n",namamurid[j],listening[j],reading[j],essay[j],score[j]);
}
break;
}
} while(option<1 || option>4 || option !=4);
getchar();
return 0;
}
There is no change to value of variable i which is set to 0 during initialization.Hence it will never enter the for loop in case 2.
I'm new to C and I am trying to write a program that will let you enter up to 100 people's age and salary. The program at first will print some sentences to introduce (the display function) and then ask you whether you want to continue or not (yes_no function). After you enter a person's information, the program will also ask if you would like to continue to enter the next person's information or not. If you would like to continue, you will need to enter 1 and 0 for no/exit. When I compile and run the code, I found a problem and i could not figure out why!
The problem is that if you choose 0 (which means no/exit) after entering one's information, the program does not exit. It instead just asks the next person's information. But when I choose 0 right from the beginning, it just exits like usual. Why?
#include<stdio.h>
#define max 100
#define yes 1
#define no 0
int display(void);
int yes_no(void);
void get_data(void);
int date[max],month[max],year[max];
int cont;
int salary;
int main(){
cont=display();
if (cont==yes){
get_data();
}
return 0;
}
int display(void){
printf("This program will let you enter ");
printf("the age and salary of up to 100 people ");
cont=yes_no();
return cont;
}
int yes_no(void){
int i=0;
printf("\nDo you want to continue? Enter 1 for Yes and 0 for No\n");
scanf("%d", &i);
while(i<0 || i>1){
printf("Invalid value.Please enter again\n");
scanf("%d", &i);
}
if(i==1){
return (yes);
}else return (no);
}
void get_data(void){
int i=0;
for(i=0;i<max;i++){
printf("Enter information for people %d\n", i+1);
printf("Enter birthday\n");
do{
printf("Enter date\n");
scanf("%d", &date[i]);
}while( 0>date[i] || 31<date[i] );
do{
printf("Enter month\n");
scanf("%d", &month[i]);
}while( 0>month[i] || 12<month[i]);
do{
printf("Enter year\n");
scanf("%d", &year[i]);
}while( 1900>year[i] || 2016<year[i]);
printf("Enter salary\n");
scanf("%d", &salary);
cont=yes_no();
}
}
void get_data(void){
int i=0;
for(i=0;i<max;i++){
printf("Enter information for people %d\n", i+1);
printf("Enter birthday\n");
do{
printf("Enter date\n");
scanf("%d", &date[i]);
}while( 0>date[i] || 31<date[i] );
do{
printf("Enter month\n");
scanf("%d", &month[i]);
}while( 0>month[i] || 12<month[i]);
do{
printf("Enter year\n");
scanf("%d", &year[i]);
}while( 1900>year[i] || 2016<year[i]);
printf("Enter salary\n");
scanf("%d", &salary);
cont=yes_no(); // <== The Problem lies here
}
}
You ask the user if wants to continue but you never check the return value of yes_no()
Just add this after this line and it should work like a charm:
if (cont == no)
return;
As others have mentioned, there are still some things you can do to "improve" your code.
defines should be capitalized so #define YES 1 would stick to this convention.
And you shouldn't use global variables. These are bad programming style. Simply pass the things you need in other functions as a parameter and if you need the manipulated value later on past them as a pointer.
Also the formatting could be improved (however this is a mostly opinion based topic ;) )
In C you usually have an extra line for each curly bracket.
void get_data(void)
{
...
}
//instead of
void get_data(void){
...
}
Also the blanks after the do-while-loop should look more like so:
do
{
...
} while(1900 > year[i]); //here the curly bracket is ok that way
And operators should have a blank on both sides:
printf("Enter information for people %d\n", i + 1);
// instead of this
printf("Enter information for people %d\n", i+1);
This is all I've seen up to now.
pls help me how to make my program repeatable
char name[99];
printf("Please Input your Complete Name: ");
scanf(" %[^\n]",name);
printf("%s\n", name);
printf("enter y or Y to continue");
scanf("%c", &redo);
while((redo == 'y') || (redo == 'Y'));
is my code is correct ? correct me if im wrong
The while at the bottom is a loop with an empty body because you have a ; right after the condition instead of a statement or a bracketed group of statements.
For this you actually want do..while, which will run the loop at least once:
char redo;
char name[99];
do {
printf("Please Input your Complete Name: ");
scanf(" %[^\n]",name);
printf("%s\n", name);
printf("enter y or Y to continue");
scanf("%c", &redo);
} while((redo == 'y') || (redo == 'Y'));
This program is supposed to balance an account by asking for original amount then asking what sort of transaction they would like. The printf call is called twice when all I want is one. output: "Enter transaction: Enter transaction: ". Otherwise works fine. code:
#include <stdio.h>
int main(void)
{
float amount, old, new;
char letter;
printf("Please enter your current balance: ");
scanf("%f", &old);
printf("Enter the kind of transaction you would like: W - withdrawl, D - deposit, F - finished. \n");
scanf("%c", &letter);
while (letter != 'F'){
if (letter == 'D'){
printf("Amount: ");
scanf("%f", &amount);
new = old + amount;
old = new;
}
if (letter == 'W'){
printf("Amount: ");
scanf("%f", &amount);
new = old - amount;
old = new;
}
printf("Enter transaction: ");
scanf("%c", &letter);
}
if (letter == 'F')
printf("Your ending balance is %f.\n", old);
return 0;
}
Would greatly appreciate any insight! Thank you!!
scanf(" %c", &letter); instead of scanf("%c", &letter);
the original (without space) would interpret Enter as an input.
You may also be able to solve the issue by clearing the input (previous Enter press) from stdin input buffer. Add this line
fflush(stdin);
before
scanf("%c", &letter);
However, the solution provided by #artm is recommended, mine might not work depending on platform.
I just don't understand why this Yes/No loop will not work. Any suggestions? Given the input is "Y". I just want it to run the loop, then ask for Y or N again. If Y, print success, if N, print a good bye statement. What's the reason?
int main(){
char answer;
printf("\nWould you like to play? Enter Y or N: \n", answer);
scanf("%c", &answer);
printf("\n answer is %c");
while (answer == 'Y'){
printf("Success!");
printf("\nDo you want to play again? Y or N: \n");
scanf("%c", &answer);
}
printf("GoodBye!");
return 0;
}
Change the second scanf to:
scanf(" %c", &answer);
// ^
The problem is, when you enter Y and press ENTER, the new line is still in the input buffer, adding a space before %c could consume it.
fixed the various issues
#include <stdio.h>
int main(){
char answer;
printf("\nWould you like to play? Enter Y or N: \n");
scanf(" %c", &answer);
printf("\n answer is %c\n", answer);
while (answer == 'Y'){
printf("Success!");
printf("\nDo you want to play again? Y or N: \n");
scanf(" %c", &answer);
printf("\n answer is %c\n", answer);
}
printf("GoodBye!");
return 0;
}
You can reduce the repetition in your code a bit, and check the result of scanf() (as you should) by writing:
int main(void)
{
char answer;
printf("Would you like to play? Enter Y or N: ");
while (scanf(" %c", &answer) == 1 && answer == 'Y')
{
printf("Answer is %c\n", answer);
printf("Success!\n");
printf("Do you want to play again? Y or N: ");
}
printf("GoodBye!\n");
return 0;
}
The first printf() lost the unused argument answer; the second printf() collected the necessary second argument, answer. Except for prompts, it is generally best to end printing operations with a newline (rather than use a newline at the start). Prompts will generally be flushed by the C library before the input from stdin is read, so you don't need the newline at the end of those.
Since printf() returns the number of characters it prints, you can use it in conditions too:
int main(void)
{
char answer;
printf("Would you like to play? Enter Y or N: ");
while (scanf(" %c", &answer) == 1 &&
printf("Answer is %c\n", answer) > 0 &&
answer == 'Y')
{
printf("Success!\n");
printf("Do you want to play again? Y or N: ");
}
printf("GoodBye!\n");
return 0;
}
This always echoes the answer, even if the answer was not Y and the loop exits.