Help with my c++ assignment

Pages: 12
With introductory homeworks you will not likely have problems with plagarism. But I am glad to see you are interested in learning more than copying.

Each of this is a simple modification of an array of characters. For example, suppose I give you:

 
std::string s = "Hello world!";

Can you write a function that makes that all uppercase?

1
2
3
4
5
6
7
8
std::string uppercase( const std::string& s )
{
  std::string result = s;

  // what can I do to result here to make all its characters uppercase? //

  return result;
}

Etc.

Hope this helps.
EDIT: COO9 initial post:

Hi am new to this.
Just wanted someone to help me with my assignment to asks me to do:
Make a menu
1:Enter a word.
2:Reverse word than been enetered.
3:Randomise the word.
4:Upper case the word
5:Lower case it
6:Organise in the alapabetical order of the word.
Exit.
It has to have a menu where the user renter a word then presses a number for example 2 then the program
reverses the word etc.
Thanks looking forward for the replies.
And yea I have started it and got basic codes done but am struggling on writing codes that aren’t from
online(plagiarism reasons)



...I have started it and got basic codes done...


Could you specify what type of help do you need? It would be easier to pin point based on your code.

Do you know how to use the switch case for the menu?

Can you use the string library?

Without seeing your code is a little hard to pin point what help do you need.
Last edited on
closed account (E0p9LyTq)
STOP PMing people! It is RUDE and most people will not want to help you.

You want help, ask for it HERE! In PUBLIC so you and other people who have questions can learn.

To get help show you have written some code, and post it here. Without any code it is hard to help you.

If you want people to do ALL the work for you, then make an offer in the JOBS section. I don't come cheap.
closed account (9G3MizwU)
Do you need 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
using namespace std;

int main()
{
    int C;
    string word;
    cout<<"Enter a word: ";
    cin>>word;
    cout<<endl<<"1:Reverse word than been enetered.";
    cout<<endl<<"2:Randomise the word.";
    cout<<endl<<"3:Upper case the word";
    cout<<endl<<"4:Lower case it";
    cout<<endl<<"6:Organise in the alapabetical order of the word.";
    cout<<endl<<"Other numbers to EXIT" //OPTIONAL
    //If you click other numbers you exit
    cout<<endl<<"Choose: ";
    cin>>C;
    if(C=1)
    {
        //........
    }
    else if(C=2)
    {
        //........
    }
    else if(C=3)
    {
        //........
    }
    else if(C=4)
    {
        //........
    }
    else if(C=5)
    {
        //........
    }
    else if(C=6)
    {
        //........
    }
    /*        OR YOU CAN USE THE "switch" FUNCTION
    switch(C)
    {
    case 1:
        //........
        break;
    case 2:
        //........
        break;
    case 3:
        //........
        break;
    case 4:
        //........
        break;
    case 5:
        //........
        break;
    case 6:
        //........
        break;
    }
    */
    return 0;
}
closed account (z05DSL3A)
COO9 wrote:
How is that rude?

It's like cold calling. I don't know you from Adam and you just ask for help with an assignment. People here give their time freely and don't want to be duplicating effort that others might have spent answering you in private.

If you are worried about people on the same course copying your code, write something that shows the issue you are having but not the details of your solution...doing this might even lead to you solving your own issues.
2:Reverse word than been enetered.
3:Randomise the word.


To give an idea and how to solve those two (out of many ways):

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
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <random>

std::string reverse(const std::string);
int randomNumber(int, int);
std::string randomizeWord(const std::string);

int main()
{
	std::string originalWord{"luminescent"};
	std::cout << "Original Word: " << originalWord << std::endl;
	std::cout << "Reverse Word: " << reverse(originalWord) << std::endl;
	std::cout << "Randomized Word: " << randomizeWord(originalWord) << std::endl;

}

/*
This function takes the original word from the user
and reverse it by using the for loop with the counter
set to the size of the word from the user and will save
each single character into the result variable.
*/
std::string reverse(const std::string originalWord)
{
	std::string result{ originalWord };
	std::size_t increment{0}; // Counter to be used as increment for each char it takes.

	for (std::size_t count = originalWord.size(); count > 0; count--)
	{
		result[increment] = originalWord[count - 1];
		increment++;
	}

	return result;
}

