GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Dec-2005, 12:19
mnkdrt mnkdrt is offline
New Member
 
Join Date: Dec 2005
Posts: 2
mnkdrt is on a distinguished road

Help with reading data...getting garbage output


Hello forum, I new here and to C++ so any help would be greatly appreciated.
I am working on a program using arrays of structures and am reading data from an infile. I thought I was reading the data in correctly but am getting garbage out put when trying to just print a screen cout check.
Can any one point out what I am doing wrong.
Thanks alot.
D.

sample goods.data-
Boxing Gloves
1025 8 47 6.56 7.58 23.69
Red Trunks
2569 9 23 9.25 3.25 12.39

CPP / C++ / C Code:
struct TOOLS
{
       char Tool_Name[50];
       int Tool_N;
       int Quanity;
       int QuanityDesired;
       float Cost;
       float Price;
       float Sales;
};

int main()
{
    TOOLS L[MaxItems];
    int i = 0, count;
    ifstream fin;
    
    fin.open("goods.data");
    if(fin.fail())
    {
                  cout << "ERROR: Cannot open file to read goods.data. \n";
                  exit(1);
    }

    fin.getline(L[i].Tool_Name, 30);
    while(fin)
    {
              fin >> L[i].Tool_N >> L[i].Quanity >> L[i].QuanityDesired >> L[i].Cost >> L[i].Price >> L[i].Sales;
              fin.get();
              i++;
              fin.getline(L[i].Tool_Name, 30);
              
    }
    i = count;
    for(i = 0; i < MaxItems; i ++)
    {
          cout << L[i].Tool_Name << endl;
    }
    fin.close();
    return 0;
}
Last edited by LuciWiz : 03-Dec-2005 at 07:42. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 02-Dec-2005, 13:28
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 922
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Help with reading data...getting garbage output


Hi mnkdrt,

Welcome to GIDForums.

You are getting garbage because you should print only upto the lines read from the file.
For example, consider that the MaxItems is 5.
But your file contains only Two sets of data.

So, if you print from 0 to 4, the first two values will be correct and the third, fourth and fifth values will be garbage.

Just make a simple adjustment.
Store the number of lines read in a variable count, and use it for printing.
Like this:
CPP / C++ / C Code:
    count = i;
    for(i = 0; i < count; i ++)
    {
          cout << L[i].Tool_Name << endl;
    }

And this statement may lead to errors:
CPP / C++ / C Code:
    while(fin)
So, just put the getline statement inside the while loop. Well and Good.
Like this:
CPP / C++ / C Code:
    while(fin.getline(L[i].Tool_Name, 30))
    {
              fin >> L[i].Tool_N >> L[i].Quanity >> L[i].QuanityDesired >> L[i].Cost >> L[i].Price >> L[i].Sales;
              fin.get();
              i++;
    }

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 02-Dec-2005, 13:46
mnkdrt mnkdrt is offline
New Member
 
Join Date: Dec 2005
Posts: 2
mnkdrt is on a distinguished road

Re: Help with reading data...getting garbage output


Great and thanks for the help
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
Need Help reading HEX data from a sensor Moooey C Programming Language 4 09-Mar-2005 13:46
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13
using vector or array oshiotse CPP / C++ Forum 4 16-Apr-2004 10:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:29.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.