Cartino
Cartino

Reputation: 41

Inputting two word string from a file (C++)

I have a .txt file in which I have to read in multiple items and then output them in a console. Here's a sample of a line from the text file

int - string - int - float - float - string - char*

32073 Stationary Bike 60 135 490.9 moderate Tue Apr 17 16:53:46

My issue here is mainly getting the second item, the string, to be input despite the space between the two words. It's also worth noting sometimes the string there is only one word. Right now it seems like choiceName is taking the entire line without stopping at a space like I thought it would.

I'm only getting a single line from the text file at a time outputted to the console instead of everything in the file. I need to hit enter to get the next line which I don't want,

    while(!(transactionLog >> idNum).eof())
    {
        getline(transactionLog, choiceName, ',');
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');

        transactionLog >> minutes >> weightPoundsCopy >> calories >> intensity;
        getline(transactionLog, timeStamp, '\n');

        std::cout << std::setfill('0') << std::setw(5) << idNum << choiceName << " " << minutes << " " << weightPoundsCopy << " " << calories << " " << intensity << " " << timeStamp << std::endl;
    }

Upvotes: 1

Views: 162

Answers (1)

Zan Lynx
Zan Lynx

Reputation: 54325

Your code is stuck waiting for Enter because you use std::cin instead of transactionLog.

Upvotes: 1

Related Questions