How do I make this into a class function?

I don't understand functions very well and my professor hasn't been any help. My code works perfectly for the purpose and logic. However, I don't have the correct function signature that the professor wants.
If someone could give me an explanation on how to add the correct function signature to my code that would help a lot.
The function looks like this:
class CIS14 {
public:
int countLastWordLength(const string str);
};

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
  /**
 * Assignment#5.1
 * DUE: 3/6/16 
 * NAME: <Joshua Cisneros>
 * PURPOSE: You are an awesome engineer in a company called "Awesomeness Software, Inc". 
 * They make word processing software. The current software in development requires that it 
 * produces character count on a given word as input. 
 * In particular, it needs to count the length of the last word in a sentence 
 * for some formatting feature in the future.  
 */
#include <iostream> 
#include <string> 
#include <algorithm> 
#include <sstream> 

using namespace std; 
/**
 * PURPOSE: displays number of char in last word only
 * PARAMETERS:
 *     string in and lastword
 *     I am also using stringstream 'ss'
 * RETURN VALUES:
 *     const string str
*/
int main(int argc, char *argv[]) { 
 string in, lastWord; 
 stringstream ss; 

 while (true) { 
 cout << "Enter your string:  "; 
 getline(cin, in); 
 reverse(in.begin(), in.end()); 
 ss.clear(); ss.str(in); 
 ss >> lastWord; 
cout << lastWord.size() << endl << endl; 
 } 
 return 0; 
} 



1
2
3
4
5
6
7
8
9
10
class CIS14
{
public:
  int countLastWordLength (const string str);
};

int CIS14::countLastWordLength (const string str)
{
  //TODO place your code here to count the length of the last word and return the length
}


In your main function you create an obj of the class CIS14 and call the method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main (int argc, char *argv[]) 
{
  string in;
  
  while (true)
  {
    cout << "Enter your string:  ";
    getline (cin, in);
   
   CIS14 obj;

   cout << "Length of last word: " << obj.countLastWordLength (in);
  }
  return 0;
}
Ok,
Write it like this:
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
#include <iostream> 
#include <string> 
#include <algorithm> 
#include <sstream> 

class CIS14 {
public:
int countLastWordLength(const string str, stringstream ss)
{
string lastWord;
string in = str;
    reverse(in.begin(), in.end()); 
 ss.clear(); ss.str(in); 
 ss >> lastWord; 
cout << lastWord.size() << endl << endl
}
};
using namespace std; 
/**
 * PURPOSE: displays number of char in last word only
 * PARAMETERS:
 *     string in and lastword
 *     I am also using stringstream 'ss'
 * RETURN VALUES:
 *     const string str
*/
int main(int argc, char *argv[]) { 
 CIS14 cis;
 string in, lastWord; 
 stringstream ss; 

 while (true) { 
 cout << "Enter your string:  "; 
 getline(cin, in); 
const string f = in;
cis.countLastWordLength(f, ss);
 }
 return 0; 
} 

I know this is not object-oriented at all, but I don't have time now to fix it. I think it should work and fulfill your assignment as well.
Good luck!
Topic archived. No new replies allowed.