我要做的是创建一个(看似)随机事实生成器,方法是选择一个包含事实的随机 .txt 文件并将其输出到屏幕上。这是代码:
#include <fstream>
#include <iomanip>
#include <iostream>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include "getQuestion.h"
using namespace std;
int main() {
int mainMenuChoice;
ifstream Bibliography;
//string easyBib;
char randomQuestion;
string easyBib;
string Questions /*[15]*/;
string Answers /*[15]*/;
ifstream inputFile;
//char newline = '\n';
//char chars;
//int linenum = 1;
string line;
char randomQ;
//cout << "%s by Noah" << endl;
cout << "Quiz Menu\n\n";
cout << "1. Play Game!\n";
cout << "2. Bibliography\n";
cout << "3. Developer Info\n";
//cout << "4. Admin Menu\n";
cout << "4. Exit\n";
cout << "Menu Choice: ";
cin >> mainMenuChoice;
switch (mainMenuChoice) {
case 1: {
srand ( time(NULL) );
randomQuestion = rand() % 10 + 1;
randomQ = '0' + randomQuestion;
randomQ = randomQ + '.txt';
inputFile.open(randomQ);
getline(inputFile, line);
vector<string> lines;
for( string line; getline(inputFile,line); )
{
lines.push_back(line);
}
size_t iRandLine = rand() % lines.size();
string randomLine = lines[iRandLine];
cout << "Line " << (iRandLine+1) << ": " << randomLine << endl;
break;
}
case 2: {
inputFile.open("Bib.txt");
//istream& getline (char* s, streamsize n, char \n )
inputFile >> easyBib;
cout << easyBib << endl;;
return(0);
}
case 3: {
cout << "Program made by: XXXX XXXXXXXX" << endl;
cout << "Mrs. XXXXXXX's Period 4 Social Studies Class" << endl;
break;
}
case 4: {
cout << "Thank you for playing!" << endl;
return(0);
}
default: {
cout << "Sorry, Invalid Choice!\n";
return(2);
}
}
return(0);
}
我也收到此编译器错误:
52: error: invalid conversion from 'char' to 'const char*'
52: error: initializing argument 1 of 'void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]'
谢谢大家!谢谢。
如果适用,我使用的是 XCode 3.2.6,和 Mac OS X 10.6.8 请不要要求我升级或下载任何东西,这是一台学校笔记本电脑,我没有 sudo 权限。