Computing.Net > Forums > Programming > input/output file...which is which?

input/output file...which is which?

Reply to Message Icon

Original Message
Name: egypt
Date: November 15, 2003 at 19:41:14 Pacific
Subject: input/output file...which is which?
OS: win98
CPU/Ram: 256RAM
Comment:

ok, i will admit i am truly lost and need help with this program. BELOW IS THE PROGRAM I HAVE SO FAR. I KNOW SOME PARTS OF THIS IS NOT CORRECT, BUT I NEED SOME ASSISTANCE WITH IT.

write a function that counts and displays on the screen the number of words in an input file. the program assumes that words in the file are separated by blank spaces or end of lines. test the program with file containing no words, one word, and several words

#include<iostream.h>
#include<fstream.h>
#include<string.h>
int main()
{

char ch;

int wordcount;

ifstream myfile;

myfile.open("storm1.txt");

if(ch==' ')
{
wordcount++;
}
cout<<"number of words:"<<wordcount<<endl;


myfile.close();


return 0;

}



Report Offensive Message For Removal

Response Number 1
Name: saddam
Date: November 15, 2003 at 19:46:58 Pacific
Subject: input/output file...which is which?
Reply: (edit)

what exactly is not working? what is the output and does it compile?


Report Offensive Follow Up For Removal

Response Number 2
Name: saddam
Date: November 15, 2003 at 19:57:57 Pacific
Subject: input/output file...which is which?
Reply: (edit)

you may need to add this:

while( !myfile.eof() )

this reads the entire file.


Report Offensive Follow Up For Removal

Response Number 3
Name: egypt
Date: November 16, 2003 at 06:38:16 Pacific
Subject: input/output file...which is which?
Reply: (edit)

the output is number0
i know that is not what the instructions ask for,but i still don't know how to put this program together. i am really lost on this!!!


Report Offensive Follow Up For Removal

Response Number 4
Name: Don Arnett
Date: November 16, 2003 at 12:15:27 Pacific
Subject: input/output file...which is which?
Reply: (edit)

You have no input statement. You've opened the file and you close the file, but have no statement to read data from the file.

I don't use C++ I/O functions, so I don't know what the exact statement is that is missing, but in C you'd want to use either

getc() or getchar() to input a character.


Also, as hinted at by saddam, you need a loop around your if statement:

ch = myfile.someFunctionToReadACharacter();
while (!myfile.eof()) {

if (ch == ' ') {
++wordCount;
}

ch = myfile.someFunctionToReadACharacter();
}


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: input/output file...which is which?

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software