std::string randomizeWord(const std::string originalWord)
{
	std::string tempWord{ originalWord }; // Save the user inputted string into a temp to be manipulated.
	std::string singleCharacter{}; // To be use to obtain single characters to append to the result string later.
	std::string result{}; // To save the randomize word and return it back to the function caller.
	
	while (!tempWord.empty()) // As long as the temp string is not empty, keep looping.
	{
		const int randomNum{ randomNumber(0, tempWord.size() - 1) }; // Obtain the random number.
		singleCharacter = tempWord.at(randomNum); // Random the location of the temp string and save the character.
		result.append(singleCharacter); // Append the character into the result string.
		tempWord.erase(tempWord.begin() + randomNum); // Delete the used character to prevent reuse.
	}

	return result;
}

/*
This function uses the random library to obtain
a random number between 0 and the size of the
string minus one.
*/
int randomNumber(int min, int max)
{
	std::mt19937 rng(std::random_device{}());
	std::uniform_int_distribution<int> distrib(min, max);

	const int random = distrib(rng);

	return random;
}
it doesn't meet the criteria of my assignment


Of course not. It is to give you an idea.

For the switch, I would use each case to call the function to perform the operations. So, something 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <string>
#include <random>
#include <limits> // To be used with the cin.ignore.

//Function Prototypes.
std::string reverse(const std::string);
int randomNumber(int, int);
std::string randomizeWord(const std::string);
void displayMenu(int&);
std::string getWord();

int main()
{
	std::string originalWord{};
	bool keepLooping{true};
	int choice{};

	while (keepLooping)
	{
		displayMenu(choice);

		switch (choice)
		{
			case 1:
				std::cin.ignore(std::numeric_limits < std::streamsize >::max(), '\n');
				originalWord =  getWord();
				std::cout << "Original Word: " << originalWord << std::endl;
				keepLooping = true;
				break;

			case 2:
				std::cout << "Reverse Word: " << reverse(originalWord) << std::endl;
				keepLooping = true;
				break;

			case 3:
				std::cout << "Randomized Word: " << randomizeWord(originalWord) << std::endl;
				keepLooping = true;
				break;

			case 4:
				// Call the function to upper case the word here.
				keepLooping = true;
				break;

			case 5:
				// Call the function to lower case the word here.
				keepLooping = true;
				break;

			case 6:
				// Call the function to alphabetize the word here.
				keepLooping = true;
				break;

			case 7:
				std::cout << "Closing the program...\n";
				keepLooping = false;
				break;

			default:
				std::cout << "Please choose between 1-7\n";
				keepLooping = true;
		}
	}

}

/*
This function takes the original word from the user
and reverse it by using the for loop with the counter
set to the size of the word from the user and will save
each single character into the result variable.
*/
std::string reverse(const std::string originalWord)
{
	std::string result{ originalWord };
	std::size_t increment{0}; // Counter to be used as increment for each char it takes.

	for (std::size_t count = originalWord.size(); count > 0; count--)
	{
		result[increment] = originalWord[count - 1];
		increment++;
	}

	return result;
}

std::string randomizeWord(const std::string originalWord)
{
	std::string tempWord{ originalWord }; // Save the user inputted string into a temp to be manipulated.
	std::string singleCharacter{}; // To be use to obtain single characters to append to the result string later.
	std::string result{}; // To save the randomize word and return it back to the function caller.
	
	while (!tempWord.empty()) // As long as the temp string is not empty, keep looping.
	{
		const int randomNum{ randomNumber(0, tempWord.size() - 1) }; // Obtain the random number.
		singleCharacter = tempWord.at(randomNum); // Random the location of the temp string and save the character.
		result.append(singleCharacter); // Append the character into the result string.
		tempWord.erase(tempWord.begin() + randomNum); // Delete the used character to prevent reuse.
	}

	return result;
}

void displayMenu(int &choice)
{
	std::cout << "\n            String Manipulator\n";
	std::cout << "-----------------------------------------\n";
	std::cout << "1) Enter the word\n";
	std::cout << "2) Reverse the word\n";
	std::cout << "3) Randomize the word\n";
	std::cout << "4) Upper case the word\n";
	std::cout << "5) Lower case the word.\n";
	std::cout << "6) Alphabetical order the word.\n";
	std::cout << "7) Exit the program\n\n";
	std::cout << "Enter your choice: ";
	std::cin >> choice;
}

/*
This function is to obtain the word from the user.
It uses the getline to obtain multiple words with
spaces (i.e. Programming is fun!). It will return
the word back to function caller.
*/
std::string getWord()
{
	std::string word{};
	std::cout << "Enter a word: ";
	std::getline(std::cin, word);

	return word;
}

