| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 820 人关注过本帖
标题:[求助]C++编程题,随机函数无法执行.
只看楼主 加入收藏
ton83668698
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-6-30
收藏
 问题点数:0 回复次数:4 
[求助]C++编程题,随机函数无法执行.

本程序中的随机函数无法执行,不知道哪里出错了,希望高手们帮帮忙!多谢咯!!!#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
using namespace std;
void search_1();
void search_2();
void add();
void test();
int B,Y;
void kaitou();
void te_1();
void te_3();
void te_2();
void te_4();
int big();
void random();
string chinese;
string english;
struct word
{
char english[20];
char chinese[20];
};
int i;
word wor[100];
int main()
{
kaitou();
return 0;
}

void kaitou()
{
cout<<" ** ** "<<endl;
cout<<" ** ** ** ** ** ** ** ** "<<endl;
cout<<" **** ^_^ 欢迎来到单词记忆轩 ^_^ **** "<<endl;
cout<<"** ** ** ** ** **"<<endl;
cout<<"* * * 1.中文译英文 * * *"<<endl;
cout<<"** * 2.英文译中文 * **"<<endl;
cout<<" * * 3.添加新词 * * "<<endl;
cout<<" ** ** * 4.记词测试 * ** ** "<<endl;
cout<<" * * 5.退出系统 * * "<<endl;
cout<<" ** ** * ** ** "<<endl;
cout<<" ** * ** "<<endl;
cout<<" * * * * * "<<endl;
cout<<" *** "<<endl;

cout<<"请选择所需要的功能"<<":"<<endl;
char a;
cin>>a;
switch(a)
{
case'1':search_1();break;
case'2':search_2();break;
case'3':add();break;
case'4':test();break;
case'5':jiewei();break;
default:cout<<"请重新输入 ";break;
}
}


void add()                //添加模块

{
void fan();
ofstream outfile("w1.txt",ios::app);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
cout<<"输入中文和英语"<<endl;
cout<<"请输入想输入单词的个数n"<<endl;
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"请输入第"<<i+1<<"个单词"<<endl;

cin>>wor[i].chinese;
outfile<<wor[i].chinese<<endl;
cin>>wor[i].english;
outfile<<wor[i].english<<endl;
}
outfile.close();
cout<<"单词输入完毕"<<endl;
fan();
}
void fan()
{
cout<<"请选择继续添加单词(Y/N)?"<<endl;
char b;
cin>>b;
if (b=='y')add();
else if(b=='n')main();
else abort();

}


void search_1()        //查询模块
{
cout<<"请输入您要查询的中文:";
cin>>chinese;
ifstream infile("w1.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<100;i++)
{
infile>>wor[i].chinese;
infile>>wor[i].english;
if(chinese==wor[i].chinese)
cout<<wor[i].english<<endl;
}
cout<<"请选择您希望的选项:"<<"1.继续"<<"2.退出"<<endl;


int b;
cin>>b;
switch(b)
{
case 1:search_1();break;
case 2:kaitou();break;
default:cout<<"对不起,您的输入有误"<<endl;
}
}
void search_2()
{
cout<<"请输入您要查询的英文:";
cin>>english;
ifstream infile("w1.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int j=2;j<100;j++)
{
infile>>wor[j].english;
if(english==wor[j].english)
cout<<wor[j-1].english<<endl;
}
int f;
cout<<"请选择您希望的选项:"<<"1.继续"<<"2.退出"<<endl;
cin>>f;
switch(f)
{
case 1:search_2();break;
case 2:kaitou();break;
default:cout<<"对不起,您的输入有误"<<endl;
}
}
void jiewei()             //退出模块
{cout<<"谢谢大家光临本单词记忆系统"<<endl;
cout<<"希望大家多提宝贵意见"<<endl;
cout<<"欢迎大家再次光临"<<endl;
}
void test()                //测试模块
{
ifstream infile("w1.txt",ios::in);
if(! infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<20;i++)
{
infile>>wor[i].chinese;
infile>>wor[i].english;
}
infile.close();
cout<<"测试开始咯!"<<endl;
cout<<"请选择"<<":"<<"1.输出英语译成汉语"<<"2.输出汉语译成英语"<<"3.随机英译汉测试"<<"4.随机汉译英测试"<<"5.回到主菜单"<<endl;
int p;
cin>>p;
switch(p)
{
case 1:te_1();break;
case 2:te_2();break;
case 3:te_3();break;
case 4:te_4();break;
case 5:kaitou();break;
default:cout<<"对不起,您的输入有误"<<endl;
}
}

void te_1()          //顺序测试模块开始
{
int j=0;
ifstream infile("w1.txt",ios::in);
if(! infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
int n;
for(int i=1;i<20;i++);
{
infile>>wor[i].english;
infile>>wor[i].chinese;
}
infile.close();
cout<<"请输入你要测试的单词个数"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"第"<<i+1<<"个单词是"<<endl;
cout<<wor[i].english<<" ";
cout<<endl;
cin>>chinese;
if(chinese==wor[i].chinese)
cout<<"棒极啦!"<<endl;
else
{
cout<<" 错了没关系"<<endl;
cout<<"正确答案是"<<endl;
cout<<wor[i].chinese<<endl;
}
if(wor[i].chinese!=chinese) j++;
}
cout<<"测试结束"<<endl;
cout<<"您答错的单词个数有"<<endl;
cout<<j<<endl;
cout<<"1.再来"<<"2.后退"<<"3.回到主菜单"<<endl;
int a;
cin>>a;
switch(a)
{
case 1:te_1();break;
case 2:test();break;
case 3:kaitou();break;
default:cout<<"对不起,您的输入有误"<<endl;
}
}
void te_2()
{
int j=0;
ifstream infile("w1.txt",ios::in);
if(! infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
int n;
for(int i=0;i<20;i++);
{
infile>>wor[i].english;
infile>>wor[i].chinese;
}
infile.close();
cout<<"请输入你要测试的单词个数"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"第"<<i+1<<"个单词是"<<endl;
cout<<wor[i].chinese<<" ";
cout<<endl;
cin>>english;
if(english==wor[i].english)
cout<<"棒极啦!"<<endl;
else
{
cout<<" 错了没关系"<<endl;
cout<<"正确答案是"<<endl;
cout<<wor[i].english<<endl;
}
if(wor[i].english!=english) j++;
}
cout<<"测试结束"<<endl;
cout<<"您答错的单词个数有"<<endl;
cout<<j<<endl;
cout<<"1.再来"<<"2.后退"<<"3.回到主菜单"<<endl;
int a;
cin>>a;
switch(a)
{
case 1:te_2();break;
case 2:test();break;
case 3:kaitou();break;
default:cout<<"对不起,您的输入有误"<<endl;
}
}
void te_3()            //随机模块开始
{
ifstream infile("w1.txt",ios::in);
if(! infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<100;i++)
{
infile>>wor[i].english;
infile>>wor[i].chinese;
}cout<<endl;
string m;int n;
for( i=0;i<100;i++)
{
if(wor[i].chinese=="")
{B=i;break;}
}//return B;


for(int j=0;;j++)
{
srand(time(NULL));
int number=rand()%B+1;
cout<<"随机数为:"<<number<<endl;
if(wor[number].chinese=="")kaitou();
else cout<<"※"<<wor[number].chinese<<"※"<<endl;
cout<<"请输入该中文的英语单词"<<endl;
cin>>m;
if(m==wor[number].english)
{cout<<"恭喜你,回答正确"<<endl;}
else
{cout<<"很抱歉,回答错误"<<endl;}
cout<<" 请选择 "<<endl;
cout<<"1:继续测试。2:返回菜单"<<endl;
cin>>n;
if(n==1)continue;
if(n==2)break;
}kaitou();
}
void te_4()
{
string k;int n;
for(int i=0;i<100;i++)
{
if(wor[i].chinese=="")
{B=i;break;}
}
ifstream infile("w1.txt",ios::in);
if(! infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(i=0;i<100;i++)
{
infile>>wor[i].chinese;
infile>>wor[i].english;
}cout<<endl;

for(int j=0;;j++)
{
srand(time(NULL));
int number=rand()%B+1;
cout<<"随机数为:"<<number<<endl;
if(wor[number].english=="")kaitou();
else cout<<"※"<<wor[number].english<<"※"<<endl;
cout<<"请输入该中文的英语单词"<<endl;
cin>>k;
if(k==wor[number].chinese)
{cout<<"恭喜你,回答正确"<<endl;}
else
{cout<<"很抱歉,回答错误"<<endl;}

cout<<" 请选择 "<<endl;
cout<<"1:继续测试。2:返回菜单"<<endl;
cin>>n;
if(n==1)continue;
if(n==2)break;
}kaitou();
}

搜索更多相关主题的帖子: void 随机函数 include int string 
2007-07-06 23:04
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
晕,好长,楼主最好把你的疑问总结下再归纳成小点。

这么长,谁有耐心看?

呵呵~

Fight  to win  or  die...
2007-07-07 00:24
Arcticanimal
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:341
专家分:20
注 册:2007-3-17
收藏
得分:0 

int big();
void random(); //这是做什么用的?你调用系统函数不需要自己在这里声明,而且random()是返回 unsigned int
string chinese;
这样的代码看的我都心疼自己的眼睛
编译了一下,没什么错误


try new catch
2007-07-07 09:14
天空の城
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2007-7-1
收藏
得分:0 
random不是系统函数。。。而是rand(),并且不用声明,包含头文件就好。。。

2007-07-07 12:55
caixingxin
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-7-7
收藏
得分:0 

#include <cstdlib>

2007-07-07 13:01
快速回复:[求助]C++编程题,随机函数无法执行.
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019600 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved