0

OK so i know the question is a bit confusing (dont downvote right away, let me explain...)

I have a text file like this:

dim
coins
oponent

I want to read this text file but while reading it, ask the user for specific responses, for example:

"reads line with the word dim" -> asks user the dimensions -> next line -> "reads line with coins" -> asks user how many coins and so forth until the EOF.

Is there anyway to do this? if yes, can you show me how?

Thanks and plz dont downvote, just tell me what's wrong and i will edit the post..

EDIT: This is the way i'm reading the file and asking the user input

void LeitorFich::lerFicheiro(string nomeFich)
{
    int i, j, t;
    string linha, nome, nome1;
    ifstream fich(nomeFich);

while(getline(fich, linha))
{
    istringstream iss(linha);

    iss >> nome;

    if(nome == "dim")
    {
        cout << nome << " ";
        iss >> i >> j;
    }
}
cin.get();

fich.close();
}
F.Dinis
  • 33
  • 8

1 Answers1

1

A simple example will look like this:

Consider I have a file called "test.txt" which contains the very content as yours

string sLine;

ifstream inFile("test.txt");

int Dim1, Dim2, coins, oponent;

while(inFile >> sLine ){
    if("dim" == sLine){
        std::cout << "Dim1: ";
        std::cin >> Dim1;
        std::cout << std::endl;

        std::cout << "Dim2: ";
        std::cin >> Dim2;
        std::cout << std::endl;
    }
    else
        if("coins" == sLine){
            std::cout << "coins: ";
            std::cin >> coins;
        }
    else
        if("oponent" == sLine){
            std::cout << "oponent: ";
            std::cin >> oponent;
        }
}

inFile.close();
Raindrop7
  • 3,805
  • 3
  • 14
  • 27