Thread: Reading a string from an input file into a structure

  1. #1
    Maxwell25
    Guest

    Reading a string from an input file into a structure

    Does anyone know how to read a whole line into a structure?
    I'm trying getline and failing.

    I'm getting sci. notation. Why doesn't this code work? Thanks.

    The text file includes:
    Plain Egg
    1.45
    Bacon and Egg
    2.45


    Code:
    void getData(menuItemType menuList [], ifstream& inFile)
    {
    	
    	int counter=0;
    	while (inFile)
    	{
    		getline(inFile,menuList[counter].menuItem);
    		getline(inFile,menuList[counter].menuPrice);
    		counter++;
    	}
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    what types are menuItem and menuPrice? getline() only reads into strings. If they aren't strings then the code won't work. Also, you might have an off by one error in the number of items you read in from the file given the conditional of the loop, maybe not, depending how the file is written.

  3. #3
    Maxwell25
    Guest
    Thanks for the reply. menuItem is a string and menuPrice is a double. I changed the second read to inFile>>

    I didn't understand the second part of your post. The new code:
    Code:
    void getData(menuItemType menuList [], ifstream& inFile)
    {
    	
    	int counter=0;
    	while (inFile)
    	{
    		getline(inFile,menuList[counter].menuItem);
    		inFile>>menuList[counter].menuPrice; 
    		counter++;
    	}
    }
    the output is now
    (1) Plain Egg 1.45
    (2) -9.25596e+061
    (3) -9.25596e+061
    (4) -9.25596e+061
    (5) -9.25596e+061
    (6) -9.25596e+061
    (7) -9.25596e+061
    (8) -9.25596e+061

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    try while (!inFile.eof()) ...or post more code, so we can test run it...

  5. #5
    Maxwell25
    Guest
    Ok, here it is:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cassert>
    #include <iomanip>
    using namespace std;
    struct menuItemType
    {
    	string menuItem;
    	double menuPrice;
    };
    
    struct custItemType
    {
    	string menuItem;
    	double menuPrice;
    };
    
    void getData(menuItemType menuList[], ifstream& inFile);
    void showMenu(menuItemType menuList[], custItemType custItem[]);  
    //void printCheck(void);
    int main()
    {	
    	menuItemType menuList[8];
    	custItemType custItem[25];
    	ifstream inFile;
    	ofstream outFile;
    	inFile.open("c:breakfast.txt");
    	if(!inFile)
    	{
    		cout<<"Cannot open input file."<<endl;
    		return 1;
    	}
    	//assert(inFile);
    	outFile.open("c:Menu.out");
    	assert(outFile);
    	
    	getData(menuList, inFile);
    	showMenu(menuList, custItem);
    	inFile.close();
    	outFile.close();
    	return 0;
    }
    void getData(menuItemType menuList [], ifstream& inFile)
    {
    	
    	int counter=0;
    	while (inFile)
    	{
    		getline(inFile,menuList[counter].menuItem);
    		inFile>>menuList[counter].menuPrice;
    		counter++;
    	}
    }
    
    void showMenu(menuItemType menuList[], custItemType custItem[])
    {
    	  
    	int g=0;
    	int counter=0;
    	int num=0;
    	int i=0;
    	for (counter=0; counter<8; counter++)
    	{
    		cout<<"("<<counter+1<<")  "<<menuList[counter].menuItem<<"  "<<menuList[counter].menuPrice<<endl;
    	}
    	cout<<endl;
    	
    	cout<<"Enter item's # [1-8] (0 to stop entering): ";
    	cin>>num;
    		cout<<"  "<<menuList[num-1].menuItem<<endl;
    		custItem[i].menuItem==menuList[num].menuItem;
    		i++;cout<<i<<" items. ";
    	while (num!=0)
    	{	
    		cout<<"(inside loop) Enter item's number [1-"<<counter<<"] (0 to stop entering): ";
    		cin>>num;
    		//cout<<"  "<<menuList[num-1].menuItem<<endl;
    		custItem[i].menuItem==menuList[num].menuItem;
    		i++;
    		cout<<custItem[0].menuItem<<i<<" items. ";
    
    	}
    	cout<<"First Item Entered: "<<custItem[0].menuItem<<endl;
    	for (g=1; g<i; g++)
    		cout<<custItem[g].menuItem;
    
    }

  6. #6
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Exclamation

    First of all: I donīt use stream classes, but Iīll try to help anyway!
    Second:

    here:
    inFile>>menuList[counter].menuPrice;
    you are reading char from the file, I mean, the data within the file is char. So, between chars and doubles there is a huge difference(7 bytes if Iīm not mistaken). First you should create a program that stores the data within a file. So, the data written will be doubles...(and strings).

    I hope that you understood me... (like I always say: sorry for the bad english, too lazy to learn it !).
    Nothing more to tell about me...
    Happy day =)

  7. #7
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Question

    Five seconds and I had a better ideia... if wonīt compromise your logic you could change that double to string too... it worked here!
    The output for your program was:

    (1)Plain Egg
    (2)1.45 Bacon
    (3)and Egg
    (4)2.45
    (5)
    (6)
    (7)
    (8)

    Now note that Bacon stays in number two. This is because

    inFile>>menuList[counter].menuPrice;

    just reads until the first blank space.
    So... You could use Plain_Egg(with the underline), or you could use something less "beatiful" like read and write. So you would change string to char []. And always read the same amount of bytes. The problem is that this wastes memory. But I donīt know better solutions, sorry
    Last edited by gustavosserra; 04-25-2003 at 11:50 PM.
    Nothing more to tell about me...
    Happy day =)

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    like someone said, you should try
    while(!infile.eof())

    but also, you could do this:
    while(!infile.eof() && !infile.fail());



    it might work, but if it doesn't it might be a good idea to keep it in there anyways, but in the end, it's your call...

  9. #9
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Thumbs down

    I still think that the problem is the struct that contains a double instead of a string. Iīve used the same loop as maxwell, I just changed the double to a string and everthing runs well...
    Nothing more to tell about me...
    Happy day =)

  10. #10
    Maxwell25
    Guest
    Ok, I've changed the double to a string. Thanks everyone.

    Then how could I add the amounts, to calculate a bill? Would static_cast work?

    I try while(!infile.eof()) and it fails each time.

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Have a look at the function ..atof();

  12. #12
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    An example of how to use atof. It is C, but who cares?
    Code:
    #include <string>
    #include <ctype.h>
    #include <conio.h>
    using namespace std;
    
    int main(){
    
       string s("5.64");
    
       cout << atof( s.c_str() ); //c_str converts string to char*
    
       getch();
    }
    Nothing more to tell about me...
    Happy day =)

  13. #13
    Maxwell25
    Guest
    Sorry, but after reading the fine print, I realize menuItem MUST be a double and not a string.

    Now how should I read it in?

  14. #14
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Lightbulb

    Code:
    //this is your own function with some changes
    void getData(menuItemType menuList [], ifstream& inFile)
    {
    	
        int counter=0;
        while (inFile)
        {
            getline(inFile,menuList[counter].menuItem);
            string tempString; //declares a temporary string
            inFile >> tempString;
            menuList[counter].menuPrice = atof(tempString.c_str());
            counter++;
        }
    }
    I not tested it ( too lazy today, sorry ), but should work. Just remember that >> reads until the first blank space or new line. But how I donīt know how to read a whole line I canīt help you further... sorry. But any other problems I still want to aid you
    Last edited by gustavosserra; 04-28-2003 at 09:41 PM.
    Nothing more to tell about me...
    Happy day =)

  15. #15
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Just a note

    I modified your function:
    Code:
    void showMenu(menuItemType menuList[], 
    custItemType custItem[]){
    
       int g=0;
       int num=0;
       int i=0;
       for (int k=0; k<counter; k++)
       {
          cout<<"("<<i+1<<")  "<<menuList
                [i].menuItem<<"  "<<menuList[i].menuPrice<<endl;
       }
       cout<<endl;
       //the rest of the function
    Your old function had an problem: it loops until 8 every time, so, this is way you got garbage on screen. In this function above, counter is a global(too lazy to make it a parameter) that is the sum of the lines read(the same counter of the functions that reads the file).
    As I said before(and before) Iīm too lazy now, so if you got any more problems, ask...

    Note for all: is does anyone know how to read a whole line from a file please tell me. The getline function in the Maxwell code donīt compile in Borland C++.
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM