Do while loop C coding error - c

What is going wrong here? I am getting the error Use of undeclared identifier 'answer'
Here's my code:
if (CalculatorChoice == 1) do {
int a;
int b;
int sum;
char answer;
printf ("You have choosen addition, please enter first number: ");
scanf("%d", &a);
printf ("Now please enter second number to addit: ");
scanf("%d", &b);
printf("The sum is: %d \n\n", sum = a+b);
printf("Do you want to go back to menupage? (y/n): ");
answer = getchar();
getchar();
} while(answer=='y');

if (CalculatorChoice == 1)
do {
/* ... */
char answer;
/* ... */
} while(answer=='y');
You have declared the variable answer inside the loop block, but it is accessed from the while condition, which is outside the loop block.
You can move the declaration outside the loop body to fix this:
if (CalculatorChoice == 1) {
char answer;
do {
/* ... */
} while(answer=='y');
}

Your version has variable declared inside block, is not accessible out of block
This code changes position
if (CalculatorChoice == 1)
{
char answer;
do {
int a;
int b;
int sum;
printf ("You have choosen addition, please enter first number: ");
scanf("%d", &a);
printf ("Now please enter second number to addit: ");
scanf("%d", &b);
printf("The sum is: %d \n\n", sum = a+b);
printf("Do you want to go back to menupage? (y/n): ");
answer = getchar();
getchar();
} while(answer=='y');
}

Related

How to insert a continue statement inside an if statement

My code is below. I am using C language. I want to repeat the action from the start if the user types Y but I am confused how can I make that happen.
I tried to look for a solution but the results doesn't fit for my program.
#include <stdio.h>
int main() {
int A, B;
char Y, N, C;
printf ("Enter value 1: ");
scanf ("%i", &B);
printf ("\nEnter value 2: ");
scanf ("%i", &A);
printf ("= %i", A + B);
printf ("\n\nAdd again? Y or N\n");
scanf ("%c", &C);
if (C == Y) {
//This should contain the code that will repeat the:
printf ("Enter value 1: ");
scanf ("%i", &B);
printf ("\nEnter value 2:
} else if (C == N)
printf ("PROGRAM USE ENDED.");
else
printf ("Error.");
}
You should just wrap your code inside a for loop:
#include <stdio.h>
int main() {
int A, B;
char Y = 'Y', N = 'N', C;
for (;;) { // same as while(1)
printf("Enter value 1: ");
if (scanf("%i", &B) != 1)
break;
printf("\nEnter value 2: ");
if (scanf("%i", &A) != 1)
break;
printf("%i + %i = %i\n", A, B, A + B);
printf("\n\nAdd again? Y or N\n");
// note the initial space to skip the pending newline and other whitespace
if (scanf(" %c", &C) != 1 || C != Y)
break;
}
printf("PROGRAM USE ENDED.\n");
return 0;
}
There are a lot of errors in your program.
Syntax error: please resolve it on your own.
There is no need for Y and N to be declared as character, you can use them directly as they are not storing any value.
NOW, there is no need for continue you can use while loop.
I have resolved your problem. Please take a look
Also, you are using a lot of scanf so there is an input buffer, a simple solution to it is using getchar() which consumes the enter key spaces.
#include <stdio.h>
int main()
{
int A, B;
char C = 'Y';
while (C == 'Y')
{
printf("Enter value 1: ");
scanf("%i", &B);
printf("\nEnter value 2");
scanf("%i", &A);
printf("= %i\n", A + B);
getchar();
printf("\n\nAdd again? Y or N\n");
scanf("%c", &C);
}
if (C == 'N')
{
printf("PROGRAM USE ENDED.");
}
else
{
printf("Error.");
}
}

Can someone explain me whats wrong with finding total part in this code

Whenever I run this I got something like ' 196875307' as the total, could
someone tell me whats wrong with it.Here I uploaded the whole code.It says my post is mostly code and to add more details,So forgive me for typing these unnecessory things XD
#include <stdio.h>
int room;
char name[20];
int i;
void main()
{
int answr,fc[6],z=0,tot;
char ans;
char food[8][30]={"Bread","Noodles","Salad","Popcorn","Chocolate ice
cream","Vanilla ice cream","Cold Coffee","Milk Shake"};
int price[8]={180,120,65,55,70,70,110,200};
printf("\n *********");
printf("\n MENU CARD");
printf("\n *********\n\n\n\n");
printf("\n Food Code\t\tprice\t\t Food Name\n");
for(i=0;i<8;i++)
{
printf("\n\t\t%d",i+1);
printf("\t\t%d",price[i]);
printf("\t\t%s",food[i]);
}
printf("\n\n\n\t *PRESS 0 TO GO TO THE MAIN MENU\n\t *PRESS 1 TO ORDER
FOOD");
scanf(" %d",&answr);
switch(answr)
{
case 0:
{
printf("Enter the main menu function here");
break;
}
case 1:do
{
printf("ENTER THE FOOD CODE YOU WANT TO HAVE :: ");
scanf(" %d",&fc[z]);
z++;
tot=tot+fc[z];
printf("total so far is %d",tot);
printf("DO YOU WANT MORE(Y/N) ::");
scanf(" %c",&ans);
}while((ans=='y')||(ans=='Y'));
printf("\nEnter your room number:");
scanf(" %d",&room);
printf("\nEnter your name:");
scanf(" %s",&name);
}
}
The issue lies in your do loop. You need to initialize tot to 0, and use the user-inputted "food code" as an array index into your price array. I don't see any use for the "fc" array you've declared. This code should work for case 1 in your switch statement.
Remember that the main function returns an int in C.
do {
tot = 0;
printf("ENTER THE FOOD CODE YOU WANT TO HAVE :: ");
scanf("%d", &z);
if (z < 1 || z > 8) {
printf("Invalid food code\n");
return -1; // main should return int in a C program
}
tot=tot+price[z-1];
printf("total so far is %d\n",tot);
printf("DO YOU WANT MORE(Y/N) ::");
scanf(" %c",&ans);
} while((ans=='y')||(ans=='Y'));
I think that you should increment your counter z after you do the addition of the total with your freshly added element into the array.
1)Get the value by scanf
2)add it by using vet[z]
3)increment z.
When your code hit the sum it will try to access to a segment of memory that is filled with some other values.

C: With one variable entered wrong, my "incorrect input" if kicks in and doesn't let the user to finish typing in rest of variables

The problem is, basically, that if user enters a:5, b:f, everything works fine. But if it's the other way around and enters a letter to the 'a' variable, the program ends saying "Incorrect input", not letting the user to finish typing in rest of the variables. Why? Is it because of how I dealt with checking if the input is correct in the first place? How to "delay" the message and make it show after user finishes entering variables?
Here's the code:
#include <stdio.h>
int main(void) {
short int l1=0, l2=0, l=0;
int a=0, b=0;
printf("Is number 'a' divisible by number 'b'?\n");
printf("Number a: ");
l1 = scanf("%d", &a);
printf("Number b: ");
l2 = scanf("%d", &b);
l=l1+l2;
if (l<2)
{
printf("Incorrect input");
return 1;
}
else if (b==0)
{
printf("Operation not permitted");
return 1;
}
else if (a%b)
{
printf("%d is not divisible by %d", a, b);
}
else printf("%d is divisible by %d", a, b);
return 0;
}
As Weather Vane already pointed out, the reason the program exits is, that when you enter a character (%c) and the scanf function is waiting for a integer (%d) it ignores the char, doesn't find an int but ends its' search on the '\n' (enter), so your variable l1 stays 0. This happens for all of your scanf calls, as it doesn't clear the buffer from characters that don't match.
Solving this
You can clear the input buffer, so that all the other scanf calls can get an actual input, though, you are still going to get an "Incorrect input" at the end.
printf("Number a: ");
l1 = scanf("%d", &a);
while (getchar() != '\n');
printf("Number b: ");
l2 = scanf("%d", &b);
while (getchar() != '\n');
If you want to repeat the input process until a user enters the numbers correctly, you have to check the return value of the scanf in a while loop, something like this:
do {
printf("Number a: ");
l1 = scanf("%d", &a);
while (getchar() != '\n');
} while (l1 != 1 || l1 != EOF);
Try this:
#include <stdio.h>
int main(void) {
short int l1=0, l2=0, l=0;
int a=0, b=0;
printf("Is number 'a' divisible by number 'b'?\n");
printf("Number a: ");
l1 = scanf("%d", &a);
getchar();
printf("Number b: ");
l2 = scanf("%d", &b);
l=l1+l2;
if (l<2)
{
printf("Incorrect input");
return 1;
}
else if (b==0)
{
printf("Operation not permitted");
return 1;
}
else if (a%b)
{
printf("%d is not divisible by %d", a, b);
}
else printf("%d is divisible by %d", a, b);
return 0;
}
Reference : scanf() leaves the new line char in the buffer

Creating a gradebook program in C

I'm working on a program that will keep track of grades entered. The program should ask if the user is finished inputting grades. If the the user is not finished, the user will continue to enter grades. If the user is finished, program will print the grades. Every time I run the program, it crashes.
Here's what I got.
#include <stdio.h>
int main (){
int i = 0;
float gradeBook;
char ans, y, Y, n, N;
printf("Please enter grade %d: ", i + 1);
scanf("%.2f", &gradeBook);
printf("\nDo you have more grades to enter? [Y or N] ");
scanf("%c", ans);
while(ans == 'y' || ans == 'Y'){
printf("Please enter the next grade %d: ", i + 1);
scanf("%i", &gradeBook);
printf("\nDo you have more grades to enter? ");
scanf("%c", ans);
}
printf("You have entered %d grades", i);
printf("The grades you have entered are %.2f ", gradeBook);
return 0;
}
You should use arrays for problems like this. Here is what I did:
#include <stdio.h>
int main (){
int i = 0, j;
float gradeBook[20];
char ans;
printf("Please enter grade %d: ", i + 1);
scanf("%f", &gradeBook[0]);
printf("\nDo you have more grades to enter? [Y or N] \n");
scanf(" %c", &ans);
while (ans == 'y' || ans == 'Y') {
printf("Please enter the next grade: \n");
i += 1;
scanf("%f", &gradeBook[i]);
printf("\nDo you have more grades to enter? \n");
scanf(" %c", &ans);
}
printf("You have entered %d grades\n", i+1);
printf("The grades you have entered are: \n");
for (j=0; j<=i; j++)
printf("%.2f ", gradeBook[j]);
printf("\n\n");
return 0;
}
Your program crashes because you are missing the & in ans in your scanf. Without the &, the scanf treats the value in "ans" as an address, and will try to access it, thus seg fault.
PS: In each iteration you are overwriting the value in "gradeBook", if you want to print a set of values, you should use an array.

scanf_s not working what is the error? can any one solve this

#include<stdio.h>
int main(void)
{
int sum(), sub(), mul(), div();
char ans;
printf("\ta) Add\n");
printf("\tb) Sub\n");
printf("\tc) Multiply\n");
printf("\td) Divition\n");
scanf_s("%c",&ans);
printf("\nYour answer is %c",ans);
if((ans == 'A')||(ans == 'a'))
printf("The sum of Two number is %d\n",sum());
else if((ans == 'B')||(ans == 'b'))
printf("The subraction of Two number is %d\n",sub());
else if((ans == 'C')||(ans == 'c'))
printf("The product of two number is %d\n",mul());
else if((ans == 'D')||(ans == 'd'))
printf("The divition of two number is %d\n",div());
else
printf("Enter the valid option\n");
}
int sum()
{
int x,y;
printf(" Addition\n");
printf("Enter the first number = ");
scanf_s("%d",&x);
printf("Enter the second number = ");
scanf_s("%d",&y);
return x+y;
}
int sub()
{
int x,y;
printf(" Subraction\n");
printf("Enter the first number = ");
scanf_s("%d",&x);
printf("Enter the second number = ");
scanf_s("%d",&y);
return x-y;
}
int mul()
{
int x,y;
printf(" Mltification\n");
printf("Enter the first number = ");
scanf_s("%d",&x);
printf("Enter the second number = ");
scanf_s("%d",&y);
return x*y;
}
int div()
{
int x,y;
printf(" Divition\n");
printf("Enter the first number = ");
scanf_s("%d",&x);
printf("ENter the second number = ");
scanf_s("%d",&y);
return x/y;
}
scanf_s("%c",&ans);
the function scanf does not recognize my input
whats wrong in the code
when i use
ans=getchar();
is working perfect but.. the scanf does not recognize what i input
can any one explain the problem in the code iam using visual studio 2010
To scan a single char you have to put a blank space before %c:
scanf(" %c", &char);
The blank space is needed to consume any \n char in the input buffer. You must put the blank space just for char type.

Resources