对于一个任务我写了两个程序。 一个用于生成随机整数文件,另一个用于对小于指定阈值的整数进行计数。 您可以在我发布的代码下找到实际的作业文本。编译两个程序失败
当编译G ++ generate.cpp -o生成,我得到这个错误:
[email protected]:~/assign2$ g++ generate.cpp -o generate
generate.cpp: In function ‘bool writeRand(int, int, int, const char*)’:
generate.cpp:12:31: error: variable ‘std::ofstream fout’ has initializer but incomplete type
ofstream fout (fname);
当我编译G ++ thresh.cpp -o脱粒,我得到这个错误:
[email protected]:~/assign2$ g++ thresh.cpp -o thresh
thresh.cpp: In function ‘int main()’:
thresh.cpp:19:16: error: variable ‘std::ifstream fin’ has initializer but incomplete type
ifstream fin(fname.c_str());
灿任何人都可以帮助我修复我的代码以使其工作?另外我需要为我的项目创建一个Makefile,因为我有多个可执行文件?
非常感谢...有点卡在做什么。
这是我的代码:
generate.cpp
#include <iostream>
#include <cstdlib> // re. atoi
using std::cout;
using std::endl;
using std::cerr;
using std::ifstream;
using std::ofstream;
bool writeRand (const int ranSeed, const int maxVal, const int numVals, const char* fname)
{
ofstream fout (fname);
if (fout)
{
srand (ranSeed);
for (int i=0; i < numVals; ++ i)
fout << rand() % (maxVal+1) << endl;
fout.close();
return true;
}
//else
return false;
}
int main (int argc, char* argv [])
{
if (argc !=5)
{
cerr << "Usage: " << argv[0] << "ranSeed maxVal numVals outFileName" << endl;
return -1;
}
const int ranSeed = atoi(argv[1]);
const int maxVal = atoi(argv[2]);
const int numVals = atoi(argv[3]);
const char* fname = argv[4];
if (ranSeed <= 0 || maxVal <= 0 || numVals <= 0)
{
cerr << "Invalid negative or zero numbers on command line....Try again..." << endl;
return -1;
}
if (writeRand(ranSeed, maxVal, numVals, fname))
cout << "ranSeed = " << ranSeed << ", maxVal = " << maxVal << ", numVals = " << numVals
<< "\nfame " << fname << " was created ok ..." << endl;
else
cout << "There was a problem creating file " << fname << " ..." << endl;
}
thresh.cpp
#include <iostream>
#include <cstdlib> // re. atoi
using std::cout;
using std::endl;
using std::cin;
using std::ifstream;
using std::string;
using std::flush;
int main()
{
//prompt and take in the desired file name
cout << "Enter name of file (for example randNums.txt): " << flush;
string fname;
getline(cin, fname);
//then can open file
ifstream fin(fname.c_str());
if(fin)
{
int max, count = 0, below = 0, val = 0;
string line;
while(true)
{
cout << "Enter the threshold value: " << flush;
getline(cin,line);
max = atoi(line.c_str());
if(max > 0) break;
cout << "Try again with value > 0 \n";
}
while(getline(fin, line))
{
val = atoi(line.c_str());
++count;
if(val < max) ++below;
}
fin.close();
cout << below << " of " << count << " values in file '"
<< fname << "' are less than " << max << '\n';
max = val+1; //last value (in file) + 1
count = 0, below = 0;
fin.open(fname.c_str());
while(getline(fin, line))
{
int val = atoi(line.c_str());
++count;
if(val < max) ++below;
}
fin.close();
cout << below << " of " << count << " values in file '"
<< fname << "' are less than " << max << '\n';
}
else
cout << "There was an error opening file " << fname << '\n';
cout << "Please 'Enter' to continue/exit..." << flush;
cin.get();
return 0;
}
个分配
产生
创建一个名为“生成”程序生成随机整数的文件。 该程序需要四个命令行参数。他们是,为了
*一个随机数种子。这是一个下面解释的整数值。 *最大值。随机值应该小于这个最大值。该值是一个整数。 *要产生值的数量 *的输出文件的名称来存储值
如果命令行参数没有给出,程序应该给出一个简短的消息上的正确用法,然后退出。命令行上的整数值应全部检查为负值。如果给出一个负值,程序应该打印一条错误信息并退出。 随机数的产生
rand()函数在每次调用时返回一个随机正整数。 srand(int)函数接受一个称为随机数种子的整数,用于初始化随机数生成器。随后对rand()的调用产生一个与其种子唯一关联的随机序列。通常在程序中,srand()被调用一次,而rand()被多次调用。
生成一个特定大小的随机数的一种常用技术是使用最大上界的div()的余数。
脱粒
创建一个程序调用脱粒,应要求用户输入一个文件名和一个整数阈值。然后程序应计算文件中小于阈值的值的数量并报告结果。
例如,mydatafile中的300个值中的43个小于17 此程序不应使用命令行参数。相反,应通过提示用户来获取所需的值。
当询问阈值时,如果用户输入一个负值,程序应该打印一个错误并循环请求,直到获得一个合适的值。
如果输入文件不存在,应打印错误消息并退出程序。
提示:
测试你与可以通过手工检查小的值的程序。例如,创建一个20个值小于10的文件。对thresh的特别好的测试是使用数据文件的最后一个值作为阈值。然后使用最后一个值加上一个作为阈值。
**作为提交的一部分,需要Makefile。如果项目有多个可执行文件,则应该有一个默认生成文件规则来构建所有可执行文件。你应该有一个规则来清理项目到一个原始状态。你的Makefile应该使用适当的变量。
你的程序只能有一个main()函数,你有两个。你的错误告诉你。 –
您不应该逐字拷贝分配文本,而是要简洁地描述你做了什么和错在哪里。 – moooeeeep
你想要两个程序 - 生成和thresh - 对吗?然后你需要运行两次g ++,每个程序一次。 – mrunion