I am trying to read from a text file and input the data into a struct. I have read through all the threads on this topic and have not been able to work out where I am going wrong. My text file is:

Plain_Egg 1.45
Bacon_and_Eggs 2.45
Muffin .99
French_Toast 1.99
Fruit_Basket 2.49
Cereal .69
Coffee .50
Tea .75

My code:

// Session9.cpp
//

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

using namespace std;

const int NUM_SIZE  = 7;

struct menuItemType
{
    string menuItem;
    double menuPrice;
};

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


int main()
{
    ifstream inData;

    menuItemType menuList[7];

    inData.open("menu.txt");

    getData(inData, menuList, NUM_SIZE);

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

    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;

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

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

}

Attached is the output I get.

I am not sure how C++ reads in the text file. Do I need to put commas inbetween the data? From the output I see that the first string data isn't being read at all. I am guess the second isn't either. I just need a subtle hint as to what I am doing wrong please.

Recommended Answers

All 3 Replies

Firstly, you have eight menuItems to read, and eight menuPrices, so you should be making an array of size 8 rather than 7.

Otherwise, nothing here that should stop your code working. If I had to guess, your file menu.txt is not in the same directory that the executable is running in, so it can't find it, so it doesn't open it.

You need to put menu.txt in the same directory as the executable, or replace inData.open("menu.txt"); withinData.open("the/complete/directory/path/to/the/file/menu.txt");

I've tested your code (with GCC running under Code::Blocks) and found it to work, which indicates to me that the menu.txt file isn't in the same directory executable. Add the following test to you code right after the open() call:

    if(inData.fail())
    {
        cout << "Could not open menu file. Stopping.";
        exit(-1);
    }

(And add #include <cstdlib> to the start of the program.) This should tell you if it is reading the menu file at all.

Thanks for the input. I had the if(inData.fail()) in there at one time. I kept moving the open commands back and forth from main to the function and lost it somewhere. If I run the executable it works because the file is there but if I run debug it does not work.

I finally found the right folder to put the file for debug and everything is working. Thanks again.

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.