Creating a gradebook program in C - 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.

Related

Do while loop C coding error

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');
}

Why scanf after the if block do not work it just displays "press 1 to enter again:" and do not take input again

scanf statement after if block is not working can somebody help me please
#include<stdio.h>
main()
{
int input,itemno,input2;
int name1,price1,name2,price2,name3,price3;
printf("enter input:\n");
scanf("%d", &input);
if(input==1)
{
printf("enter number of items:\n");
scanf("%d",&itemno);
if(itemno<=3)
{
printf("enter name and age:\n");
scanf("%d %d\n %d %d\n %d %d\n",&name1,&price1,&name2,&price2,&name3,&price3);
}
else
printf("you can only enter 3 students");
}
printf("press 1 to enter again: \n");
scanf("%d",&input2);
if(input2==1)
{
printf("hey");
}
}
scanf("%d %d\n %d %d\n %d %d\n",&name1,&price1,&name2,&price2,&name3,&price3);
Don't use escape characters in scanf function call.
just delete the last \n in your scanf
the result is :
result

if statement with char condition in C

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;

In C: 2 printf output when all I want is one.

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.

Variables retaining value after program is terminated, how to prevent? C

This is a program that asks the user to input Shipping information about selling bikes, pretty lame. At the end when it prints out the number of bikes order, and the total cost, the numbers get screwed up. The previously entered amounts seem to be sticking in the memory. How do I fix this? If that is not the problem I would not mind being told so :)
#include <stdio.h>
#include <math.h>
//structure
typedef struct
{char cust_name[25];
char add_one[20];
char add_two[20];
}ORDER;
ORDER order;
int main(void){
fflush(stdin);
system ( "clear" );
//initialize variables
double number_ordered = 0;
double price;
char bike;
char risky;
double m = 359.95;
double s = 279.95;
//inputs for order
printf("Enter Customer Information\n");
printf("Customer Name: ");
scanf(" %[^\n]s", &order.cust_name);
printf("\nEnter Street Address: ");
scanf(" %[^\n]s", &order.add_one);
printf("\nEnter City, State, and ZIP: ");
scanf(" %[^\n]s", &order.add_two);
printf("\nHow Many Bicycles Are Ordered: ");
scanf(" %d", &number_ordered);
printf("\nWhat Type Of Bike Is Ordered\n M Mountain Bike \n S Street Bike");
printf("\nChoose One (M or S): ");
scanf(" %c", &bike);
printf("\nIs The Customer Risky (Y/N): ");
scanf(" %c", &risky);
system ( "clear" );
//print order
printf("\n**********Shipping Instructions**********");
printf("\nTo: %s\n %s\n %s", order.cust_name, order.add_one, order.add_two);
if (bike == 'M' || bike == 'm')
printf("\n\nShip: %d Mountain Bikes", number_ordered);
else
printf("\n\nShip: %d Street Bikes", number_ordered);
if (bike == 'M' || bike == 'm')
price = number_ordered * m;
else
price = number_ordered * s;
if (risky == 'Y' || risky == 'y')
printf("\nBy Freight, COD %d\n", price);
else
printf("\nBy Freight, And Bill The Customer %d\n", price);
printf("*****************************************\n");
return 0;
}
You are printing number_ordered and price, which are doubles, using %d. %d is only for integer types. Use %lf to printf or scanf doubles.
The formats for both your scanf and your printf are wrong, so you're neither reading nor writing your values properly.

Resources