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