C++执行后的随机数

  • 本文关键字:随机数 执行 C++ c++
  • 更新时间 :
  • 英文 :


一切都像我想要的那样工作,但唯一的问题是,当我执行这个文件时,它会在行尾生成一些我认为不正常的随机数,我知道这段代码有问题,任何人都可以查看这段代码并告诉我出了什么问题

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
using namespace std;
void message();
int choose1(char);
int choose2(char);
int choose3(char);
int main()
{
message();
int inputA, inputB, inputC;
cout<<"A little girl kicks a soccer ball. It goes 10 feet and comes back to her. How is this possible?"<<endl;
cout<<"---------------------------------------"<<endl;
cout<<"1. Because the ball has air in it. "<<endl;
cout<<"2. Because the ball went up. "<<endl;
cout<<"3. Because of the gravity."<<endl;
cin>>inputA;
cout<<choose1(inputA)<<endl;
system("PAUSE");
system("cls");
cout<<"How can a man go eight days without sleep?"<<endl;
cout<<"1. Because he is death. "<<endl;
cout<<"2. Because he slept at night. "<<endl;
cout<<"3. Because he is not born yet. "<<endl;
cin>>inputB;
cout<<choose2(inputB)<<endl;
system("PAUSE");
system("cls");
cout<<"What can you never eat for breakfast??"<<endl;
cout<<"1. Breakfast. "<<endl;
cout<<"2. Dinner. "<<endl;
cout<<"3. Lunch. "<<endl;
cin>>inputC;
cout<<choose3(inputC)<<endl;
system("PAUSE");
system("cls");
cout<<"nnnnThank you for playing this simple game K,Thsuyipa Yimchungernnnn"<<endl;
system("PAUSE");
return 0;
}
void message(){
system("color a");
cout<<"===================================================="<<endl;
cout<<"===================================================="<<endl;
cout<<"Hi welcome to this simple math book"<<endl;
cout<<"here we will ask you some simple question and"<<endl;
cout<<"all you have to do is just press the number which nyou think is the right answer"<<endl;
cout<<"===================================================="<<endl;
cout<<"===================================================="<<endl;
cout<<"===================================================="<<endl;
}
int choose1(char input){    
switch(input){
case 1:
cout<<"The answer is wrong.... Better luck next time"<<endl;
break;
case 2:
cout<<"The answer is wrong.... Better luck next time"<<endl;
break;
case 3:
cout<<"The answer is RIGHT.... Because she kick the ball and gravity pull it down ;) "<<endl;
break;
default:
cout<<"You've entered a wrong digit"<<endl;
return 1; //Same thing with a boolean. If it doesn't have a matching case, return 1 to note that is was a failure.
}
}
int choose2(char input){    
switch(input){
case 1:
cout<<"The answer is wrong.... Better luck next time"<<endl;
break;
case 2:
cout<<"The answer is RIGHT.... Because he slept during the night time ;)"<<endl;
break;
case 3:
cout<<"The answer is wrong.... Better luck next time "<<endl;
break;
default:
cout<<"You've entered a wrong digit"<<endl;
return 1; //Same thing with a boolean. If it doesn't have a matching case, return 1 to note that is was a failure.
}
}
int choose3(char input){    
switch(input){
case 1:
cout<<"The answer is wrong.... Better luck next time"<<endl;
break;
case 2:
cout<<"The answer is RIGHT.... ;) "<<endl;
break;
case 3:
cout<<"The answer is wrong.... Better luck next time"<<endl;
break;
default:
cout<<"You've entered a wrong digit"<<endl;
return 1; //Same thing with a boolean. If it doesn't have a matching case, return 1 to note that is was a failure.
}
}

您的choose1函数并不总是返回值,因此这意味着您具有未定义的行为并且您正在获得垃圾结果。

int f()形式的任何函数都必须return整数值。您的switch语句在除默认情况之外的所有情况下都避开了函数中唯一的return

你可能有一个更激进的编译器警告,可以提醒你这种情况,所以如果你犯了这样的错误,打开很多错误并不丢人,这样这些错误就会变得更加明显,你不会浪费时间调试。

基本解决方法如下:

int choose1(char input){    
switch(input){
case 1:
cout<<"The answer is wrong.... Better luck next time"<<endl;
return 1;
case 2:
cout<<"The answer is wrong.... Better luck next time"<<endl;
return 1;
case 3:
cout<<"The answer is RIGHT.... Because she kick the ball and gravity pull it down ;) "<<endl;
return 0;
default:
cout<<"You've entered a wrong digit"<<endl;
return 1; //Same thing with a boolean. If it doesn't have a matching case, return 1 to note that is was a failure.
}
}

使用int来表示实际bool是有问题的,这里的许多重复也是如此。在处理这样的问题时,首先尝试从数据的角度思考,然后如何显示它。例如,定义一个包含简单structstd::vector,该定义问题、可能的答案和正确答案。这最大限度地减少了代码重复,因为您可以编写一个函数来显示其中一个,请求输入并验证答案,而无需一遍又一遍地复制粘贴。

相关内容

最新更新