Output is correct for every part of the loop besides the firstloop... which gives values of 0 instead of the actual data fromfile. please help! C++
RAW CODE BELOW
//MAIN :
#include "header.h"int main(){ifstream fin;int numlines = LineCount(&fin);
Item *store = ReadFile(&fin, numlines);store = new Item [numlines];
for(int i = 0; i < numlines; i++){
cout << "The ID is... " << store.ID <<endl;cout << "Price is... " << store.Cost <<endl;}
delete [] store;
return 0;}
//FUNCTIONS:
#include "header.h"
int LineCount(ifstream *fin){fin->open("data.dat");if(fin->fail()){cout << "Error" << endl;exit(1);}int count = 0;string line;while(!fin->eof()){getline(*fin, line);count ++;}
fin->close();
//delete fin;return count;}
Item *ReadFile(ifstream *fin, int numlines){
Item *stock;stock = new Item [numlines];string id, costs;fin->open("data.dat");if(fin->fail()){cout << "Error" << endl;exit(1);}
for(int i = 0; i < numlines; i++){getline(*fin, id, '\t');getline(*fin, costs);stock.ID = stof(id);stock.Cost = stof(costs);}
fin->close();delete [] stock;return stock;}
//HEADER:
#include <iostream>#include <fstream>#include <stdio.h>using namespace std;struct Item{float ID;float Cost;};int LineCount(ifstream *fin);Item *ReadFile(ifstream *fin, int count);
//FILE READING FROM: DATA.DAT
1237 19007412 194213126 31.2414291 12314412 124.1564214 124.1242151 1251651241 123151123 132.516521 125.1612412 125.151213 1231.54
Output is correct for every part of the loop besides the first loop... which gives values of 0 instead of the actual dat
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am