Saturday, 15 May 2010

C++ Getting input from an external file -



C++ Getting input from an external file -

so have code here:

std::cout << "here's question 2 " << char(156) << "200" << endl; sleep(2000); playsound(text("millionaire/£100play.wav"), null, snd_filename | snd_async | snd_loop); std::cout << "in maths, of these numbers not referred square number?" << endl; sleep(2000); std::cout << "a: 0" << endl; sleep(2000); std::cout << "b: 1" << endl; sleep(2000); std::cout << "c: 2" << endl; sleep(2000); std::cout << "d: 4" << endl; sleep(2000); answerques2: std::cout << "so, a, b, c or d?"; std::cin >> answer2; if (answer2 == "c" || answer2 == "c") { std::cout << "that's correct, you've won " << char(156) << "200!" << endl; playsound(text("millionaire/£100correct.wav"), null, snd_filename); sleep(2000); }

now, code not problem. quiz question , 4 answers (a, b, c , d). in order create more questions, you'd have go code , go through lengthy process edit everything. want create text file can edit questions , answers within text file, hence replacing in code (so example, if wanted alter q1, open text file, replace question , when load program, question changed). how able this?

this total solution, though must fill in rest of existing code. utilize below function, getfilelines, load lines file vector. easy work way. took time adapt char/string since using, though default wstring/wchar_t.

#include <string> #include <vector> #include <fstream> #include <iostream> #include <windows.h> using namespace std; bool fileexists(const std::string& name) { file * file; errno_t result = fopen_s(&file, name.c_str(), "r"); if (result == static_cast<errno_t>(0)) { fclose(file); homecoming true; } else { homecoming false; } } std::vector<std::string> getfilelines(std::string filepath) { vector<string> lines; if (!fileexists(filepath)) homecoming lines; ifstream input(filepath); if (!input.is_open() || input.fail()) homecoming lines; string line; { std::getline(input, line); lines.push_back(line); } while (!input.eof() && !input.fail() && !input.bad()); if (!input.eof() && (input.fail() || input.bad())) throw exception("getfilelines failure"); homecoming lines; } int wmain() { vector<string> quizlines = getfilelines("c:\\quiz.txt"); // replace path file if (quizlines.size() == 5) { string question = quizlines[0]; string answer1 = quizlines[1]; string answer2 = quizlines[2]; string answer3 = quizlines[2]; string answer4 = quizlines[2]; // code begins here std::cout << "here's question 2 " << char(156) << "200" << endl; sleep(2000); playsound(text("millionaire/£100play.wav"), null, snd_filename | snd_async | snd_loop); std::cout << question << endl; sleep(2000); std::cout << "a: " << answer1 << endl; // rest of code changes utilize answer# variables should follow } else { std::cout << "could not load quiz external file. cannot continue." << endl; } }

i recommend read documentation on standard library elements used not familiar with. of these links, ordered mutual utilize first, may useful that:

http://www.cplusplus.com/reference/string/string/

http://www.cplusplus.com/reference/vector/vector/

http://www.cplusplus.com/reference/fstream/ifstream/

and don't pay attending people downwards rating honest question. born world doing headstands seems.

and also, record, exceedingly ~easy~ question answer. why? not because dumb question, because imagine how mutual must seek access file contents. if inquire fundamental question how @ file content, should expect lot of quick total answers, because in case, should on hand. figured out using online searches, of course, though it's not easy figure out pieces documentation should reading.

c++ file function text external

No comments:

Post a Comment