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 put into the variable user input option. it should be very straight forward code, but I keep getting error, someone help me to fix some error please.
I am still in the process of getting used to code in c language.
//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 persons 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);
}
CodePudding user response:
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 (" s", string1);
CodePudding user response:
The problems are relatively trivial, _exit(0)
does not exist in the declared libraries, if you were working on linux you'd need unistd.h
and that would fix the warning. But since you're on windows, apparenty you have such function declared on process.h
, don´t quote me on that, I would however, prefer to use exit(0)
or _Exit(0)
instead, that's on the stdlib.h
, you would need to include it in your file's headers.
The scanf
problems, first of all, name
is already a pointer, you need not to use the &
operator:
scanf("s", name); // Be sure to include width to avoid buffer oferflow
Moreover CL does not like scanf
at all, you can either 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) { // no return type needed
//buffer size for scanf_s
printf("Please enter your name: \n");
scanf_s("%s", name, size); //<---
printf("You 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 it's necessary scope, globals can get out of hand fast.)
Or suppress that specific error, you can see here how to do it.
The safest bet, however, would be to use a decent compiler if possible, for example gcc or clang which even has integration with Visual Studio.