简体   繁体   中英

C++ Getting input from an external file

So I have this code here:

std::cout << "Here's Question 2 now for " << char(156) << "200" << endl;
Sleep(2000);

PlaySound(TEXT("Millionaire/£100Play.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
std::cout << "In maths, which of these numbers is not referred to as a 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, the code itself is not the problem. What this is essentially is a quiz with a question and then 4 answers (A, B, C and D). Now in order to actually make up more questions, you'd have to go into the code itself and go through a lengthy process to edit everything. I want to make a text file that you can edit the questions and answers inside the text file, hence replacing everything in the code (So for example, if I wanted to change Q1, I could open the text file, replace the question and when I load up the program, the question will be changed). How would I be able to do this?

This is a full solution, though you must fill in the rest of your existing code. I personally use the below function, GetFileLines, to load lines from a file into a vector. Easy to work with that way. I took the time to adapt it to char/string since that is what you are using, though I default to 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);
        return true;
    }
    else {
        return false;
    }
}

std::vector<std::string> GetFileLines(std::string filePath)
{
    vector<string> lines;

    if (!FileExists(filePath))
        return lines;

    ifstream input(filePath);
    if (!input.is_open() || input.fail())
        return lines;

    string line;

    do {
        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");

    return lines;
}


int wmain() {

    vector<string> quizLines = GetFileLines("c:\\quiz.txt"); // replace with path to your 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];

        // Your code begins here
        std::cout << "Here's Question 2 now for " << 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 your code with changes to use answer# variables should follow
    }
    else {
        std::cout << "Could not load quiz from external file. Cannot continue." << endl;
    }
}

I recommend you read some documentation on this standard library elements I used that you are not familiar with. Any of these links, ordered by most common use first, may be useful to you for 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 attention to people down rating an honest question. Some were born into this world doing headstands it seems.

And also, for the record, this was an exceedingly ~easy~ question to answer. Why? Not because it was a dumb question, but because imagine how common it must be to try to access file contents. So if you ask a fundamental question like how do I get at that file content, you should expect a lot of quick full answers, because like in my case, they should be on hand. Could it be figured out using online searches, of course, though it's not always easy to figure out the pieces whose documentation you should be reading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM