This program read words, lines and paragraphs from a file, and display on console screen.
It compiles but shows No information. Can anybody help me out, what am im doing wrong? :(
include <iostream> //*COMPILES BUT DOSENT SHOW
include <fstream> // ANY!! INFORMATION ON SCREEN. :(
using namespace std;//What Am i doing wrong??
int main(int argc, char *argv[])
{
ifstream infile;
bool flag =false;
bool paraFlag = false;
char c;
int words = 0;
int lines = 0;
int paragraphs = 0;
infile.open("C:\imput.txt");
c = infile.get();
while (!infile.eof())
{
while (c != '\n')
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
if (!flag)
{
words++;
flag = true;
}
}
else
flag = false;
c = infile.get();
}
if (c == '\n')
{
lines++;
if (!paraFlag)
{
paragraphs++;
paraFlag = true;
}
c = infile.get();
while (c == '\n'){
lines++;
paraFlag = false;
c = infile.get();
}
}
}
cout << " W " << words << endl;
cout << " L " << lines << endl;
cout << " P " << paragraphs << endl;
infile.close();
system("PAUSE");
return 0;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-15
Your code does not check that the file has opened successfully. You could always step teh code in teh debugger (well if you have more luck with it that I ever have had!).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This program read words, lines and paragraphs from a file, and display on console screen.
It compiles but shows No information. Can anybody help me out, what am im doing wrong? :(
include <iostream> //*COMPILES BUT DOSENT SHOW
include <fstream> // ANY!! INFORMATION ON SCREEN. :(
using namespace std;//What Am i doing wrong??
int main(int argc, char *argv[])
{
ifstream infile;
bool flag =false;
bool paraFlag = false;
char c;
int words = 0;
int lines = 0;
int paragraphs = 0;
infile.open("C:\imput.txt");
c = infile.get();
while (!infile.eof())
{
while (c != '\n')
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
if (!flag)
{
words++;
flag = true;
}
}
else
flag = false;
c = infile.get();
}
if (c == '\n')
{
lines++;
if (!paraFlag)
{
paragraphs++;
paraFlag = true;
}
c = infile.get();
while (c == '\n'){
lines++;
paraFlag = false;
c = infile.get();
}
}
}
cout << " W " << words << endl;
cout << " L " << lines << endl;
cout << " P " << paragraphs << endl;
infile.close();
system("PAUSE");
return 0;
}
Your code does not check that the file has opened successfully. You could always step teh code in teh debugger (well if you have more luck with it that I ever have had!).
Just curious, is your input file really called imput.txt?