Error in a code and cant find out why
Jan 31, 2014 at 7:06pm UTC
I should write a function which will count how many words are in a sentence. I thought that this should be right however, there is some error and I do not get why. Could you please help me?
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
#include <iostream>
#include <string>
using namespace std;
void f1(string);
int main()
{
string a;
getline(cin,a);
f1(a);
}
void f1(string a){
int count=0;
if (a==' ' ){
count++;
}
}
Jan 31, 2014 at 7:09pm UTC
Error is on line 18. Please help.
Jan 31, 2014 at 7:19pm UTC
okay I already got it if anyone needs help with this kind of thing in the future here is the code:
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
#include <iostream>
#include <string>
using namespace std;
void f1(string a);
int main()
{
string a;
getline(cin,a);
f1(a);
}
void f1(string a){
int count=1;
for (int i=0;i<a.length();i++){
if (a[i]==' ' ){
count++;
}
}
cout<<count;
}
Topic archived. No new replies allowed.