Thanks to Muschops and Schol-R-LEA I finally got my program working. However I decided to change my file format and replace the underlines with spaces and separate the data with a comma. I open the file and use getline to grab the data but I have an error that reads:

IntelliSense: no instance of overloaded function "getline" matches the argument list
argument types are: (std::ifstream, std::string, const char [2])

Here is my code:

// Session9.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>

using namespace std;

const int NUM_SIZE  = 8;

struct menuItemType
{
    string menuItem;
    double menuPrice;
};

void myPause();
void getData(ifstream& indata, menuItemType menu[], int listSize);
void showMenu(menuItemType menu[], int listSize);


int main()
{
    ifstream inData;

    menuItemType menuList[8];

    inData.open("menu.dat");

    if(inData.fail())
    {
        cout << "Error opening file." << endl;
        myPause();
        return 1;
    }

    getData(inData, menuList, NUM_SIZE);

    inData.close();
    inData.clear();

    showMenu(menuList, NUM_SIZE);

    myPause();

    return 0;
}

// Function to pause the program to view window contents
void myPause()
{
    cout << "Press ENTER to continue..."; 
    cin.clear();
    cin.sync();
    cin.get();
}
// Function to store data into struct
void getData(ifstream& indata, menuItemType list[], int listSize)
{
    int index;
    string menu;
    double price;

    for(index=0; index < listSize; index++)
    {
        //indata >> list[index].menuItem;
        //indata >> list[index].menuPrice;
        getline(indata, list[index].menuItem, ",");
        getline(indata, list[index].menuPrice, ",");
    }
}
// Function to display menu
void showMenu(menuItemType list[], int listSize)
{
    int index;

    for(index=0; index < listSize; index++)
    {
        cout << list[index].menuItem << ", " << list[index].menuPrice << endl;
    }
}

You can see the commented out code I was using to get the data from the file in the fuction getData. I searched through several books as well as this forum and I cannot understand what I am doing wrong. Any suggestions would be appreciated. Thanks.

Recommended Answers

All 2 Replies

line 72, 73: The delimiter should be a char not a string. ','

I made the changes and the error went away for the first getline but stuck on the second. After playing around I realized there was no second comma on the line. I changed the code to:

for(index=0; index < listSize; index++)
    {
        getline(indata, list[index].menuItem, ',');
        indata >> list[index].menuPrice;
    }

Works great, thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.