Source outline: User selects option either to 1. Make Bugatti; 2. Create Bugatti; or 3. Exit program. After each option is complete, the user should be returned back to the menu to select another option.
(Note: the user cannot display the car until it is created, hence the if statement in case 2)
The problem: User's inputs for createCar() function are not being returned back into main() (specifically to case 2 - Display Bugatti) and is displaying some large, odd values, instead of the user's inputs. I know it has something to do with the values not being stored into memory/called back to main().
Also, the while statements in createCar() function are completely being disregarded when I use parameters for some reason.
I would appreciate answers in code to make things easier to resolve personally if possible, thanks!
#include <stdio.h>
#include <math.h>
#define now 2017
//Function headers
void printMenu(void);
void createCar(int *speed, int *year, int *bhp, int *age);
int main(void)
{
//Variables
int userInput;
int topSpeed, yearMade, horsepower, carAge;
/***Loop program to return to menu after option is completed***/
for(;;)
{
//Print menu and get input from user
printMenu();
scanf("%i", &userInput), fflush(stdin);
//Validate input
while(userInput < 1 || userInput > 3)
{
printf("\nWrong input, please retry...\n");
scanf("%i", &userInput), fflush(stdin);
}
//Make decisions after user's choice
switch(userInput)
{
//Option 1: Create car then return to menu
case 1:
createCar(&topSpeed, &yearMade, &horsepower, &carAge);
continue;
//Option 2: Read car details (if created) then return to menu
case 2:
if(topSpeed == NULL)
{
printf("\nYou must first create a car, please retry...\n\n");
continue;
}
printf("\n----Bugatti Veyron----\n");
printf("Top Speed: %i km/h\nYear made: %i\nAge: %i years old\nHorsepower: %i bhp\n", &topSpeed, &yearMade, &horsepower, &carAge);
printf("----------------------\n");
continue;
//Option 3: Kill program
case 3:
exit(1);
}
}
return 0;
}
//Function: Display menu
void printMenu(void)
{
printf("-----------------------------------------\n");
printf("[Bob's Custom Car Creation Complex v1.0]\n");
printf("1. Create Bugatti\n2. Display Bugatti\n3. Exit\n");
printf("-----------------------------------------\n");
}
//Function: Make a car + validate inputs
void createCar(int *speed, int *year, int *bhp, int *age)
{
//Prompt user for top speed + validate input
printf("Enter the top speed of your Bugatti:");
scanf("%i", &speed), fflush(stdin);
while(speed <=0)
{
printf("You cannot have a top speed of nothing silly :-D\nPlease retry...\n");
scanf("%i", &speed), fflush(stdin);
}
//Prompt user for year mate + validate input
printf("What year is your Bugatti produced?:");
scanf("%i", &year), fflush(stdin);
while(year <=0)
{
printf("You cannot own a Bugatti that is from the future laddy!!\nPlease retry...\n");
scanf("%i", &year), fflush(stdin);
}
//Calculate age of car
age = now - year;
//Prompt user for horsepower + validate input
printf("How much horsepower does your Bugatti have?:");
scanf("%i", &bhp), fflush(stdin);
while(bhp <=0)
{
printf("A Bugatti with no engine... doesn't sound too promising :-O\nPlease retry...\n");
scanf("%i", &bhp), fflush(stdin);
}
}
You have to dereference the age and year pointer to get/set its value.
//Calculate age of car
*age = now - *year;
You have to remove the '&' at the scanf() in createVar, because speed, year and bhp are already pointers to int.
Enabling compiler warnings and resolving them would avoid you troubles!
Related
This code grabs input from users, and first, to ensure they have entered the correct type of input, I let the function print out the menu on the screen and then scanf() the user input. It should be very straight forward code, but I keep getting errors and warnings which I don't understand, can someone plesae help me with this?
(I am still in the process of getting used to code in C.)
//include library
#include<stdio.h>
#include<string.h>
//variable declaration
int age,userInputOption,ptr_InputCk;
char name[20];
float point;
//Fuction prototype
void displayMenu();
void wrongInput();
char getTheName(char name[20]);
void optionSwitch();
int getTheAge(int age);
float getThePoint(float point);
//void clearData(char name, int age, float point);
int quitTheProgram();
void displayKnownData();
//main function
int main() {
displayMenu();
ptr_InputCk = scanf("%d", &userInputOption);
if (ptr_InputCk != 1) {
wrongInput();
displayMenu();
}
else if (userInputOption >=1 || userInputOption <=5) {
optionSwitch();
}
else if(userInputOption == 6) {
quitTheProgram();
}
return (0);
}
//Define Functions
void displayMenu() {
printf("1. enter a name: \n\n");
printf("2. enter an age: \n\n");
printf("3. enter the person’s points per game: \n\n");
printf("4. display the known data: \n\n");
printf("5. clear all data: \n\n");
printf("6. quit: \n\n");
printf("Please enter a number between 1 ~ 6:\n\n");
}
void wrongInput() {
printf("Wrong input, please re-enter");
}
void optionSwitch() {
switch (userInputOption) {
case 1:
getTheName(name);
break;
case 2:
getTheAge(age);
break;
case 3:
getThePoint(point);
break;
case 4:
displayKnownData();
break;
//case 5:
//clearData(name,age,point);
//break;
case 6:
quitTheProgram();
break;
}
}
char getTheName(char name[20]) {
printf("Please enter your name: \n");
scanf("%s", &name);
printf("You name: %s\n", name);
return (name[20]);
}
int getTheAge(int age) {
printf("Please enter your age: \n");
scanf("%d", &age);
printf("Your age: %d\n", age);
return (age);
}
float getThePoint(float point) {
printf("Please enter points: \n");
scanf("%f", &point);
printf("Your age: %f\n", point);
return (point);
}
/*/
void clearData() {
char* name = NULL;
int* age = NULL;
float* point = NULL;
return(name, age, point);
}*/
int quitTheProgram() {
_exit(0);
}
void displayKnownData() {
printf("name: %s\nage: %d\npoint: %f\n", name, age, point);
}
I would like to add that using scanf the way you did to fill a string isn't secure because you can easily overflow your buffer (char name[20]) if the user enters more than 19 characters.
Other solutions:
#include <stdio.h>
#define MAX_LIMIT 20
int main()
{
char str[MAX_LIMIT];
fgets(str, MAX_LIMIT, stdin);
printf("%s", str);
return 0;
}
Or
(...)
puts ("Please enter a string of 20 characters or fewer.");
scanf ("%20s", string1);
C4013 - _exit(0) does not exist in the declared libraries, if you were working on linux you'd find it in unistd.h.
Since you're working with msvc, apparenty you have such function declared on process.h (don´t quote me on that), I would, however, prefer to use the more appropriate exit(0) (or alternatively _Exit(0)), you should find these in stdlib.h, you need to include it in your file's headers.
C4477/C6067 - name is already a pointer, you need not to use the & operator:
scanf("%19s", name); // Be sure to include a width specifier to avoid buffer oferflow
C4996 - msvc does not like scanf at all, you can either:
1 - Use scanf_s as it tells you to (there are also some corrections I would make to the function itself as commented bellow):
void getTheName(char* name, size_t size) { // void return type
//buffer size for scanf_s
printf("Please enter your name: \n");
scanf_s("%s", name, size); //<---
printf("Your name: %s\n", name);
//no return needed, the input gets stored on the memory pointed by name
}
(Since name is global you could use sizeof(name) for buffer size, but I would advise you to make it local to its necessary scope, globals can get out of hand fast. The advice extends to all of your global variables, none of them seems to need to be global in your code.)
2 - Alternatively you could suppress that specific error, you can see how to do it here:
Why does Visual Studio 2013 error on C4996?
The safest bet, however, would be to use a better compiler if possible, for example gcc or clang, both can be used with Visual Studio.
C6031 - It complains about the lack of verification of scanf return value, and that's good advice, the function returns the number of read elements, in this case it shoud return 1, so you could use, for example:
if(scanf("%19s", name) != 1){
//handle error
}
//otherwise continue normal execution
This would make sure it works as expected.
The error C4566 is about character encoding, I suspect it may be a false positive or related to your encoding settings, anyway, fix the other problems and see if it still happens. You can then post a more specific question, if it remains.
Final notes - By clicking on the errors you'll navigate to the Microsoft docs of the respective error, that may help you understand the issue. Granted it's not always ver helpful. I would also advise, in the future, to post the errors as text, it's easier for users who want to help to find a possible solution by making research easier.
I'm making a menu that lists options 1-3. The user is expected to enter an integer.
scanf("%d", &select_option)
How do I prompt error when user enters a char (for example "a", or "asd" for long strings, or a mixture like "1a2") instead of an expected int? Thanks.
Note: When the user enters a 'char' like 'a', 'asd', the code goes into an infinite loop for some reason.
Here's my program (minimal example):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
printf("Favourite sports? \n");
printf("1. Tennis\n");
printf("2. Badminton\n");
printf("3. Basketball\n");
printf("4. Exit program.\n");
printf("Enter your choice (1-4): ");
scanf("%d", &select_option);
while(select_option != 4)
{
switch(select_option)
{
case 1:
printf("You like tennis! Nice! \n");
break;
case 2:
printf("You like badminton! Nice!");
break;
case 3:
printf("You like basketball! Nice!");
break;
default:
system("clear");
printf("Invalid option. Please re-enter your choice (1-4).\n");
}//end switch
printf("Favourite sports? \n");
printf("1. Tennis\n");
printf("2. Badminton\n");
printf("3. Basketball\n");
printf("4. Exit program.\n");
printf("Enter your choice (1-4): ");
scanf("%d", &select_option);
}//end while
}//end main
You could do this:
#include <stdio.h>
int main(void) {
int v;
int ret = scanf("%d", &v);
if(ret == 1)
printf("OK, %d\n", v);
else
printf("Something went wrong!\n");
return 0;
}
where I took advantage of the return value of scanf(), and based on that value, I made an assumption. This will fail for the case of "1a2", but will succeed for "12" and "a".
However, this is a broad question and personally the way I would go for it is:
Use fgets() to read input.
Discard newline.
Convert string to integer (with strtol() for example).
Validate input.
I am assuming u are a beginner. You can use Switch Case which is used usually for creating menus and depending on the choice of the user executes the particular case.
I will show u a small example.
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
printf("Select the sports u want to do\n");
printf("1.Tennis\n2.Karate\n3.Football\n");
scanf("%d",&n);
Switch(n)
{
case 1:printf("You chose Tennis\n");
break; //To prevent from all cases being executed we use
//break which helps from coming out of a loop
case 2:printf("You chose Karate\n");
break;
case 3:printf("You chose Football\n");
break;
default:printf("Please enter an appropriate number !");
//Cases which dont match with the input are handled by default !
}
}
Also to make the user enter input until he wants to exit add a while loop with a variable !
I hope this helps!
I have an assignment to write a program for supporting an art gallery in C. It has to be menu based program using lists. I wrote the first function of the menu and I need some help writing the other three. So I have a structure of an unique code of the painting, author's name, painting's name, price, year of the painting. I have to create a function deleting a painting using the unique code, print out all the info about every painting and modifying a painting again using said code. The data has to be in a dynamic type structure using a linked list. This is the program so far.
#include <stdio.h>
void addPainting(void);
void erasePainting(void);
void printData(void);
void modifyData(void);
int main()
{
struct paintings
{
char code[20];
char name[50];
char paintingName[50];
double price;
int year;
}painting[100];
int choice;
do
{
printf("Menu\n");
printf("To add a painting press 1.\n");
printf("To erase a painting press 2.\n");
printf("To print data for all paintings by authors alphabetically press 3.\n");
printf ("To modify data for a painting press 4.\n");
printf("To exit the program press 5.\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
addPainting();
break;
}
case 2:
{
erasePainting();
break;
}
case 3:
{
printData();
break;
}
case 4:
{
modifyData();
break;
}
default: printf ("Wrong choice. Try again\n");
break;
}
}while (choice !=5);
void addPainting()
{
FILE *fp;
struct paintings painting;
printf("Enter code:");
scanf("%s", &painting.code);
printf("Enter the name of the author:");
scanf("%s", &painting.name);
printf("Enter the name of the painting:");
scanf("%s", &painting.paintingName);
printf("Enter price:");
scanf("%lf", &painting.price);
printf("Enter the year of creation:");
scanf("%d", &painting.year);
if ((fp=fopen("paintings","wb"))==NULL)
exit(1);
if ((fwrite (&painting,sizeof(painting),1,fp)!=1))
exit(2);
fclose(fp);
}
}
First problem: You are missing the closing brace ( } ) for the main() function. (but I am sure you knew that)
The reason for the struct size error is that you are attempting to create an instance of struct painting in the void addPainting() function, when it was created with local scope in the main function, and therefore it is not visible to the function. Create struct painting with global scope if you want to use it this way:
This will build (and run) but only for the functions defined, the others are commented out. There are other problems you will have to work out, or ask about.
EDITED to fix scanf() statements, show use of fopen()/fclose(), create and write strings using sprintf()/fputs()...
void addPainting(void);
//void erasePainting(void);
//void printData(void);
//void modifyData(void);
typedef struct //created with global scope, visible to all functions
{
char code[20];
char name[50];
char paintingName[50];
double price;
int year;
}PAINTING;
PAINTING painting[100];//array of instance of PAINTING
#define PATH "C:\\play\\painting.txt" //edit to your need
int main()
{
int choice;
do
{
printf("Menu\n");
printf("To add a painting press 1.\n");
printf("To erase a painting press 2.\n");
printf("To print data for all paintings by authors alphabetically press 3.\n");
printf ("To modify data for a painting press 4.\n");
printf("To exit the program press 5.\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
addPainting();
break;
}
case 2:
{
//erasePainting();
break;
}
case 3:
{
//printData();
break;
}
case 4:
{
//modifyData();
break;
}
default: printf ("Wrong choice. Try again\n");
break;
}
}while (choice !=5);
}
void addPainting(void)
{
FILE *fp;
char stringToWrite[80];
//Note: this function could be prototyped with an int argument
// to be used as an index for the array arguments of your
// structure. Currently, the indexes are hard-coded to 0,
printf("Enter code:");
//scanf("%s", &painting[0].code);
scanf("%s", painting[0].code); //& not needed for char array (char *) et. al.
printf("Enter the name of the author:");
scanf("%s", painting[0].name);
printf("Enter the name of the painting:");
scanf("%s", painting[0].paintingName);
printf("Enter price:");
scanf("%lf", &painting[0].price);
printf("Enter the year of creation:");
scanf("%d", &painting[0].year);
fp = fopen (PATH, "a");//open for create/append text file (not write binary, "wb")
if(fp)
{
sprintf(stringToWrite, "Painting Code is: %s\n", painting[0].code);
fputs(stringToWrite, fp);
// do others same way...
//...
fclose(fp);
}
}
So I have to make an ATM machine program for my Intro to C programming class and I'm getting kinda frustrated. My professor just emailed us saying this: "Under NO circumstances should you declare any pointers. You are provided the pointers you will need to use and those are the function arguments. They should not be re-declared in the functions or in the main function.
You alos need to actually READ the comment I wrote with the assignment. Most (or all ) of you will need to add an argument to the withdrawal function because, to do it correctly, you will need access to the type of account."
I tried not declaring the pointers in the main function but I'd get errors. I also am not sure if I should be using if/else instead of switch, cause every time it asks me if I'd like to do another transaction the program closes no matter which (1, 2, 3) I select.
My last problem is I don't know how to go about updating the amount of money in the selected account when making a transaction. (currball) is confusing...
I really appreciate any help I can get.
// main.c
// Project Assignment 2
//
// Created by Paul Gleichman on 2/22/14.
// Copyright (c) 2014 Paul Gleichman. All rights reserved.
//
#include <stdio.h>
//* Displays the list of user’s options available
//** Displays the user’s selections and sets the value of the choice
void mainMenu(int *choice);
//Prompts the user for the amount of their deposit and updates the selected account
void DepositMoney( double *currBal);
//Asks the user if they want another transaction
void Repeat(char * doAgain);
//Displays the types of account they would like to access and sets the
//value of the chosen account type
void AccountMenu( char *typeAcct);
//Prompts the user for the amount of the withdrawal, determines if there are
//sufficient funds and updates the selected account if funds are dispensed
void WithdrawMoney( double *currBal);
//Displays the user’s current account balance for the selected account
void ShowBalance( double currBal);
int main()
{
double preBal = 4325;
double checking = 575;
double savings = 3750;
double currBal;
int choice;
char doAgain;
char typeAcct;
//Welcome Screen
printf("***** Welcome to Legendary Bank ***** \n");
//Ask user what they'd like to do
printf("** What would you like to do today? ** \n");
//List options
mainMenu(&choice);
switch (choice) {
case 1:
AccountMenu(&typeAcct);
DepositMoney(&currBal);
Repeat(&doAgain);
break;
case 2:
AccountMenu(&typeAcct);
WithdrawMoney(&currBal);
Repeat(&doAgain);
break;
case 3:
AccountMenu(&typeAcct);
ShowBalance(currBal);
Repeat(&doAgain);
}
}
//*Displays the list of user’s options available
//**Displays the user’s selections and sets the value of the choice
void mainMenu(int *choice)
{
printf("1 - DEPOSIT \n");
printf("2 - WITHDRAWAL \n");
printf("3 - CHECK ACCOUNT BALANCE \n");
printf("Important: ");
printf("To transfer money first select \n(2) for WITHDRAWAL, then \n(1) for DEPOSIT\n");
scanf(" %d", choice);
}
//Prompts the user for the amount of their deposit and updates the selected account
void DepositMoney( double *currBal)
{
printf("How much would you like to deposit?: \n");
scanf(" %lf", currBal);
printf("Thank you, please take your receipt.\n");
}
//Asks the user if they want another transaction
void Repeat(char * doAgain)
{
int choice;
printf("Would you like to make another transaction?\n");
printf("(Y)es / (N)o ? \n");
scanf(" %c", doAgain);
do {
mainMenu(&choice);
} while (doAgain == 'Y' || doAgain == 'y');
}
//Displays the types of account they would like to access and sets the
//value of the chosen account type
void AccountMenu( char *typeAcct)
{
printf("Please select account: \n");
printf("Choose C for Checking\n");
printf("Choose S for Savings\n");
scanf(" %c", typeAcct);
}
//Prompts the user for the amount of the withdrawal, determines if there are
//sufficient funds and updates the selected account if funds are dispensed
void WithdrawMoney( double *currBal)
{
printf("How much would you like to withdraw?\n");
scanf(" %lf", currBal);
printf("Thank you, please take your cash and receipt\n");
}
//Displays the user’s current account balance for the selected account
void ShowBalance( double currBal)
{
printf("You have %lf in your account\n", currBal);
}
You need to do something like this. edited your main and repeat function
int main()
{
double preBal = 4325;
double checking = 575;
double savings = 3750;
double currBal;
int choice;
char doAgain =0;
char typeAcct;
//Welcome Screen
while(1){
printf("***** Welcome to Legendary Bank ***** \n");
//Ask user what they'd like to do
printf("** What would you like to do today? ** \n");
//List options
mainMenu(&choice);
do{
switch (choice) {
case 1:
AccountMenu(&typeAcct);
DepositMoney(&currBal);
Repeat(&doAgain);
break;
case 2:
AccountMenu(&typeAcct);
WithdrawMoney(&currBal);
Repeat(&doAgain);
break;
case 3:
AccountMenu(&typeAcct);
ShowBalance(currBal);
Repeat(&doAgain);
default :
printf("invalid Choice");
Repeat(&doAgain);
}
}while(doAgain == 'Y');
printf("//////////////////NEW TRANSACTION//////////////\n")
}
return 0;
}
in your repeat function
void Repeat(char * doAgain)
{
int choice;
printf("Would you like to make another transaction?\n");
printf("(Y)es / (N)o ? \n");
scanf(" %c", doAgain);
}
The following code is a project I'm working on and after entering the details as in name and email it won't go to the next part of the code which is printing the price and then go to the next function. What did I do wrong??
Also, what can I do so that a customer can enter their details with spacing?
Thanks in advance.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int option,card_num,csc,phone_number;
char name,address,e_mail,registration;
void membership();
void payment();
int main()
{
membership();
return 0;
}
void membership()
{
printf("\tTHE CRUMP'S MEMBERSHIP");
printf("\n\n This membership ...");
printf("\n\n REGISTRATION [CONTRACTOR(A)/CORPORATION(B)]=");
scanf("%c",®istration);
switch (registration)
{
case 'A':
printf("\n\nEnter details without any spacing.");
printf("\nNAME:");
scanf("%s",&name);
printf("\nADDRESS:");
scanf("%s",&address);
printf("\nPHONE NUMBER:");
scanf("%d",&phone_number);
printf("\nE-MAIL:");
scanf("%s",&e_mail);
break;
case 'B':
printf("Enter details without any spacing.");
printf("\nNAME OF CORPORATION:");
scanf("%s",&name);
printf("\nADDRESS OF CORPORATION:");
scanf("%s",&address);
printf("\nPHONE NUMBER OF CORPORATION:");
scanf("%d",&phone_number);
printf("\nE-MAIL OF CORPORATION:");
scanf("%s",&e_mail);
break;
}
if (registration=='A')
printf("\n THE REGISTRATION FEE = |RM 50/MONTH |\t| RM 500/YEAR|");
else if (registration=='B')
printf("\n THE REGISTRATION FEE = |RM 200/MONTH |\t| RM 2200/YEAR|");
}
void payment()
{
printf("\n\nChoose method of payment: ");
printf("\n\t 1- Money Transfer \n\t 2-Debit Card\n");
scanf("%d",&option);
if (option==1)
{
printf("\nYou have chosen Money Transfer.");
printf("\nYou can transfer your money at our bank account --> 4365 4200 1471");
printf ("\n your membership will be confirmed when we have received the payment");
printf("\n************************************************************");
if (registration=='A')
{
printf("\nNAME:%s",name);
printf("\nADDRESS:%s",address);
printf("\nPHONE NUMBER:%d",phone_number);
printf("\nE-MAIL:%s",e_mail);
}
else if (registration=='B')
{
printf("\nNAME OF CORPORATION:%s",name);
printf("\nADDRESS OF CORPORATION:%s",address);
printf("\nPHONE NUMBER OF CORPORATION:%d",phone_number);
printf("\nE-MAIL OF CORPORATION:%s",e_mail);
}
printf("\n\n your transaction completed...\n\n Enjoy your membership discount.");
}
else if (option==2)
{
printf("\nYou have chosen Credit/Debit card.");
printf("\n Please enter your Credit/Debit card number:");
scanf("%d",&card_num);
printf("\n Please enter CSC code:");
scanf("%d",&csc);
printf("\nYour membership will be confirmed when we have received the payment");
printf("\n*********************************************************\n\n");
if (registration=='A')
{
printf("\nNAME:%s",name);
printf("\nADDRESS:%s",address);
printf("\nPHONE NUMBER:%d",phone_number);
printf("\nE-MAIL:%s",e_mail);
}
else if (registration=='B')
{
printf("\nNAME OF CORPORATION:%s",name);
printf("\nADDRESS OF CORPORATION:%s",address);
printf("\nPHONE NUMBER OF CORPORATION:%d",phone_number);
printf("\nE-MAIL OF CORPORATION:%s",e_mail);
}
printf("\n\n Your transaction is completed...\n\n Enjoy your membership discount.");
}
}
You are trying to read strings into char variables:
char name,address,e_mail,registration;
//...
scanf("%s",&name);
char only holds one character, not a string. If you want to read a string use an character array:
char name[100];
//...
and pass it to scanf like this:
scanf("%s",name);
because the array already decays to a pointer and taking the address is unnecessary.
Note that this requires you to set a limit for the name length. For example I choose 100-1 = 99 characters as limit. If the input is longer, undefined behaviour occurs, which is what you are experiencing right now.
Also note that you never call payment, so this will never be executed. If you want it to be executed after membership, then you need to call it:
int main()
{
membership();
payment();
return 0;
}
Your program has no call to payment() function thats why the program flow is not complete.