Read data from a file

I am trying to use ifstream to read data from a .txt and store it into different arrays.

The problem is that the data are numbers and not letters, This is what I have so far:

/*
Complete Corporate Sales Data (#3) in chapter 11 on page 659 of the text book.

The main program should read in the sales data from the attached input file.
Modify the program as follows by creating the following functions -

1. getData - Takes no arguments, reads in the data from the file into the divisions,
returns a populated company division.

2. dispDivInfo - Takes a division as an argument, Be sure the division cannot
be modified in dispDivInfo Displays the divisions info to the screen.*/
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>

using namespace std;
struct sales
{
string divname;
double quarterly[4];
double totsum;
double quartaverage;
};

void getData(sales[], const double);
void salesresult(sales[], const double);
void dispDivInfo(const sales[], const double);


void main()
{

const int branches = 4;
sales teams[branches] = { {"North"},{"South"},{"East"},{"West"} };
getData(teams, branches);
salesresult(teams, branches);
dispDivInfo(teams, branches);

system("pause");

}

void getData(sales teams[],const int branches)
{
int a, b, c, d;
ifstream myfile;
myfile.open("quarterlysalesfigures.txt");
while (myfile >> a >> b >> c >> d)
{
cout << a, b, c, d;
}

}


What you have so far doesn't compile.


The problem is that the data are numbers and not letters


Just read them in as a numbers then (which you already do).

1
2
3
4
5
6
7
while (myfile >> a >> b >> c >> d)
{
  /* OK, what do you think you should do with this data once you have read them
   * in? Inside this while loop you have to do something with that data, so I
   * would start here. */
  cout << a, b, c, d;
}


Try to get your program to compile first, and then ask us of things you don't
understand.
The problem is it is nor reading the first 4 values (the .txt has 16 values) and i don't know hot to read the numbers after these 4 values . And I can't compile the program without these because the other parts are basically to calculate the sum and average of these values.
You don't have to compile it all at the same time. Just implement small parts,
and compile those small parts, and compile often.


i don't know hot to read the numbers after these 4 values


Look at your while loop.

1
2
3
4
5
6
7
8
9
10
11
while (myfile >> a >> b >> c >> d)
{
  /* The while loop reads in four values and stores them into a, b, c, and d
   * respectively. OK. It will do this until the myfile object isn't able to
   * read the file anymore (or in this case the end of the file has probably
   * been reached).
   *
   * What this loop is essentially reading in four things at a time until it
   * there isn't anything left in the file to read. */
  cout << a, b, c, d;
}


If you just want to grab the first four things in the text file, just read four
things outside the loop like this:

1
2
3
4
myfile >> a >> b >> c >> d;

/* And then you can do your reading in the while loop */
while(myfile >> /* your code */ )


It might be helpful if you gave us the format of the file being read so we can
understand it as well.
Topic archived. No new replies allowed.