gpt4 book ai didi

c - 为什么这段代码可以在 gcc 6.3.0 上运行,而不能在 4.9.2 上运行?

转载 作者:行者123 更新时间:2023-11-30 21:34:56 26 4
gpt4 key购买 nike

我有一个类(class)游戏,用于显示二分搜索。该代码在我的 ide(atom) 和 gcc 编译器(gcc 版本 6.3.0 (MinGW.org GCC-6.3.0-1))上编译和运行没有错误。我的教授正在使用 Dev C++,他的编译器是(gcc 4.9.2)。不幸的是,他的版本编译时不会出现错误,但在从普通游戏中获取用户输入时也会失败。我在调试方面不够熟练,无法找出问题所在。感谢任何帮助,并且解释将是令人难以置信的。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>



//--Menu--//
//user input for main menu
int userInput(){
int var;
scanf("%d", &var);
return var;
}
//main menu for the user to decide what version they want to play
void menuMain(){
printf("------------------------\n");
printf("Menu\n");
printf("1 - Normal Game\n");
printf("2 - Efficency Mode\n");
printf("3 - Quick Game\n");
printf("4 - EXIT PROGRAM\n");
printf("------------------------\n");
};
//good bye message
void menuGoodBye(){
printf("---------------\n");
printf("Good Bye!\n");
printf("---------------\n");
}
//gets the users response and capitalizes it
char responseUser(int guess){
char y;
printf("---------------------------------------------\n");
printf("Is it %d?\n",guess);
printf("Please respond with Lower (L), Higher (H), or Correct (C).\n");
scanf("%s",&y);
y = toupper(y);
return y;
}

//--Normal Game--//
//instructions for the user Normal Games
void instructions(int min,int max){
printf("----------------------------------------------------------------------\n");
printf("Pick a number from %d to %d and I will try and guess it.\n",min,
max);
printf("If your number is lower, respond with a L.\n");
printf("If your number is higher, respond with a H.\n");
printf("If I guess correctly, respond with a C.\n");
printf("----------------------------------------------------------------------\n");
}
//uses binary search for efficient gameplay
int efficentGuesser(int min, int max){
return (max+min)/2;
}
//uses random numbers with updated minimums and maximums
int gameGuesser(int min, int max){
int x;
x=rand()%(max+1-min)+min;
return x;
}
//the modular switch for the game
void gameSwitch(int (*method)(int, int),int min, int max){
char response;
int guess;
do{
guess = (*method)(min,max);
response = responseUser(guess);
switch (response) {
case 'L':
max= guess-1;
break;
case 'H':
min= guess+1;
break;
case 'C':
break;
default:
break;
}
}while(response != 'C');
}


//--Quick Game--//
//instructions for the quick game
void instructionsQuickGame(int min, int max){
printf("----------------------------------------------------------------------\n");
printf("Pick a number from %d to %d and I will try and guess it.\n",min,
max);
printf("----------------------------------------------------------------------\n");
}
//search for a quick game
void quickGame(int (*method)(int, int),int x, int y){
int input,guess,min,max, trys;
input= userInput();

clock_t begin = clock();
min = x;
max = y;

do{
guess=(*method)(min, max);
if(input > guess){
min = guess + 1;
}else{
max = guess - 1;
}
trys++;
}while(input != guess);
clock_t end = clock();

printf("Wow,That took me %.8f seconds and %d trys!\n",(double)(end -
begin) / CLOCKS_PER_SEC,trys);

}

//--Modular Block--//
//basic building block for game, you can import functions modularly
void defaultGame(int (*method)(int, int), void (*s)(), void(*instructions)
(int, int)){
int min, max;
min =0;
max =100;
(*instructions)(min,max);
(*s)((*method),min,max);

}


//the actual code that runs
int main(){
srand(time(0));
int response;
do{
menuMain();
response=userInput();
switch (response){
case 1:
//defaultGame(method, what switch, what instructions)
defaultGame(gameGuesser, gameSwitch, instructions);
break;
case 2:
//defaultGame(method, what switch, what instructions)
defaultGame(efficentGuesser, gameSwitch,instructions);
break;
case 3:
//defaultGame(method, what switch, what instructions)
defaultGame(efficentGuesser, quickGame, instructionsQuickGame);
break;
case 4:
menuGoodBye();
break;
default:
printf("Please Pick a number from the menu.\n");
break;
}
}while(response != 4);

return EXIT_SUCCESS;
}

最佳答案

未定义行为 (UB)

字符; ... scanf("%s",&y); 是未定义的行为,因为 "%s" 指示 scanf() 形成一个字符串 至少一个非空白字符加上附加的空字符char y 太小了。 @Eugene Sh.

请考虑以下内容。

scanf(" %c",&y);
<小时/>

其他问题也存在,但以上是一个大问题。

关于c - 为什么这段代码可以在 gcc 6.3.0 上运行,而不能在 4.9.2 上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54150809/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com