C++/Adding arrays
Expert: Zlatko - 4/29/2010
QuestionHello Zlatko, thanks for the help you already gave me on Reading a File. Now I am in the same program trying to add another array (totalwinningNumber), I am not sure if I did it right because it works just for three numbers of lines (noOfLines) and I really want this program works for unknowing number of lines. You explain me before that in the program I cannot change the file size without changing the program and I thought I just I have to change in the code the number of lines(const int noOfLines) and that's it but I was wrong. Can you explain why not and help me with the code please.
This is the code:
#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],
int totalwinningNumber[])
{
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];
int totalwinningNumber[noOfwinningNumber];
ifstream infile;
infile.open("example.txt");
if(!infile)
{
cout <<"Input file (example.txt) does not exist."<<endl;
return 1;
}
getLines(infile, theDate, drawNumber, winningNumber,
totalwinningNumber);
for(int i = 0; i < noOfLines; ++i)
{
cout << "Date: " << theDate[i] << " Draw " << drawNumber[i] << " WN ";
for (int j = 0; j < noOfwinningNumber; ++j)
{
cout << setw(4) << winningNumber[i][j];
}
cout << " Total ";
for(int j = 0; j < noOfwinningNumber; ++j)
totalwinningNumber[i] = totalwinningNumber[i] + winningNumber[i][j];
cout << totalwinningNumber[i] << endl;
}
infile.close();
return 0;
}
AnswerHello Raul.
I think the problem is with
for(int j = 0; j < noOfwinningNumber; ++j)
totalwinningNumber[i] = totalwinningNumber[i] + winningNumber[i][j];
It seems that your are trying to sum up the j'th number in each winning number set. The line in the loop should be
totalwinningNumber[j] = totalwinningNumber[j] + winningNumber[i][j];
Remember i is the line index, j is the index into a set of winning numbers. You should rename i and j to something that better indicates their function. I'd suggest lineIx, and numberIx.
Also, notice that the
cout << totalwinningNumber[i] << endl;
is outside of the for loop, even though it is indented to look like part of the loop. To have more than one line in the loop, you need to put braces { } around the lines. Also, I think you want to print out the totalWinningNumbers after all the lines have been processed, so the cout should be in another loop outside of the line processing loop. Here is what I think the code should look like.
#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],
int totalwinningNumber[])
{
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];
int totalwinningNumber[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,
totalwinningNumber);
memset(totalwinningNumber, 0, sizeof(totalwinningNumber));
for(int i = 0; i < noOfLines; ++i)
{
cout << "Date: " << theDate[i] << " Draw " << drawNumber[i] << " WN ";
for (int j = 0; j < noOfwinningNumber; ++j)
{
cout << setw(4) << winningNumber[i][j];
}
cout << " Total ";
for(int j = 0; j < noOfwinningNumber; ++j)
{
totalwinningNumber[j] = totalwinningNumber[j] + winningNumber[i][j];
}
cout << endl;
}
cout << "Total of Winning Numbers\n";
for (int numberIx = 0; numberIx < noOfwinningNumber; ++numberIx)
{
cout << setw(4) << totalwinningNumber[numberIx] << endl;
}
infile.close();
return 0;
}
Good luck on the lottery, but don't spend too much. It cannot be beaten.