Help me!

Write a procedure that counts the number of words in a string.

errors about my functions

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
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <cstdlib>
#include <stdlib.h> 

void getn (string& wings);
void count (string words,int& wordcount = 1);
using namespace std;

int main()
{
	string w;
	int n;
	getn( w );
	count( w,n);
	cout << " String is:" << w << endl;
	cout << "Number of words:" << n << endl;
	
	return 0;
}
void getn(string& wings)
{
	cout << "input string here: ";
	getline(cin,wings);
	cout<< endl;
}
void count(string words,int& wordcount = 0)
{
	int counter = 0;
	bool exit = 0;
	int words1 = static_cast <int>(words.length());
	while(counter < words.length() || exit == 1)
		{
			int p = 0;
			if(words.at(p) = " " && words.at(p+1) == " ")
				{
					cout << "No word in this.\n" << "exiting program";
					exit++;
				}
			else if (words.at(p) != " " && words.at(p+1) == " ")
				{
					wordcount++;
				}
				
			counter++;
		}			
}
Last edited on
And the errors are? And please use code tags.
g++ -Wall -o "exercise9_1" "exercise9_1.cpp" (in directory: /home/stephen/Desktop/untitled folder)
exercise9_1.cpp:7: error: variable or field ‘getn’ declared void
exercise9_1.cpp:7: error: ‘string’ was not declared in this scope
exercise9_1.cpp:7: error: ‘wings’ was not declared in this scope
exercise9_1.cpp:8: error: variable or field ‘count’ declared void
exercise9_1.cpp:8: error: ‘string’ was not declared in this scope
exercise9_1.cpp:8: error: expected primary-expression before ‘int’
exercise9_1.cpp: In function ‘int main()’:
exercise9_1.cpp:15: error: ‘getn’ was not declared in this scope
exercise9_1.cpp:16: error: ‘count’ was not declared in this scope
exercise9_1.cpp: At global scope:
exercise9_1.cpp:28: error: default argument for ‘int& wordcount’ has type ‘int’
exercise9_1.cpp: In function ‘void count(std::string, int&)’:
exercise9_1.cpp:36: warning: comparison with string literal results in unspecified behaviour
exercise9_1.cpp:36: error: ISO C++ forbids comparison between pointer and integer
exercise9_1.cpp:36: warning: suggest parentheses around assignment used as truth value
exercise9_1.cpp:41: warning: comparison with string literal results in unspecified behaviour
exercise9_1.cpp:41: error: ISO C++ forbids comparison between pointer and integer
exercise9_1.cpp:41: warning: comparison with string literal results in unspecified behaviour
exercise9_1.cpp:41: error: ISO C++ forbids comparison between pointer and integer
Compilation failed.

This is it.Ill change it
I don't know if it will make a difference or not, but in your function prototypes, I'd suggest removing your variable names and values. I don't know if it should throw an error, but I know that I was taught to just use the data types in the prototypes.
Thanks man,ill try and use that.
Volatile Pulse wrote:
I don't know if it will make a difference or not, but in your function prototypes, I'd suggest removing your variable names and values. I don't know if it should throw an error, but I know that I was taught to just use the data types in the prototypes.
Nope, makes no sense. The (default) values on line 7 is only for the protoype. Later on line 27 it makes no sense.

Move line 8 before line 6 and those errors will go away.
I see why, because string is actually std::string. Was not thinking straight on that one. I need to stop staying up so late.
thanks guys. i fixed the errors and it allows it to run but its not out putting the amount words correctly
heres the new 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <cstdlib>
#include <stdlib.h> 

using namespace std;
void getn (string& wings);
void count (string words,int& wordcount);


int main()
{
	string w;
	int n;
	getn( w );
	count( w,n);
	cout << " String is:" << w << endl;
	cout << "Number of words:" << n << endl;
	
	return 0;
}
void getn(string& wings)
{
	cout << "input string here: ";
	getline(cin,wings);
	cout<< endl;
}
void count(string words,int& wordcount)
{
	int counter = 0;
	bool exit = 0;
	int counts = wordcount;
	int words1 = static_cast <int>(words.length());

	while(counter < words1 || exit == 1)
		{
			int p = 0;
			if(words.at(p) == ' ' && words.at(p+1) == ' ')
				{
					cout << "No word in this.\n" << "exiting program\n";
					exit = 1;
					break;
				}
			else if (words.at(p) != ' ' && words.at(p+1) == ' ')
				{
					counts++;
				}
				
			counter++;
		}			

}
 
Check your while loop on line 35. I'm not sure what you are trying to do in there.
i typecast the length of the string in-putted and made it an int.
The while loop is if counter is less than the string length so it keeps counting up until counter is greater than the length and then it breaks.
I meant to check the inside of the while loop, sorry.
Um the if statement is just declaring that if there is more than one space char then its not a word so it exits
the second one counts location P as a letter and then P+1 as a space then its a word.i guess
The end of a string isn't terminated by ' ' so be careful if there is only one word in the string.
Its still not counting up.the amount of words.ugh so frustrating
Topic archived. No new replies allowed.