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);
}
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:
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)
instead), you should find these in stdlib.h
, you would need to include it in your file's headers.
C4477/C6067 - name
is already a pointer, you need not to use the &
operator:
scanf("s", name); // Be sure to include specifier width 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) { // no return type needed
//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 - Suppress that specific error, you can see here how to do it.
The safest bet, however, would be to use a better compiler if possible, for example gcc or clang which even has integration with Visual Studio.
C6031 - It complains about the lack of verification on 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("s", 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 aslo advise, in the future, to post the errors as text, as it's easier for users who want to help to find a possible solution by making research easier.