Code:
#include <stdio.h>
float getUserDetail(char*, float*);
float getDiscount(char, float, float, float*);
float getDetails(char*, float*, float*);
float FPrint(char*, float*, float*, float*);
int main()
{
char name, CH, Std, movie;
float age, RM, Disc, seat, Time;
printf("\t\t\t\tWELCOME TO UniMAP CINEMA!!!");
do
{
getUserDetail(&name, &age);
getDiscount(Std, age, RM, &Disc);
getDetails(&movie, &seat, &Time);
printf("\nDo you like to continue? If yes, press 'Y':");
scanf("%s",&CH);
}
while(CH=='Y'||CH=='y');
FPrint(&movie, &seat, &Time, &Disc);
printf("\n\t\t\tThank you");
return 0;
}
float getUserDetail(char*name, float*age)
{
printf("\nName: ");
scanf("%s", name);
printf("Age: ");
scanf("%f",age);
return (*age);
}
float getDiscount(char Std,float age, float RM, float*Disc)
{
RM == 15;
if (age < 13){
*Disc == ((30/100)*RM);
return (*Disc); }
else if (age < 19){
printf("Are you a student?(Y/N):");
scanf("%s", &Std);
{
if(Std == 'Y'|| Std == 'y'){
*Disc == ((50/100)*RM);
return (*Disc); }
else
*Disc == RM;
return (*Disc); }}
else
*Disc == RM;
return (*Disc);
}
float getDetails(char*movie, float*seat, float*Time)
{
printf("Name of the movie: ");
scanf("%s", &movie);
printf("Number of the seat: ");
scanf("%f", &seat);
printf("Time of the movie: ");
scanf("%f", &Time);
}
float FPrint(char*movie, float*seat, float*Time, float*Disc)
{
printf("\n\t\t\tBooking Successful");
printf("\n\t\tMovie Name:%s", &movie);
printf("\n\t\tSeat Number:%f", &seat);
printf("\n\t\tTime:%f", &Time);
printf("\n\t\tDiscounted Price:RM%s", &Disc);
}
Output:
Name: kaus
Age: 21
Name of the movie: qwerty
Number of the seat: 21
Time of the movie: 2.15
Do you like to continue? If yes, press 'Y':n
Booking Successful
Movie Name:§■e
Seat Number:0.000000
Time:0.000000
Discounted Price:RM
■e
Thank you
--------------------------------
Process exited after 14.19 seconds with return value 0
Press any key to continue . . .
If I put the '*' instead of '&' and the printf part I get this output:
Name: kaus
Age: 21
Name of the movie: qwerty
Number of the seat: 21
Time of the movie: 2.15
Do you like to continue? If yes, press 'Y':n
Booking Successful
Movie Name:(null)
Seat Number:0.000000
Time:0.000000
Discounted Price:RM(null)
Thank you
--------------------------------
Process exited after 12.51 seconds with return value 0
Press any key to continue . . .
What do I do? and please do forgive me for any silly mistakes I might have made.Thank You.
Tried using '*' and '&' but not getting the correct output.
Still rusty on the topic of pointers
I want it to print out the
movie name:
time:
seat no.:
price:
Related
I was trying to ask the user if he/she wants another cupcake only if the cost of the first cupcake is less than their budget (maxPrice), but that does not work. It prints nothing after receiving user input. The error occurs where the program states: "if(anotherCupcake == "yes")" and it also occurs when it states, "anotherCupcake == "no".
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(int argc, char**argv)
{
float maxPrice;
char flavor[20];
char flavor2[20];
float price;
double price2;
char anotherCupcake[20];
double newPrice;
// int idxToDel = 2;
//memove(&word[idxToDel], &word[idxToDel + 1], strlen(word) - idxToDel);
printf("Please enter starting budget: ");
scanf("%f", &maxPrice);
printf("Enter the flavor of cupcake 1: ");
scanf("%s", &flavor);
printf("Enter the price of %s cupcake: $", flavor);
scanf("%f", &price);
//determining if you have enough money for a second cupcake
if(price == maxPrice)
{
printf("\nOops! No more money left to spend on anymore cupcakes...enjoy the %s one! Bye!", flavor);
}
else if(price > maxPrice)
{
newPrice=maxPrice-price;
printf("\nWow, you overspent on that first cupcake...you owe $%.2lf. Bye!", fabs(newPrice)*1.0);
}
else
{
printf("Ok, looks like you you have $%.2f left to spend on cupcakes... Would you like to get another one?", maxPrice - price);
scanf("%s", &anotherCupcake);
//figuring out what second cupcake you want
if(anotherCupcake == "yes")
{
printf("Enter the flavor of cupcake 2: ");
scanf("%s", &flavor2);
printf("Enter the price of cupcake 2 $: ");
scanf("%f", &price2);
printf("Done! Enjoy your %s and %s cupcakes! Bye!", flavor, flavor2);
}
if(anotherCupcake == "no")
{
printf("Ok then...Enjoy that one %s cupcake! Bye!", flavor);
}
}
return 0;
}
try use string compare to compare values of two string. It may work.
if(strcmp("yes", anotherCupcake) == 0)
{
printf("Enter the flavor of cupcake 2: ");
scanf("%s", &flavor2);
printf("Enter the price of cupcake 2 $: ");
scanf("%f", &price2);
printf("Done! Enjoy your %s and %s cupcakes! Bye!", flavor, flavor2);
}
The program is just simply supposed to calculate the users age by subtracting their dob from the current year. When I run the program it compiles successfully but I get a long number such as -215863352. The if and else conditions are added just to test them out, I was writing various programs using them to make sure I understand the syntax in c. I figure I'm missing something simple but can't figure it out.
#include <stdio.h>
int main()
{
int year;
int cyear;
int age = cyear - year;
printf("Please enter the year you were born: \n");
scanf("%i", &year);
printf("Now enter the current year: \n");
scanf("%i", &cyear);
if (1+1 == 2){
printf("You must be %i", age);
}
else {
printf("Cannot compute age, GOODBYE:\n");
}
return 0;
}
You are calculating the age before the input is taken from the user. So the age variable is storing a garbage value.
Solution:
Position the calculation of age after taking the input from user that is after taking input of cyear using scanf. The correct code is given below
#include <stdio.h>
int main()
{
int year;
int cyear;
int age =0; //initialise with 0
printf("Please enter the year you were born: \n");
scanf("%i", &year);
printf("Now enter the current year: \n");
scanf("%i", &cyear);
age = cyear - year; //note the change here
if (1+1 == 2){
printf("You must be %i", age);
}
else {
printf("Cannot compute age, GOODBYE:\n");
}
return 0;
}
enter code here
#include <stdio.h>
int main()
{
long long int year;
printf("Please enter the year you were born: \n");
scanf("%lld",&year);
long long int cyear;
printf("Now enter the current year: \n");
scanf("%lld",&cyear);
long long int age = cyear-year;
if (1){
printf("You must be %lld", age);
}
else { printf("Now enter the current year: \n");
scanf("%lld",&cyear);
printf("Cannot compute age, GOODBYE:\n");
}
return 0;
}
In this code at checkout function g_tot calculation brings garbage value. I think its because I'm calculating two variables from another two functions, but I don't know how to fix it. There's another error in restaurant function in if condition if I enter value more than 8 it'll bring garbage value to tot. But the most important one is
#include<stdio.h>
#include<conio.h>
// Global variables
int room,answr,days=0;
char name[20],choose;
int i=0,tot=0,p_tot=0,g_tot=0,z=0;
int p_price[2][5]={4000,10000,20000};
void screenheader()
{
printf("\n ::::::::::::::::::::::::::::::::::::::");
printf("\n :: ::");
printf("\n :: ############################ ::");
printf("\n :: # # ::");
printf("\n :: # WELCOME # ::");
printf("\n :: # TO # ::");
printf("\n :: # SURF HOTEL # ::");
printf("\n :: # # ::");
printf("\n :: ############################ ::");
printf("\n :: ::");
printf("\n ::::::::::::::::::::::::::::::::::::::");
}
void check_in()
{
int contact_No[20],NIC[10];
char first_name[10],last_name[10],Country[10];
system("cls");
screenheader();
printf("\n1. Packages");
printf("\n2. Room Allocation");
printf("\n3. Back\n\n");
scanf(" %d",&answr);
switch(answr)
{
case 1:{
system("cls");
printf("\n\n\nPer 2 Persons");
printf("\n\t\tPackage Name >>>> Couple");
printf("\n\t\tRs.4000/= per day");
printf("\n\t\tBed >>>> 1");
printf("\n\t\t *Tv Available");
printf("\n\n\n\n\t\t\nPer 4 Persons\n\t\tPackage Name >>>>
Family");
printf("\n\t\tRs.10,000/= per day");
printf("\n\t\tBed >>>> 2");
printf("\n\t\t*Tv Available \n\t\t*A/C \n\t\t*WIFI");
printf("\n\n\n\nPer 8 Persons\n\t\tPackage Name >>>> Deluxe");
printf("\n\t\tRs.20,000/= per day");
printf("\n\t\tBed >>>> 3 Large ");
printf("\n\t\t *Tv Available \n\t\t*A/C \n\t\t*WIFI\n\t\t*Local
Travel Guide\n\t\t*Balcony with a view\n\t\t*Writing desk");
printf("\n\n*Press 1 to go back");
getch();
check_in();
break;
}
case 2:{
printf("What package do you want?");
scanf(" %d",&i);
p_tot=p_tot+p_price[i-1];
if(i == 1)
{
printf("You have selected Couple package");
}
else if (i == 2)
{
printf("You have selected Family Package ");
}
else if (i == 3)
{
printf("You have selected Deluxe package");
}
else
{
printf("\n\nWrong input, please refer to packages and try
again.\nPress Enter to select another package");
getch();
check_in();
}
printf("\nEnter First Name:\n");
scanf(" %s",&first_name);
printf("Last Name:\n");
scanf(" %s",&last_name);
printf("How many days do you want to stay?");
scanf(" %d",&days);
printf("Enter your Country:");
scanf(" %s",&Country);
printf("Enter your NIC No:");
scanf(" %d",&NIC);
printf("Enter your Contact No:");
scanf(" %d",&contact_No);
printf("Hello %s %s you have booked a Room for
%d",&first_name,&last_name,days);
getch();
system("cls");
int main();
}
case 3: main();
}
}
void restaurant()
{
int fc[6];
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("Press Enter To Continue To The Restaurant ");
getchar();
system("cls");
printf("\n\n\n\n\n\t *********");
printf("\n\t MENU CARD");
printf("\n\t *********\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\tRs. %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:
{
main();
break;
}
case 1:do
{
printf("\n\nENTER THE FOOD CODE YOU WANT TO HAVE :: ");
scanf("%d", &z);
if (z < 1 || z > 8)
{
printf("Invalid food code\n");
}
tot=tot+price[z-1];
printf("total so far is Rs.%d\n",tot);
printf("DO YOU WANT MORE(Y/N) ::");
scanf(" %c", &ans);
} while(ans=='y'|| ans=='Y');
printf("\n\nYour bill is Rs.%d",tot);
printf("\nYour bill will be added to the total bill at checkout");
printf("\n\nPress Enter to go back to main menu");
getch();
system("cls");
main();
}
}
void check_out()
{
system("cls");
screenheader();
printf("\n\nAre you sure you want checkout (Y/N)");
scanf(" %c",&choose);
if(choose=='n' || choose=='N')
{
main();
}
else if(choose== 'Y' || choose=='y')
{
system("cls");
screenheader();
g_tot=p_tot+tot;
printf("Total");
printf("%d",g_tot);
}
}
int main()
{
screenheader();
printf("\n\n\nPress Enter To Continue");
getchar();
system("cls");
screenheader();
printf("\n\n\n\n\t\t ************* \n");
printf("\t\t * MAIN MENU * \n");
printf("\t\t ************* \n\n\n");
printf("\t\t\t01. Check In \n");
printf("\t\t\t02.Restaurant\n");
printf("\t\t\t03.Checkout \n");
printf("\n\t\t\t04.Exit");
scanf(" %d",&answr);
switch(answr)
{
case 1:{
check_in();
break; }
case 2:{
restaurant();
break; }
case 3: {
check_out();
}
}
return 0;
}
if condition if I enter value more than 8 it'll bring garbage value to tot.
This is as expected. When z > 8, code attempts to access outside price[] range. Result: undefined behavior (UB). The prior if (z < 1 || z > 8) did not steer code away from price[z - 1]. Rest of code including g_tot = p_tot + tot; is now questionable.
int price[8] = {180, 120, 65, 55, 70, 70, 110, 200};
...
if (z < 1 || z > 8) {
printf("Invalid food code\n");
}
tot = tot + price[z - 1]; // UB here
...
g_tot = p_tot + tot;
Do not access price[z - 1] unless z in the range [1...8].
Other problems exist: Best to enable all compilers warnings and seem them (12+) yourself.
Basically I'm a beginner coder and this is what I wrote:
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("COLOR 0A");
char playerName[13];
char playerGender;
int playerAge;
printf("Please input your name and age!\nName: ");
scanf("%s", playerName);
printf("Age (from 18 to 50): ");
scanf("%d", &playerAge);
label:
if(playerAge > 18 && playerAge < 50)
{
printf("What gender are you, M(male) or F(female): ");
scanf("%c", playerGender);
gender:
if(playerGender == 'M' || playerGender == 'F'){
printf("Okay, so your name is %s, you're %d years old and you're a %s.", playerName, playerAge, playerGender);
}else{
printf("Try again.\n\n"
"What gender are you, M(male) or F(female): ");
scanf("%c", playerGender);
goto gender;
}
}else{
printf("Wrong, try again.\n"
"Age (from 18 to 50): ");
scanf("%d", &playerAge);
goto label;
}
return 0;
}
When I put the required age to continue, it crashes on the scanf for the playerGender. Right after it shows me the question about my gender? Where is my mistake?
try:
scanf("%c", &playerGender);
instead of
scanf("%c", playerGender);
as scanf takes a pointer and not a reference to the variable you are trying to fill.
I am also beginner but I think that you need to write thisscanf("%s", playerName); like thisscanf("%12s", playerName);
BTW let me know if that works.
Question:
The program should have menu to allow the user perform the following functions:
semester subject planning
entering of subjects' grade
display of subjects' information for the semester
The program should only terminate when the user select to quit the program.
Please help me solve this problem.
When I run this C programming code it will appear this error:
Error 16 error C2143: syntax error : missing ';' before 'type' g:\mini project testing\cgpacalculation\cgpacalculation\cgpacalculation4.c 190
Error 17 error C2143: syntax error : missing ';' before 'type' g:\mini project testing\cgpacalculation\cgpacalculation\cgpacalculation4.c 224
Please help me correct my coding:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <Windows.h>
//declaration
void userdetail(void);
void mainmenu(void);
void menu(void);
void menu_two(void);
void lines(void);
void getSubject(void);
void getCalculation(void);
void about(void);
float gradesToGP(char grades);
//display
void main()
{
mainmenu();
}
//definition
void mainmenu(void)
{
int select;
lines();
printf("\t\t\t CGPA Calculation \n");
lines();
printf("Enter \"1\" - Student Detail and Subject Information\n");
printf("Enter \"2\" - About\n");
printf("Enter \"0\" - Exit\n");
lines();
printf("Enter your choice :");
scanf("%d", &select);
lines();
if (select == 1)
{
userdetail();
getSubject();
}
else if (select == 2)
{
about();
getch();
menu();
}
else if (select == 0)
{
system("cls");
printf("\t\t THANK YOU FOR USING THIS PROGRAM =D \n");
getch();
}
else
{
printf("\a\a WRONG INPUT! \n");
lines();
getch();
system("cls");
mainmenu();
}
}
void menu(void)
{
int choice;
printf("Enter \"1\" - Back to Main Menu\n");
printf("Enter \"0\" - Exit\n");
lines();
printf("Enter your choice :");
scanf("%d", &choice);
if (choice == 1)
{
system ("cls");
mainmenu();
}
else if (choice == 0)
{
system("cls");
printf("\t\t THANK YOU FOR USING THIS PROGRAM =D \n");
getch();
}
else
{
printf("\a\a WRONG INPUT! \n");
getch();
system("cls");
menu();
}
}
void lines(void)
{
printf("**********************************************************************");
}
void userdetail(void)
{
char name[100][100], id[100][10];
printf("Enter your full name :\n");
scanf("%s", &name);
printf("Enter your student ID :\n");
scanf("%s", &id);
}
void getSubject(void)
{
int loop,numSubject, credit[100];
float GP[100], TargetCGPA[100];
char SubjectCode[100][40], grade[100];
char name[100][100];
char id[100][40];
int sumCredit = 0;
double sumCreditxGP = 0;
system("cls");
lines();
printf("Enter total subject :");
scanf("%d", &numSubject);
lines();
for (loop = 0; loop <= numSubject-1; loop++)
{
printf("Subject %d \n", loop+1);
printf("Enter subject code :");
scanf("%s", &SubjectCode[loop]);
printf("Credit hour :");
scanf("%d", &credit[loop]);
printf("Enter your grade :");
scanf("%s", &grade[loop]);
GP[loop] = gradesToGP(grade[loop]);
lines();
}
printf("Enter your targeted CGPA for this semester :");
scanf("%f", &TargetCGPA);
lines();
printf("Press \" ENTER \" or any button");
getch();
system("cls");
menu_two();
void menu_two(void);
{
int choice;
printf("Enter \"1\" - CGPA Calculation\n");
printf("Enter \"0\" - Exit\n");
lines();
printf("Enter your choice :");
scanf("%d", &choice);
if (choice == 1)
{
system ("cls");
getCalculation();
}
else if (choice == 0)
{
system("cls");
printf("\t\t THANK YOU FOR USING THIS PROGRAM =D \n");
getch();
}
else
{
printf("\a\a WRONG INPUT! \n");
getch();
system("cls");
menu_two();
}
}
void getCalculation(void);
{
system("cls");
lines();
printf("Student Name : %c\n", name);
printf("Student ID : %c\n", id);
lines();
printf("No. Subject Code Credit Hour Grade Grade Point");
lines();
for (loop = 0; loop <= numSubject-1; loop++)
{
printf("\n%d %s\t %d\t%s\t%.2f\n", loop+1, SubjectCode[loop], credit[loop], grade[loop], GP[loop]);
}
for (loop = 0; loop <= numSubject-1; loop++)
{
sumCredit = sumCredit + credit[loop];
sumCreditxGP = sumCreditxGP + credit[loop] * GP[loop];
}
lines();
printf("Total credit hour is = %d\n\n", sumCredit);
printf("Total credit hour X grade point is = %.2f\n\n", sumCreditxGP);
printf("CGPA is = %.2f", sumCreditxGP / sumCredit);
lines();
getch();
menu();
}
}
void about(void)
{
system("cls");
lines();
printf("\n\t\t\tMini Project\n");
lines();
printf("Develop by: Shah Rezza Bin Jasni\n");
printf("Institution: Universiti Tenaga Nasional\n\n\n");
lines();
printf("COPYRIGHT 2014");
lines();
}
float gradesToGP(char grades)
{
if (grades == 'A+')
{
return(float)4.00;
}
else if (grades == 'A')
{
return(float)4.00;
}
else if (grades == 'A-')
{
return(float)3.67;
}
else if (grades == 'B+')
{
return(float)3.33;
}
else if (grades == 'B')
{
return(float)3.00;
}
else if (grades == 'B-')
{
return(float)2.67;
}
else if (grades == 'C+')
{
return(float)2.33;
}
else if (grades == 'C')
{
return(float)2.00;
}
else if (grades == 'C-')
{
return(float)1.67;
}
else if (grades == 'D+')
{
return(float)1.33;
}
else if (grades == 'D')
{
return(float)1.00;
}
else if (grades == 'E')
{
return(float)0.00;
}
else
{
return(float)0.00;
}
}
It looks like you put the function menu_two inside another function. Your Visual C++ compiler doesn't accept local functions.
Same problem with getCalculation. According to your declarations, those should be in global scope.
You have few problems with your code-
menu_two();
} // here you need to close it
void menu_two(void) // remove the semicolon here
Because Local functions are not acceptable. Don't include any function inside another function.
void getCalculation(void) // remove semicolon here
Make the above two functions as a Global function. any try to pass the necessary information to that functions and try.