/*
This function uses the random library to obtain
a random number between 0 and the size of the
string minus one.
*/
int randomNumber(int min, int max)
{
	std::mt19937 rng(std::random_device{}());
	std::uniform_int_distribution<int> distrib(min, max);

	const int random = distrib(rng);

	return random;
}
Last edited on
This is the code i have for lowercase and uppecase:


First, I recommend that you break it down into two functions. Actually, you have broke it down here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Convert the string to upper case and return the new string
string
stringToUpper(string currentString)
{
    cout << "StringToUpper is not yet implemented\n";
    return currentString;
}
 
// Convert the string to lower case and return the new string
string
stringToLower(string currentString)
{
    cout << "StringToLower is not yet implemented\n";
    return currentString;
}


An example of how to lowercase the word would be like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
This function takes the word inputted by the user
and uses a for loop to iterate each character
and lower the case to be saved into a result variable
to be return to the caller function.
*/
std::string lowerCase(const std::string originalWord)
{
	std::string result{originalWord};

	for (std::size_t count = 0; count < result.size(); count++)
	{
		result[count] = std::tolower(originalWord[count]);
	}

	return result;
}


Another suggestion is to avoid the goto. You should use functions, instead.
Yes, it is for lowercase. Can you post the whole code together? It will be easier to solve the issue.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iostream>
#include <string>
#include <random>
#include <cctype> // To be used for tolower and toupper.
#include <limits> // To be used with the cin.ignore.

//Function Prototypes.
std::string reverse(const std::string);
std::string randomizeWord(const std::string);
std::string lowerCase(const std::string);
std::string upperCase(const std::string);
std::string alphabetize(const std::string);
int randomNumber(int, int);
void displayMenu(int&);
std::string getWord();

int main()
{
	std::string originalWord{};
	bool keepLooping{ true };
	int choice{};

	while (keepLooping)
	{
		displayMenu(choice);

		switch (choice)
		{

		case 1:
			std::cin.ignore(std::numeric_limits < std::streamsize >::max(), '\n');
			originalWord = getWord();
			keepLooping = true;
			break;

		case 2:
			std::cout << "Reverse Word: " << reverse(originalWord) << std::endl;
			keepLooping = true;
			break;

		case 3:
			std::cout << "Randomized Word: " << randomizeWord(originalWord) << std::endl;
			keepLooping = true;
			break;

		case 4:
			std::cout << "Uppercase Word: " << upperCase(originalWord) << std::endl;
			keepLooping = true;
			break;

		case 5:
			std::cout << "Lowercase Word: " << lowerCase(originalWord) << std::endl;
			keepLooping = true;
			break;

		case 6:
			std::cout << "Alphabetized Word: " << alphabetize(originalWord) << std::endl;
			keepLooping = true;
			break;

		case 7:
			std::cout << "Closing the program...\n";
			keepLooping = false;
			break;

		default:
			std::cout << "Please choose between 1-7\n";
			keepLooping = true;
		}
		std::cout << "Original Word: " << originalWord << std::endl;
	}

}

/*
This function takes the original word from the user
and reverse it by using the for loop with the counter
set to the size of the word from the user and will save
each single character into the result variable.
*/
std::string reverse(const std::string originalWord)
{
	std::string result{ originalWord };
	std::size_t increment{ 0 }; // Counter to be used as increment for each char it takes.

	for (std::size_t count = originalWord.size(); count > 0; count--)
	{
		result[increment] = originalWord[count - 1];
		increment++;
	}

	return result;
}

std::string randomizeWord(const std::string originalWord)
{
	std::string tempWord{ originalWord }; // Save the user inputted string into a temp to be manipulated.
	std::string singleCharacter{}; // To be use to obtain single characters to append to the result string later.
	std::string result{}; // To save the randomize word and return it back to the function caller.

	while (!tempWord.empty()) // As long as the temp string is not empty, keep looping.
	{
		const int randomNum{ randomNumber(0, tempWord.size() - 1) }; // Obtain the random number.
		singleCharacter = tempWord.at(randomNum); // Random the location of the temp string and save the character.
		result.append(singleCharacter); // Append the character into the result string.
		tempWord.erase(tempWord.begin() + randomNum); // Delete the used character to prevent reuse.
	}

	return result;
}

