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);
};
#include "stdafx.h"
#include <string>
#include "Phrase.h"
#include <iomanip>
#include <iostream>
usingnamespace 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;
}
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.