programming

i need the program to count the words and characters ad give me a a number but instead it gives me a list idk what im doing wrong

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("speech.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}

else cout << "Unable to open file";
std::ifstream ifs("speech.txt");
if(!ifs)
{
return 1;
}

std::string word;

while(ifs >> word)
{
std::cout << "word: '" << word << "' has " << word.length() << " characters." << std::endl;
}
system("pause");
return 0;
}
Last edited on
Did you write that? Also, please use the code format tags around code.
yes i did and im new to this idk how to
First select edit.

Then wrap your code around a set of code blocks like this


[ c o d e ]
cout << "hello world!";
[ / c o d e ]

Except getting rid of all the spaces (including the ones by the brackets and it should now look like this

 
cout << "hello world!";
if c++ code not understand then no other language is under for this fist all the people work on c++
Last edited on
This program seems to be working just fine. I modified it a little bit anyway though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;

int main () {
string line;
int c=0;
ifstream myfile ("speech.txt");
if (myfile.is_open())
{
getline (myfile,line);
myfile.close();
}

else cout << "Unable to open file";
ifstream ifs("speech.txt");
if(!ifs)
{
return 1;
}

string word;

while(ifs >> word)
{
cout << "word: '" << word << "' has " << word.length() << " characters." << endl;
c++;
}
cout<<"There are "<<c<<" Words."
system("pause");
return 0;
}
Last edited on
Topic archived. No new replies allowed.