void displayMenu(int &choice)
{
	std::cout << "\n            String Manipulator\n";
	std::cout << "-----------------------------------------\n";
	std::cout << "1) Enter the word\n";
	std::cout << "2) Reverse the word\n";
	std::cout << "3) Randomize the word\n";
	std::cout << "4) Upper case the word\n";
	std::cout << "5) Lower case the word.\n";
	std::cout << "6) Alphabetical order the word.\n";
	std::cout << "7) Exit the program\n\n";
	std::cout << "Enter your choice: ";
	std::cin >> choice;
}

/*
This function is to obtain the word from the user.
It uses the getline to obtain multiple words with
spaces (i.e. Programming is fun!). It will return
the word back to function caller.
*/
std::string getWord()
{
	std::string word{};
	std::cout << "Enter a word: ";
	std::getline(std::cin, word);

	return word;
}

/*
This function uses the random library to obtain
a random number between 0 and the size of the
string minus one.
*/
int randomNumber(int min, int max)
{
	std::mt19937 rng(std::random_device{}());
	std::uniform_int_distribution<int> distrib(min, max);

	const int random = distrib(rng);

	return random;
}

/*This function takes the word inputted by the user
and uses a for loop to iterate each character
and lower the case to be saved into a result variable
to be return to the caller function.
*/
std::string lowerCase(const std::string originalWord)
{
	std::string result{ originalWord };

	for (std::size_t count = 0; count < result.size(); count++)
	{
		result[count] = std::tolower(originalWord[count]);
	}

	return result;
}

/*This function takes the word inputted by the user
and uses a for loop to iterate each character
and upper the case to be saved into a result variable
to be return to the caller function.
*/
std::string upperCase(const std::string originalWord)
{
	std::string result{ originalWord };

	for (std::size_t count = 0; count < result.size(); count++)
	{
		result[count] = std::toupper(originalWord[count]);
	}

	return result;
}

/*
This function uses the bubble sort algorithm.
Inside the function is a for loop nested inside a 
do-while loop. The for loop sequences through the 
entire array, comparing each element with its neighbor 
and swapping them if necessary. Anytime two elements 
are exchanged, the flag variable swap is set to true.
The for loop is executed repeatedly until it can 
sequence through the entire array
without making any exchanges.
*/
std::string alphabetize(const std::string originalWord)
{
	bool swap;
	std::string result{originalWord};
	char temp{}; // To save characters to swap the letters.

	do
	{
		swap = false;
		for (std::size_t count = 0; count < result.size() - 1; count++)
		{
			if (result[count] > result[count + 1])
			{
				temp = result[count];
				result[count] = result[count + 1];
				result[count + 1] = temp;
				swap = true;
			}
		}
	} while (swap);

	return result;
}
Last edited on
Below is your code with my recommendations.


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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <string>
#include <algorithm>
#include <random>
#include <cctype> // Needed for toupper and tolower

using namespace std;

// Prompt for a new word and return it.

// Print a "thanks for playing" message
//void
void QuitNow(/*void*/)
{
	cout << endl << endl << "Thank you for using StringWorld - please come back soon" <<
		endl;
}

// Reverse a string
//string
string ReverseWord(string originalString)
{
	string localString;
	int len;
	cout << "Original String: " << originalString << "\n";
	len = originalString.length();
	cout << "Total Length: " << len << "\n";
	for (int i = len; i >= 0; i++) {
		cout << "now lets see what localString contains: " << localString << "\n";
	}
	return localString;
}

// Convert the string to upper case and return the new string
//string
string stringToUpper(string currentString)
{
	string result{ currentString };

	for (unsigned int count = 0; count < result.size(); count++)
	{
		result[count] = toupper(currentString[count]);
	}

	return result;
	//cout << "StringToUpper is not yet implemented\n";
	//return currentString;
}

// Convert the string to lower case and return the new string
//string
string stringToLower(string currentString)
{
	string result{ currentString };

	for (unsigned int count = 0; count < result.size(); count++)
	{
		result[count] = tolower(currentString[count]);
	}

	return result;
	//cout << "StringToLower is not yet implemented\n";
	//return currentString;
}

/*
This function uses the random library to obtain
a random number between 0 and the size of the
string minus one.
*/
int randomNumber(int min, int max)
{
	std::mt19937 rng(std::random_device{}());
	std::uniform_int_distribution<int> distrib(min, max);

	const int random = distrib(rng);

	return random;
}

