Drink machine simulator initializing from txt file issue

I am trying to create a drink machine simulator that reads from a text file called "DrinkMachineInventory.txt" for the input.

The text file must contain the following information:
Coca-Cola,0.75,20
Root Beer,0.75,20
Sprite,0.75,20
Spring Water,0.80,20
Apple Juice,0.95,20

I will not post the whole program, instead I will just post the part relevant to my issue. Can someone help getting this program to read from the txt file and display the information? Here is what I have so far but it is not displaying correct data after compiling.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int MAX_DRINK = 5;

struct DrinkMachine
{
    string drinkName;
    double drinkPrice;
    int drinkQty;
};

void initializeMachine(DrinkMachine []);
int menu(DrinkMachine []);

int main ()
{
    cout << setprecision(2) << fixed;
    DrinkMachine drink[MAX_DRINK];
    initializeMachine(drink);
    menu(drink);
}

void initializeMachine(DrinkMachine drink[])
{
    ifstream inputFile;
    inputFile.open("DrinkMachineInventory.txt");
    
    for(int i = 0; i<MAX_DRINK; i++)
    {
        getline(inputFile, drink[i].drinkName, ',');
        inputFile.clear();
        inputFile.sync();
        
        inputFile >> drink[i].drinkPrice;
        inputFile.clear();
        inputFile.sync();
        
        inputFile >> drink[i].drinkQty;
        inputFile.clear();
        inputFile.sync();
    }
    inputFile.close();
}
  
    
int menu(DrinkMachine drink[])
{
    int choice;
    for(int i = 0; i < MAX_DRINK; i++)
    {
        cout << i+1 << ". " << setw(15) << left << drink[i].drinkName << "-   $" << drink[i].drinkPrice << "   -   "<< drink[i].drinkQty << endl;
    }
    cout << "6. Quit\n\n";
    cout << "Please enter your selection: ";
    cin >> choice;
    return choice;
}
Last edited on
in function initializeMachine you use getline once to read in the drink title, then you use the normal method to read in drinkprice. But the normal method reads until it encounters a whitespace, so it will read in "0.75,20" and etc. You'll need to use a different method.
Right, thats where im getting stuck. Any ideas?
use the getline function again? using the same parameters. Should work as expected, tell me if you encounter some more problems.
I tried that as well, i get a compiler error.
Whats the error?
39 - no matching function for call to `getline(std::ifstream&, double&, char)'
43 - no matching function for call to `getline(std::ifstream&, int&, char)'

when modifying lines 39 to 45 to the following:

getline(inputFile, drink[i].drinkPrice, ',');
inputFile.clear();
inputFile.sync();

getline(inputFile, drink[i].drinkQty, ',');
inputFile.clear();
inputFile.sync();
weird, my guess is getline only reads in strings then. Another method you could use is to read the whole line into a string, then use a for loop to search through the string until a ',' is found and proceed to break off the current string into a separate string using substr();
Topic archived. No new replies allowed.