| | |
How can I read from a txt file?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 6
Reputation:
Solved Threads: 0
Hello there, My program is supposed to read data from a txt file, and put this data in a 2 dimension array. All I have is the last line of data in my text file. My teacher said that I have to update the array in my loop. I have no idea how to do it. Can someone help me? Nicole
example of txt file
1 2 3
4 5 6
...
int main()
{
const int TYPE = 3;
const int COUNTRY = 50;
int result[COUNTRY][TYPE] = {0};
int totalPoint[COUNTRY] = {0};
int numGold,numSilver,numBronze;
int cntCountry = 0;
int totalMedal = 0;
while(!cin.eof() )
{
cin >> numGold >> numSilver >> numBronze;
if(numGold > 0 || numSilver > 0 || numBronze > 0)
{
++cntCountry;
}
}
for ( int i = 0; i < cntCountry; ++i )
{
result[i][0] = numGold;
result[i][1] = numSilver;
result[i][2] = numBronze;
}
example of txt file
1 2 3
4 5 6
...
int main()
{
const int TYPE = 3;
const int COUNTRY = 50;
int result[COUNTRY][TYPE] = {0};
int totalPoint[COUNTRY] = {0};
int numGold,numSilver,numBronze;
int cntCountry = 0;
int totalMedal = 0;
while(!cin.eof() )
{
cin >> numGold >> numSilver >> numBronze;
if(numGold > 0 || numSilver > 0 || numBronze > 0)
{
++cntCountry;
}
}
for ( int i = 0; i < cntCountry; ++i )
{
result[i][0] = numGold;
result[i][1] = numSilver;
result[i][2] = numBronze;
}
> while(!cin.eof() )
This is a bad idea and will probably result in you processing the last line of the file twice. A better method is to use your input as the condition:
Without knowing the format of your file, I have to assume that your teacher meant this change:
At the end of the file, the array has a total of everything rather than the values of the last line.
This is a bad idea and will probably result in you processing the last line of the file twice. A better method is to use your input as the condition:
C++ Syntax (Toggle Plain Text)
while ( cin >> numGold >> numSilver >> numBronze )
C++ Syntax (Toggle Plain Text)
result[i][0] += numGold; result[i][1] += numSilver; result[i][2] += numBronze;
I'm here to prove you wrong.
![]() |
Similar Threads
- Read a Regular .txt file (C)
- The Fastest way to read a .txt File (C++)
- how do I have txt file be read by the compiler? (C++)
- Need Help to read .txt file and store its contents (Java)
Other Threads in the C++ Forum
- Previous Thread: calender
- Next Thread: C++ Compiler???
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






