【问题标题】:Stumped on why my code won't output a text?对为什么我的代码不会输出文本感到困惑?
【发布时间】:2018-05-08 18:33:45
【问题描述】:

这是我第一次在这里发帖,如果帖子格式不正确,我深表歉意。我是 C++ 新手,正在寻求帮助。

在大约第 18 行之后,我似乎无法弄清楚是什么阻止了我的代码。它处理第一个 cout 和 cin,但不继续处理下一个 cout?

如果您输入一个键,它将运行 if/then 语句中列出的所有条件并完成。但是,我在这里的目标是让计算机生成一个随机数,然后要求我输入 (Y/N)。给定输入,它要么应该生成另一个随机数,要么结束。

使用编译器不会产生错误,所以我现在有点傻眼了。

如果有任何帮助,我将不胜感激,谢谢。

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

int comp;

using namespace std;
int main ()
{


comp= 1+(rand() % 10);

randomnumber:
string again;

cout <<"The computer chose this random number: "<< comp << endl;
cin >> comp;

cout << "Would you like to run this again? Y/N" << endl;
cin >> again;



if((again == "y")||(again == "Y"))
{
    goto randomnumber;
}

else if((again == "n")||(again == "N"))
{
 cout << "OK" << endl;
}

else if((again != "y")||(again != "Y")||(again != "n")||(again !="N"))
{
 cout << "Please type Y or N" << endl;

}
    return 0;
}

【问题讨论】:

  • 不要使用标签goto for 循环!改用实际的循环。至于你的问题,我建议你learn how to debug your code,因为它会帮助你很容易地找出问题所在。还有更多问题。
  • 最后,要么停止逃课,要么停止猜测事情(这有点像你做的)。 Get a good book or two instead.
  • 在你做任何其他事情之前,看看你是否可以更新你的编译器工具。 #include&lt;iostream.h&gt; 有 20 多年前标准化 C++ 代码的味道。自那时以来,C++ 发生了很大变化,使用旧工具很难找到帮助和毕业后就业。
  • if((again != "y")||(again != "Y")||(again != "n")||(again !="N")) 仔细想想这句话。

标签: c++


【解决方案1】:

首先让我们看一下代码应该如何满足您的要求。

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream.h>
//int comp; No need for comp to be a global variable here.
using namespace std;
int main ()
{
    int comp;
    randomnumber://if you were to use goto, the label should have been here.
    comp= 1+(rand() % 10);
    //randomnumber: avoid goto statements at all if possible.
    //string again; No need for it to be a string. 
    char again;
    cout <<"The computer chose this random number: "<< comp << endl;
    //cin >> comp; since you want computer to generate a random no, why ask the  user for it? 
    cout << "Would you like to run this again? Y/N" << endl;
    cin >> again;
    if((again == "y")||(again == "Y"))
    {
        goto randomnumber;
    }
    else if((again == "n")||(again == "N"))
    {
             cout << "OK" << endl;        
    }
    else if((again != "y")||(again != "Y")||(again != "n")||(again !="N"))
    {
             cout << "Please type Y or N" << endl;

    }
    return 0;
 }

现在让我们看看如何以一种更不复杂、更简单的方式做到这一点。

#include<iostream.h> //#include<iostream> if No such file error.
#include<time.h> // for seeding rand()
#include<stdlib.h> // for rand
using namespace std;
int main()
{
    srand(time(NULL)); //seeding rand()
    while(true)
    {
        int comp=1+(rand() % 10);
        cout <<"The computer chose this random number: "<< comp << endl;
        cout<<"Would you like to run this again?Y/N"<< endl;
        char choice;
        cin>>choice;
        if ((choice == 'N')|| (choice =='n'))
        {
            cout<<"OK"<< endl;
            break;
        }
        else
            while(choice!='y' && choice!='Y') // forcing user to enter a valid input.
            {
                cout<<"Please type Y or N" << endl;
                cin>>choice;
            }
    }
    return 0;
}

我希望这会有所帮助。

【讨论】:

  • 虽然确实是一个很大的改进,但您还没有解决using namespace std; 引起的潜在问题。也不是通过使用模数引入随机数的偏差。这些天也没有更好的随机数工具可用。我还建议提倡nullptr 而不是NULL。不推荐使用的标头的使用也没有得到解决。简而言之:很好的答案,但可能会更好。恕我直言。
  • 顺便说一句 srand(time(NULL)); 是一个可怕的种子。 time() 只有第二个分辨率,所以在同一秒内运行程序的任何人都会得到相同的种子。此外,一天中的时间是相当可预测的,完全基于可猜测的时间来制作种子。
  • 感谢您的建议和示例。我会努力解决我的一些错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 2021-06-21
相关资源
最近更新 更多