[英]I wrote a code in C language to print passed/not passed on basis of his marks. No matter, what I input, I am getting the same results i.e passed
[英]the code i wrote is for input one person's date, which related with his age, score , and name. however I am having trouble to get my code fixed
此代碼從用戶那里獲取輸入,並首先確保他們輸入了正確的輸入類型。 我讓 function 在屏幕上打印出菜單,然后 scanf() 將用戶輸入放入可變用戶輸入選項中。 它應該是非常直接的代碼,但我不斷收到錯誤,請有人幫我修復一些錯誤。
我仍在習慣使用 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 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);
}
問題相對微不足道,聲明的庫中不存在_exit(0)
,如果您使用的是 linux,則需要unistd.h
並且可以修復警告。 但是由於您在 windows 上,顯然您在process.h
上聲明了這樣的 function ,請不要引用我的話,但是我寧願使用exit(0)
或_Exit(0)
代替,這是在標准庫上stdlib.h
,您需要將其包含在文件的標題中。
scanf
的問題,首先name
已經是一個指針,不需要使用&
操作符:
scanf("%19s", name); // Be sure to include width to avoid buffer oferflow
此外,CL 根本不喜歡scanf
,您可以按照它告訴您的方式使用scanf_s
,我還會對 function 本身進行一些更正,如下所述:
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
}
(由於name
是全局的,您可以使用sizeof(name)
作為緩沖區大小,但我建議您將其設置為必要的 scope 的本地,全局變量可能會很快失控。)
或抑制該特定錯誤,您可以在此處查看如何操作。
然而,最安全的選擇是盡可能使用合適的編譯器,例如gcc或clang ,它們甚至與 Visual Studio 集成。
我想補充一點,使用 scanf 填充字符串的方式並不安全,因為如果用戶輸入超過 19 個字符,您很容易溢出緩沖區( char name[20]
)。
其他解決方案:
#include <stdio.h>
#define MAX_LIMIT 20
int main()
{
char str[MAX_LIMIT];
fgets(str, MAX_LIMIT, stdin);
printf("%s", str);
return 0;
}
或者
(...)
puts ("Please enter a string of 20 characters or fewer.");
scanf ("%20s", string1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.