Mar 2, 2011 at 7:27am UTC
Hi! Is it possible to read from specific rows from a notepad?
For Example:
Jay
Mark
Christian
I only want to display the word Christian which is on the third row, what will I do? can you please show me? Thanks! Here's my code:
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <cstring.h>
#include <string.h>
#include <conio.h>
//using namespace std;
int main()
{
char account[80];
char string1[] = ".txt";
char string2[17];
cout<<endl;
cout<<"Enter Account Number: ";
cin>>account;
strcpy(string2, account);
strcat(string2, string1);
int bal = 0;
int x;
ifstream inFile;
inFile.open(string2);
if (!inFile) {
cout << "Invalid Account";
exit(1); // terminate with error
}
while (inFile >> x)
{
bal = bal + x;
}
inFile.close();
clrscr();
cout<<endl;
cout<< bal << endl;
getch ();
return 0;
}
Mar 2, 2011 at 9:34am UTC
1. Don't use char arrays as strings, use the standard string class.
2. You read a whole line with getline(). If you read with >> you only read the next token, which is often less than a line.