// Randomize the string and return the randomized string
//string
string randomizeString(string currentString)
{

	string tempWord{ currentString }; // Save the user inputted string into a temp to be manipulated.
	string singleCharacter{}; // To be use to obtain single characters to append to the result string later.
	string result{}; // To save the randomize word and return it back to the function caller.

	while (!tempWord.empty()) // As long as the temp string is not empty, keep looping.
	{
		const int randomNum{ randomNumber(0, tempWord.size() - 1) }; // Obtain the random number.
		singleCharacter = tempWord.at(randomNum); // Random the location of the temp string and save the character.
		result.append(singleCharacter); // Append the character into the result string.
		tempWord.erase(tempWord.begin() + randomNum); // Delete the used character to prevent reuse.
	}

	return result;
	//cout << "randomizeString is not yet implemented\n";
	//return currentString;
}

// Sort the letters in the string and return the sorted string
//string
string sortLetters(string currentString)
{
	bool swap;
	std::string result{ stringToLower(currentString) }; // Convert to lower case to sort the letters.
	char temp{}; // To save characters to swap the letters.

	do
	{
		swap = false;
		for (std::size_t count = 0; count < result.size() - 1; count++)
		{
			if (result[count] > result[count + 1])
			{
				temp = result[count];
				result[count] = result[count + 1];
				result[count + 1] = temp;
				swap = true;
			}
		}
	} while (swap);

	return result;
	//cout << "sortLetters is not yet implemented\n";
	//return currentString;
}

void Menu(/*void*/)
{
	string currentString;
	int menuOption;
	bool quit = false;

	cout << "Welcome to StringWorld" << endl << endl;


	do {
		// Print the current word and the menu


		cout << "\t1. Reverse the current word or phrase" << endl;
		cout << "\t2. Convert the current word to uppercase" << endl;
		cout << "\t3. Convert the current word to lowercase" << endl;
		cout << "\t4. Randomize the letters in the current word." << endl;
		cout << "\t5. Sort the letters in the current word in ascending alphabetic order." << endl;
		cout << "\t0. Quit StringWorld" << endl;

		// Get teh user's selection
		string word;
		cout << "Enter a word: ";
		cin >> word;
		cout << "Please enter a valid option (1 - 5 or 0 to quit): ";
		cin >> menuOption;

		// Switch on the user's selction and do the right thing.
		switch (menuOption) {

		case 1:
			currentString = ReverseWord(word/*currentString*/);
			cout << "Reversed Word: " << currentString << endl;
			break;
		case 2:
			currentString = stringToUpper(word/*currentString*/);
			cout << "Uppercase Word: " << currentString << endl;
			//while (str[i])
			//{
			//	c = str[i];
			//	putchar(toupper(c));
			//	i++;
			//}
			break;
		case 3:
			currentString = stringToLower(word/*currentString*/);
			cout << "Lowercase Word: " << currentString << endl;
			//while (str[i])
			//{
			//	c = str[i];
			//	putchar(tolower(c));
			//	i++;
				break;
		case 4:
			currentString = randomizeString(word/*currentString*/);
			cout << "Randomized String: " << currentString << endl;
			break;
		case 5:
			currentString = sortLetters(word/*currentString*/);
			cout << "Sorted String: " << currentString << endl;
			break;
		case 0:
			QuitNow();
			quit = true;	// indicate that you should quit
			break;
			}
		} while (!quit);
	}



	
int main()
{
	Menu();
	return 0;
}
I didn't fix it on purpose. My advise is to work on it and if you have any question, ask away. You can take a look at my example code I posted it to give you an idea.
Last edited on
The error you have is a runtime error.
Line 28: "ill-defined for-loop: counts up from maximum"

In other words, you begin the count from the size of the string and then your loop increments; hence, it goes outside of the allocated memory.
If you still have this in your code, then:
1
2
3
4
len = originalString.length();
for ( int i = len; i >= 0; i++ )
{
}

* If the string has len characters, then valid indices are 0..len-1. The original[len] is one past the string.

