I am in need of an explanation on how to pass information from my input file to my program. This is what I'm trying:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct BevInfo
{
       char bevName[11];
       float bevPrice;
       int bevLeft; 
};

int main(int argc, char *argv[])
{
    BevInfo drink[5];
    
    fstream myfile;
    myfile.open("machine.txt", ios::in | ios::out);
    myfile.getline(drink[0].bevName, 11);
    myfile >> drink[0].bevPrice;
    myfile >> drink[0].bevLeft;
    
    myfile.close();
    
    cout << drink[0].bevName << endl;
    cout << drink[0].bevPrice << endl;
    cout << drink[0].bevLeft << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

This is my input File:

Coca-Cola 	0.75	20
Root Beer	         0.75	20
Sprite		0.75	20
Spring Water	0.80	20
Apple Juice	0.95	20

Why is it not working?

instead of using getline(), you should continue your use of the >> extraction operator. unlike getline(), the extraction operator depends on 'white space' delimiters.

the way you are doing it now, you are trying to do is to store the entire first line into bevName :s

Edited 6 Years Ago by Clinton Portis: n/a

I understand that, but I only know how to use the >> operation for numbers, how would I get the char filled with the drink name without getline?

Ifstream's extraction operator is overloaded to handle every native c++ datatype, to include char's.

In your implementation, you should be using >> data extraction for everything.

This article has been dead for over six months. Start a new discussion instead.