Problem with classes and other issues

I am trying to write a code that will allow the user to input words that will then be manipulated by the code to either make all letters lower case, reverse the sentence, sort the words alphabetically, or encrypt the words. This uses a class named "Phrase". I am trying to figure out what the various problems are with the code but am having no luck. Think you guys can figure out what I cannot?

Phrase.h
1
2
3
4
5
6
7
8
9
10
  #pragma once
class Phrase
{
public:
	int lowerCase(int input);
	int reversed(int input);
	int sort(int input);
	int encrypt(int input);
};


Phrase.cpp
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
#include "stdafx.h"
#include "Phrase.h"
#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

int Phrase::lowerCase(int input) {//makes all letters lower case
	char words;
	int i;
	for(int i = 0;words[i] != '\0'; i++) {
		words[i] = tolower(words[i]);
		return words;
};

int Phrase::reversed(int input) {//reverses the sentence
	char words;
	for (int i = words(); i >= 0; i--)
	}
	return words;
};

int Phrase::sort(int input) {//sorts words alphabetically
	char words;

	return words;
};

int Phrase::encrypt(int input) {//encrypts in rot13
	char words;
	for (int i = 0; words[i] != '\0'; j++) {
		if (words[i] >= 'a' && words[i] <= 'm') {
			words[i] += 13;
		}

		else if (words[i] > 'm' && words[i] <= 'z') {
			words[i] -= 13;
		}

		else if (words[i] >= 'A' && words[i] <= 'M') {
			words[i] += 13;
		}

		else if (words[i] > 'M' && words[i] <= 'Z') {
			words[i] -= 13;
		}

	}
	return words;
};


main.cpp
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
54
55
56
57
58
59
60
#include "stdafx.h"
#include <string>
#include "Phrase.h"
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
	char words;
	int option=0;
	bool mainMenu = true;
	Phrase manipulate;
	int startanew;

	//Start: Queries the user
	cout << "Please enter a phrase to manipulate:  ";
	cin >> words;

	cout << "What would you like to do to your phrase?  " << endl << "1:Lower case" << endl <<
			"2:Reverse the words" << endl << "3:Sort the words alphabetically" << endl << "4:Encrypt" 
			<< endl << "Which option would you like? (Enter a number from above)  ";
	cin >> option;

		while (mainMenu) {//Start of the main menu(loops the user if chosen)
		
		if(option == 1){//Option 1 selection
			cout << "Your phrase is now all lowercase!" << endl << manipulate.lowerCase(words) << endl;
		}
		else('/0');
		if (option == 2) {//Option 2 selection
			cout << "Your phrase is now entirely reversed!" << endl << manipulate.reversed(words) << endl;
		}
		else('/0');
		if (option == 3) {//Option 3 selection
			cout << "Your phrase is now sorted alphabetically!" << endl << manipulate.sort(words) << endl;
		}
		else("/0");
		if (option == 4) {//Option 4 selection
			cout << "Your phrase is now encrypted!(I can keep a secret)" << manipulate.encrypt(words) << endl;
		}
		else('/0');

		cout << "Would you like to try something else? ";
		cin >> startanew;
		if (startanew == 'yes') {
			mainMenu = true;
		}
		else('/0');
		if (startanew == 'no') {
			mainMenu = false;
		}
		else('/0');
	}


	system("pause");
    return 0;
}
Last edited on
There’re errors in your code, but I think the main point is classes are not conceived to be collection of functions only. You can get the best from classes when you add (at least) a ‘variable’ (a property) which you later access by your class methods.

Here is an example based on your code:
Phrase.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef PHRASE_H
#define PHRASE_H

#include <string>

class Phrase {
public:
    Phrase(std::string words_arg);
    std::string& lowerCase();
    std::string& reversed();
    std::string& sort();
    std::string& encrypt();
private:
    std::string words;
};

#endif // PHRASE_H 


Phrase.cpp
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
54
55
56
57
58
59
#include <algorithm>
#include <iomanip>
#include <iterator>
#include <set>
#include <sstream>
#include <string>
#include "Phrase.h"

Phrase::Phrase(std::string words_arg) : words {words_arg} {}

std::string& Phrase::lowerCase() //makes all letters lower case
{
    for(auto& c : words) { c = std::tolower(c); }
    return words;
}

std::string& Phrase::reversed() //reverses the sentence
{
    std::reverse(words.begin(), words.end());
    return words;
}

std::string& Phrase::sort() // sorts words alphabetically
{
    for(auto st = words.find_first_of(",;:.?!\'");
        st != std::string::npos;
        st = words.find_first_of(",;:.?!\'")) {
            if(st != std::string::npos) { words.at(st) = ' '; }
    }
    std::istringstream ss (words);
    std::multiset<std::string> pieces { std::istream_iterator<std::string>(ss),
                                        std::istream_iterator<std::string>()    };
    words.clear();
    for(const auto& s : pieces) { words += s + ' '; }
    words.pop_back();
    return words;
}

std::string& Phrase::encrypt() //encrypts in rot13
{
    for(auto& c : words) {
        if(c >= 'a' && c <= 'm') {
            c += 13;
        }

        else if (c > 'm' && c <= 'z') {
            c -= 13;
        }

        else if (c >= 'A' && c <= 'M') {
            c += 13;
        }

        else if (c > 'M' && c <= 'Z') {
            c -= 13;
        }
    }
    return words;
}


main.cpp:
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
54
#include <iostream>
#include <sstream>
#include <string>
#include "Phrase.h"

int main()
{
    int option = 0;
    do {
        std::cout << "\nPlease enter a phrase to manipulate:  ";
        std::string words;
        std::getline(std::cin, words);
        std::cout << "\nWhat would you like to do to your phrase?\n"
                     "1) Lower case\n"
                     "2) Reverse the words\n"
                     "3) Sort the words alphabetically\n"
                     "4) Encrypt\n"
                     "5) Exit\n"
                     "Which option would you like (1-5)?  ";
        std::string ans;
        std::getline(std::cin, ans); // get rid of terminating '\n'
        std::istringstream ss(ans);
        ss >> option;
        if(option < 1 || 5 < option) {
            std::cout << "\nCan't understand your answer.\nPlease, retry.\n\n";
            continue;
        }
        Phrase manipulate(words);
        if(option == 5) { break; }
        switch(option) {
        case 1:
            std::cout << "\nYour phrase is all lowercase is:\n" 
                      << manipulate.lowerCase() << '\n';
            break;
        case 2:
            std::cout << "\nYour phrase entirely reversed is:\n" 
                      << manipulate.reversed() << '\n';
            break;
        case 3:
            std::cout << "\nYour phrase sorted is:\n" 
                      << manipulate.sort() << '\n';
            break;
        case 4:
            std::cout << "\nYour phrase encrypted (I can keep a secret) is:\n" 
                      << manipulate.encrypt() << '\n';
            break;
        }
    } while(0 < option || option < 6);

    // system("pause"); http://www.cplusplus.com/forum/beginner/1988/
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return 0;
}

Topic archived. No new replies allowed.