1 Replies - 730 Views - Last Post: 19 June 2007 - 10:31 PM Rate Topic: -----

#1 baihu924  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-June 07

ifstream help

Post icon  Posted 19 June 2007 - 06:43 PM

this program is suppose to read from a data file and search for valid or invalid characters in the data, valid characters to be integers, plus, minus, equals, whitespace, and tabs. i used notepad for the data file and found the data in the program by inputing, data.txt. for the user input. right now, this program can only read invalid characters. for example, i saved in the notepad just the characters: blah, and the program read perfect saying that it read 4 invalid characters and 0 valid characters. but when i saved in the notepad any valid characters, for example: blah1, it doesnt work and i have to function break the program to exit. i need help......

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

using namespace std;

int main()
{
char file;
int good;
int bad;
ifstream data;
string indata;

good=0;
bad=0;

cout<<"Enter name of the data file: ";
cin>>indata;

data.open(indata.c_str());
if (data.fail())
{
cout << "\nFailure to find input file " << indata << endl;
system("pause");
return(1);
}
else
{
data >> file;

while( !data.eof() )
{
switch(file)
{
case' ':
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9':
case'0':
case'+':
case'-':
case'\t':
case'=': good++;
break;
default: bad++;

data>>file;
}

}
cout<<"The number of invalid characters in the file:"<<(bad)<<endl;
cout<<"The number of valid characters in the file:"<<good<<endl;
}
system("pause");
return 0;
}

Is This A Good Question/Topic? 0
  • +

Replies To: ifstream help

#2 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: ifstream help

Posted 19 June 2007 - 10:31 PM

all you have to do is move the data>>file; out side of the switch and it works
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1