We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,745 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Problems with storing and outputing data from a text file

Hi all...I really need someones help and real fast. I am trying to read a textfile with multiple columns that are each separated by a #. eg:

Inventory.txt
Nokia Lumia 610#Nokia#Touch#8GB#8#Tango OS#3899.00#
Blackberry Curve 9380#Blackberry#Both#1GB#8#Blackberry OS 7#3499.00#
Samsung Galaxy S3#Samsung#Touch#32GB#8#Andriod#6999.00#
iPhone 4S#Apple#Touch#16GB#8#IOS#7899.00#

I am trying to save each bit of data in separate variable in a structure because later on I am going to perform calulates and editing but for now. I cant seen to get it to work. When I run my code, it only displays the first line and nothing else. Plus it shows a weird number at the beginning: 1.13002e-317

Can someone help correct my code to display all of the textfile in neat columns and store each in its owm variable correctly.
ps: I cant help think I need to be using Pointers, can someone help with that too

#include<fstream>
#include<iostream>
#include<string>
using namespace std;

struct CellphoneInfo
{
    string name;
    string manu;
    string cellDesign;
    string internalMem;
    string camera;
    string os;
    double costPrice;
};

void outputData(CellphoneInfo [],int);
int main()
{
    string input;
    const int size = 100;

    CellphoneInfo *cellphonePtr;

    fstream dataFile;
    dataFile.open("Inventory.txt",ios::in);
    int count =0;

    if(dataFile)
    {
        while(dataFile)
        {
            count++;
            getline(dataFile,cellphone[count].name,'#');
            getline(dataFile,cellphone[count].manu,'#');
            getline(dataFile,cellphone[count].cellDesign,'#');
            getline(dataFile,cellphone[count].internalMem,'#');
            getline(dataFile,cellphone[count].camera,'#');
            getline(dataFile,cellphone[count].os,'#');
            dataFile >> cellphone[count].costPrice;
        }       

        dataFile.close();
        cout << "File read in" << endl;
        outputData(cellphone,count);

    }
    else
    {
        cout << "Error: Cannot open file.\n";
    }
    return 0;

}

void outputData(CellphoneInfo arr [],int count)
{
    cout << "CellName\tMan Name\tDesign\tMemory\tCamera\tOS\tPrice\n\n"; 
    for(int i = 0;i < count;i++)
    {
        cout << arr[i].name << "\t" << arr[i].manu << "\t" << arr[i].cellDesign << "\t" << arr[i].internalMem << "\t" << arr[i].camera << "\t" << arr[i].os << "\t" << arr[i].costPrice;
    }
    cout << endl;
}
3
Contributors
2
Replies
4 Days
Discussion Span
6 Months Ago
Last Updated
3
Views
ritchonson101
Newbie Poster
1 post since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

where is the array cellphone used in lines 34-40 declared?

Ancient Dragon
Achieved Level 70
Team Colleague
32,116 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69

There is no information about cellphone[] in your entire program. One more thing read Click Here

The description for Delim says:

The operation of extracting successive characters is stopped when this character is read.

So if you do getline and pass '#' as delin, it will stop reading data after name.

I can suggest go for getLine but do not use delim, with return string you can always use strtok and get all the information using a loop. To avoid these many statments you can also use two loops:

  1. to read line from text file.
  2. Once line is being read, extract all values using strtok

Hope this will help!nJoy!!

mLearning
Newbie Poster
6 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0667 seconds using 2.73MB