Output is correct for every part of the loop besides the first loop... which gives values of 0 instead of the actual dat

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Output is correct for every part of the loop besides the first loop... which gives values of 0 instead of the actual dat

Post by answerhappygod »

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
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply