不知道什么问题,求指教

lovepal4 2011-02-20 02:17:01
问题看起来像要输入数据的现象,但是那个光标是死的
而且程序中并没有要输入数据

用F10一句一句检查,发觉程序停在一个函数的执行语句上,但是不进入这个函数。
这句前面句是if条件,判断没问题,然后转到这句,忽然把本在后面的控制台窗口调前,一个光标死在那里,然后就不动了
...全文
102 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuyuyu101 2011-02-20
  • 打赏
  • 举报
回复
应该是陷入死循环了
arong1234 2011-02-20
  • 打赏
  • 举报
回复
所谓到处是cin,不是说你到处定义“不同的”cin,而是说cin的函数在不同的地方到处调用。

找问题是自己的事情,而不是别人帮你去找。别人给你建议,教你去找,我给你说的方法你试了么?

[Quote=引用 6 楼 lovepal4 的回复:]
没有到处是cin,就一个cin啊,其他是调用cin的成员函数
有人帮试下找找问题没?
[/Quote]
lovepal4 2011-02-20
  • 打赏
  • 举报
回复
没有到处是cin,就一个cin啊,其他是调用cin的成员函数
有人帮试下找找问题没?
qq120848369 2011-02-20
  • 打赏
  • 举报
回复
[code=C]换个试试[/code]
qq120848369 2011-02-20
  • 打赏
  • 举报
回复
[code=C]测试一下好不好用[/code]
arong1234 2011-02-20
  • 打赏
  • 举报
回复
觉得你的代码到处是cin,很可能真在读,有时候调试器定位的代码不准确,因此你看到不在cin那儿。建议在每个输入前输出一行信息,提示你需要输入啥
lovepal4 2011-02-20
  • 打赏
  • 举报
回复
代码我贴出来,分了3个文件

//powerball.cpp
#include <iostream>
#include <string>
#include "Functions.h"
using namespace std;

int main()
{
cout <<"Powerball Demo Program"<<endl
<<"This program simulates the powerball game. User can choose a powerball "
<<"(range from 1 to 42) and five white balls (range from 1 to 55, no repeat)" <<endl;
int userBall[6];
int systemBall[6];
bool checkmate;

string ans;
do
{
bool valid=AskForPowerball(userBall);
if (valid)
{
SystemPowerball(systemBall); //到这句就不动了,就像进入了死循环
checkmate=CheckMatch(userBall, systemBall);
WriteCheck(checkmate, userBall, systemBall);
}
cout <<"\nDo you want to play again? (yes/no) ";
getline(cin, ans);
}while (ans=="yes");

return 0;
}

//Functions.h
bool AskForPowerball(int userBall[], int max=6);
void SystemPowerball(int systemBall[], int max=6);
bool CheckMatch(int userBall[], int systemBall[], int max=6);
void WriteCheck(bool check, int userBall[], int systemBall[], int max=6);

//Functions.cpp
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;

bool AskForPowerball(int userBall[], int max=6)
{
cout <<"\nPlease enter your 6 Powerballs in order (red first, seperated with space)\n";
for (int i=0; i<max; i++)
cin >>userBall[i];
if (cin.fail())
{
cin.clear();
cin.ignore(1024, '\n');
cout <<"Invalid Input!"<<endl;
return false;
}
cin.ignore(1024, '\n');

//检查红球和白球是否超出范围,白球是否重复
if (userBall[0]<1||userBall[0]>42)
{
cout <<"\nRed powerball out of range!"<<endl;
return false;
}
bool checkWhite[56]; //用check array 检查重复,也可以排列后比较相邻检查重复
for (int i=0; i<56; i++)
checkWhite[i]=false;
for (int i=1; i<max; i++)
{
if (userBall[i]<1||userBall[i]>55)
{
cout <<"\nWhite powerball out of range!"<<endl;
return false;
}
if (!checkWhite[userBall[i]])
checkWhite[userBall[i]]=true;
else
{
cout <<"\nRepeated white ball "<<userBall[i]<<endl;
return false;
}
}

return true;
}

void SystemPowerball(int systemBall[], int max=6)
{
srand((unsigned)time(NULL));
//red ball
systemBall[0]=rand()%42+1;
//five white ball
bool checkWhite[56];
for (int i=0; i<56; i++)
checkWhite[i]=false;
for (int i=1; i<max; i++)
{
bool again=true;
do
{
systemBall[i]=rand()%55+1;
if (!checkWhite[systemBall[i]])
{
checkWhite[systemBall[i]]=true;
again=false;
}
}while(again=true);
}
}

bool CheckMatch(int userBall[], int systemBall[], int max=6)
{
//check powerball match first
if (userBall[0]!=systemBall[0])
return false;
//white ball rank
for (int i=1; i<max-1; i++)
for (int j=2; j<max; j++)
{
int temp1;
if (userBall[j-1]>userBall[j])
{
temp1=userBall[j-1];
userBall[j-1]=userBall[j];
userBall[j]=temp1;
}
if (systemBall[j-1]>systemBall[j])
{
temp1=systemBall[j-1];
systemBall[j-1]=systemBall[j];
systemBall[j]=temp1;
}
};
//check white ball match
for (int i=1; i<max; i++)
if (userBall[i] != systemBall[i])
return false;

return true;
}

void WriteCheck(bool checkmate, int userBall[], int systemBall[], int max=6)
{
cout <<"\nUser selected balls:"
<<"\n powerball: "<<userBall[0]
<<"\n white ball: ";
for (int i=1; i<max; i++)
cout <<setw(5)<<userBall[i];
cout <<endl;
cout <<"\nSystem selected balls:"
<<"\n powerball: "<<systemBall[0]
<<"\n white ball: ";
for (int i=1; i<max; i++)
cout <<setw(5)<<systemBall[i];
cout <<endl;
if (checkmate)
cout <<"\nCongratulations! your powerball and white balls match the system!"<<endl;
else
cout <<"\nSorry, your balls don't match the system."<<endl;
}
mstlq 2011-02-20
  • 打赏
  • 举报
回复
无代码的话,大家也猜不出所以然来
请楼主google“提问的智慧”

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