2017-07-14 166 views
-2

一切都像我想要的那样工作,但唯一的是,当我执行这个文件时,它会在行尾产生一些随机数,我认为这是不正常的,我知道什么是错的这个代码,任何人都可以看看这个代码,并告诉我什么是错在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<<"\n\n\n\nThank you for playing this simple game K,Thsuyipa Yimchunger\n\n\n\n"<<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. 
    } 
} 
+2

该行?哪条线?这里有很多线条,我敢打赌,99%的线条与你的问题无关。 – tadman

+0

这里的随机数是屏幕截图http://i.imgur.com/nnFhMaE.png – user8286839

+0

注意'return'语句在你的选择函数中。这是非法的代码。具有非'空号'返回类型的函数必须在所有路径上返回一个值。 – user4581301

回答

0

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是有问题的,因为很多的重复这里。当接近这样的问题时,首先尝试考虑数据,然后再如何显示它。例如,定义一个std::vector,其中包含一个简单的struct,它定义了一个问题,可能的答案和正确的答案。这可以最大限度地减少代码重复,因为您可以编写一个函数来显示其中的一个函数,要求输入并验证答案,而不必一遍又一遍地复制粘贴。

+0

先生,我是新的C++和我刚刚开始学习这种编程语言,所以如果你可以针指出并粘贴正确的代码,我将不胜感激 – user8286839

+0

我已经在这里添加了一些更多的澄清,但这不是替代很好的C++入门书和一些时间阅读基础知识。 C++是一种冷酷无情的语言,它会*完全*你所要求的而不管后果如何,所以在编写代码时要非常小心,以确保你正确地做到了,即使这意味着经常咨询你的参考资料。就像电锯一样,它非常强大,但如果你没有密切关注你如何使用电锯,它也是不安全的。 – tadman

+1

谢谢主席先生,我从你的评论中学到了很多东西,并在上面发布了答案。我会尽量减少我的代码,以便我不必每次都复制粘贴问题。谢谢你,tadman有一个美好的一天..... – user8286839