* You increment the loop counter. On the second iteration the counter is len+1. You should decrement.
I do not know what you updated in the function below, but I wrote some comments with some things to keep in mind.
1
2
3
4
5
6
7
8
9
10
11
12
string ReverseWord(string originalString)
{
	string localString; // Nothing is assigned to this variable.
	int len;
	cout << "Original String: " << originalString << "\n";
	len = originalString.length();
	cout << "Total Length: " << len << "\n";
	for (int i = len; i >= 0; i++) {
		cout << "now lets see what localString contains: " << localString << "\n"; // It will display every loop and nothing is assigned to this variable.
	}
	return localString; // Since nothing is assigned to this variable, then the return is undefined or blank.
}
Last edited on
I can see your point. I got other obligations; therefore, I do not answer to homework related PMs. My reason is that beginners can benefit from it and others with more experienced can provide their valuable input. I recommend you to do a internet search about reversing strings or something in those terms. You should be close to finish. Happy coding!
closed account (z05DSL3A)
COO9 wrote:
...and i might delete this post after i solve this (don't want to have this coding just up in internet like this as anyone can access it)...

That kind of thing is not looked kindly on here. You have received a lot of help with this, best leave it to help others...
closed account (z05DSL3A)
As a long term member of this site I'm just offering some friendly advice. Take it or leave it.

I have been keeping an eye on this and wrote a bit of code for fun. I didn't want to side track your conversation with chicofeo but was going to suggest you keep the switch a bit neater and smaller by calling functions from it. Keeping it small makes it easier for the reader to comprehend the structure.

You may as well have the code to study...
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <random>
#include <algorithm>
#include <string>

void ShowMenu ();
void ShowWord (const std::string &str);
int GetChoice ();
void OnEnterWord (std::string &str);
void OnReverseWord (std::string &str);
void OnRandomiseWord (std::string &str);
void OnUpperCaseWord (std::string &str);
void OnLowerCaseWord (std::string &str);
void OnSortWord (std::string &str);
//-----------------------------------------------------------------------------
int main()
{
    std::string word{};
    bool proceed{ true };
    while (proceed)
    {
        ShowWord (word);
        ShowMenu ();
        switch (GetChoice ())
        {
            case 1: OnEnterWord (word);   continue;
            case 2: OnReverseWord (word); continue;
            case 3: OnRandomiseWord (word); continue;
            case 4: OnUpperCaseWord (word); continue;
            case 5: OnLowerCaseWord (word); continue;
            case 6: OnSortWord (word); continue;
            
            case 0: proceed = false;
        }
    }
}
//-----------------------------------------------------------------------------
void ShowMenu ()
{
    std::string text{ "\n"
        "\t1: Enter a word.\n"
        "\t2: Reverse the word.\n"
        "\t3: Randomise the word.\n"
        "\t4: Upper case the word\n"
        "\t5: Lower case the word\n"
        "\t6: Sort the word.\n\n"
        "\t0: Exit.\n\n" };

    std::cout << text;
}
//-----------------------------------------------------------------------------
void ShowWord (const std::string &str)
{
    std::cout << "\n\tCurent Word: " << str << "\n";
}
//-----------------------------------------------------------------------------
int GetChoice ()
{   
    int choice{ 0 };
    std::cout << "\tChoice: ";
    std::cin >> choice;
    return choice;
}
//-----------------------------------------------------------------------------
void OnEnterWord (std::string &str)
{
    std::cout << "\tEnter new word: ";
    std::cin >> str;
}
//-----------------------------------------------------------------------------
void OnReverseWord (std::string &str)
{
    std::reverse (str.begin (), str.end ());
}
//-----------------------------------------------------------------------------
void OnRandomiseWord (std::string &str)
{
    std::random_device rd;
    std::mt19937 g (rd ());
    std::shuffle (str.begin (), str.end (), g);
}
//-----------------------------------------------------------------------------
void OnUpperCaseWord (std::string &str)
{
    std::transform (str.begin (), str.end (), str.begin (), ::toupper);
}
//-----------------------------------------------------------------------------
void OnLowerCaseWord (std::string &str)
{
    std::transform (str.begin (), str.end (), str.begin (), ::tolower);
}
//-----------------------------------------------------------------------------
void OnSortWord (std::string &str)
{
    std::sort (str.begin (), str.end ());
}
Last edited on
closed account (z05DSL3A)
I guess he didn't take the advice...
Garçon: “Bienvenue à Paris!”
Tourist: “Gol, holy f***! Doesn’t anyone here speak American!!?
@COO9 initial post:
Hi am new to this.
Just wanted someone to help me with my assignment to asks me to do:
Make a menu
1:Enter a word.
2:Reverse word than been enetered.
3:Randomise the word.
4:Upper case the word
5:Lower case it
6:Organise in the alapabetical order of the word.
Exit.
It has to have a menu where the user renter a word then presses a number for example 2 then the program
reverses the word etc.
Thanks looking forward for the replies.
And yea I have started it and got basic codes done but am struggling on writing codes that aren’t from
online(plagiarism reasons)
Pages: 12