Jump to content

C++ Reading Data file into Struct Array HELP!!!

- - - - -

  • Please log in to reply
6 replies to this topic

#1
kevinsabres

kevinsabres

    Newbie

  • Members
  • Pip
  • 9 posts
Basically here is the code for my program. The program reads in data from a txt file, stores it in an array of a struct type with 2 variables. then it prints this array. later the info is used for calculations, ect. But when it reads the data then prints the data ONLY the FIRST record in the struct the first 2 variables for the first array element, it prints it fine and also lets me do calculations on it. the rest of the array prints a bunch of wierd endless numbers when ran.:
[code]
//include statements
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>

Edited by kevinsabres, 21 April 2010 - 10:48 AM.
add code tags (the # button)


#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
getData() isn't quite right. The function should also return the value of counter because there may or may not be exactly size number of records in the file.
int getData(ifstream& indata, menuItemType list[], int length)
{
    int counter = 0;

    while(counter < length &&  getline(indata, list[counter].menuItem) )
    {
        indata >> list[counter].menuPrice;
        indata.inore(); // remove the '\n' after the price
        ++counter;

    }
    return counter;
}

Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
kevinsabres

kevinsabres

    Newbie

  • Members
  • Pip
  • 9 posts
thanks for the help that did it. i just didnt understand why it wasnt reading it right. i knew it had to be with getData. im goign to try and look that over and see what you did with it. or if you had time could you explain what tis doing. im quite new to C++.

#4
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
getline() returns its this pointer until either end-of-file or an error occurs. So you can take advantage of that by using a while loop to read all the rows in a file, such as what I posted. This feature is only useful on text files. For binary files you have to use the stream's read() method.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#5
kevinsabres

kevinsabres

    Newbie

  • Members
  • Pip
  • 9 posts
so can you not use getline with a for loop?

The way i had the code before you fixed it, it would only read in the first menuItem and the first menuPrice.

#6
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
>>so can you not use getline with a for loop?
Yes you can, but why? You may not know the number of items in the file, so in that case the for loop will not work.

The main reason your version of that function did not work was because you did not flush the '\n' from the stream after reading the double.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#7
kevinsabres

kevinsabres

    Newbie

  • Members
  • Pip
  • 9 posts
yeah i figured it was somethign with that pesky \n character...

so then this statment then ignores the \n then :

indata.ignore();

thatnks for all the hlep




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users