You are here:

C++/Reading a file

Advertisement


Question
Hi, I am trying to learn c++ by myself , I made this program, it is about to  read data and put it into arrays from the file example.txt   the compiler show me no error but I got nothing on the console.
Can you check my code please?







#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int noOfDates = 3;
const int noOfDraws = 3;
const int noOfwinningNumber = 6;


void getTheDate(ifstream& inp, string theDate[], int);
void getDrawNumber(ifstream& inp, string drawNumber[], int);
void getWinningNumber(ifstream& inp, int winningNumber[], int );

int main()
{
   string theDate[noOfDates];
   string drawNumber[noOfDraws];
   int winningNumber[noOfwinningNumber];
   ifstream infile;

   infile.open("candData.txt");
   if(!infile)
   {
     cout <<"Input file (example.txt) does not exist."<<endl;
     return 1;
   }

   getTheDate(infile, theDate, noOfDates);
   getDrawNumber(infile, drawNumber, noOfDraws);
   getWinningNumber(infile, winningNumber, noOfwinningNumber);

   infile.close();

   return 0;
}

void getTheDate(ifstream& inp, string theDate[], int noOfRows)
{
   for(int i = 0; i < noOfRows; i++ )
       inp >> theDate[i];
   return;
}

void getDrawNumber(ifstream& inp, string drawNumber[], int noOfRows)
{
   for(int i = 0; i < noOfRows; i++)
       inp >> drawNumber[i];
   return;
}

void getWinningNumber(ifstream& inp, int winningNumber[], int noOfRows)

{
   for(int i = 0; i < noOfRows; i++)

           inp >> winningNumber[i];
   return;
}

Input file

example.txt

2/12/2010     1            1     3    23    34      45       4
2/15/2010     2            3     6     14   23      56       34
2/19/2010     3            12   16   34   36      47       23

Answer
Hello Raul.

It's great that you are teaching yourself a new skill. I'm glad to help out.

The program is not showing any output because the only output line you have is if the input file is not found. You need to put in more cout lines.

However, your program will not work the way you expect because it is trying to get all the dates first, then all the draw numbers, then all the winning numbers, but that is not how your file is set up. Your file is a series of characters in lines going from left to right, and lines going from top to bottom. That is the order that it must be read in. Actually, you can jump around the file, but that is a more advanced move.

Have a look at the program below. It does what you are trying to do. It is still not a very good program, because you cannot change the file size without changing the program, but it is a start. Read the program, then try to run it. If you have any questions about what it is doing, just ask.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

const int noOfLines = 3;
const int noOfwinningNumber = 6;

void getLines(ifstream& inp, string theDate[], string drawNumber[], int winningNumbers[][noOfwinningNumber])
{
  for(int i = 0; i < noOfLines; i++)
  {
      inp >> theDate[i];
      inp >> drawNumber[i];
      for(int j = 0; j < noOfwinningNumber; j++)
      {
          inp >> winningNumbers[i][j];
      }
  }
  return;
}

int main()
{
   string theDate[noOfLines];
   string drawNumber[noOfLines];
   int winningNumber[noOfLines][noOfwinningNumber];
   ifstream infile;

   infile.open("infile.txt");
   if(!infile)
   {
       cout <<"Input file (example.txt) does not exist."<<endl;
       return 1;
   }

   getLines(infile, theDate, drawNumber, winningNumber);

   for(int i = 0; i < noOfLines; ++i)
   {
       cout << "Date: " << theDate[i] << " Draw " << drawNumber[i] << " Winning numbers ";
       for (int j = 0; j < noOfwinningNumber; ++j)
       {
           cout << setw(3) << winningNumber[i][j];
       }
       cout << endl;
   }

   infile.close();

   return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.