1. ## Homework problem!

Hey guys, heres the instructions for my homework:
1. The program should start by asking the user to enter a word, a sentence, or a string of numbers. Store whatever the user enters into a C++ string.

2. The program should then display the following menu (you must use ‘Q’ for quit):

----------------------------------------
1) Inverse String
2) Reverse String
3) To Uppercase
4) Count Number Words
5) Count Consonants
6) Enter a Different String
7) Print the String
Q) Quit

If the user selects 1: Inverse the upper and lower case letters of the string. If the string contains numeric characters or special characters do not change them. NOTE: This option should actually change the string to its inverse. Note this option does not display the changed string. If a user wanted to inverse the string and then display the string’s inverse they would select option 1 and then they would select option 7.

Example: If the string is: My name is John and I am 20 years old.
The inverse would be: mY NAME IS jOHN AND i AM 20 YEARS OLD.

If the user selects 2: – Reverse the order of the characters in the string. NOTE: This option should actually change the string to its reverse. Note this option does not display the changed string. If a user wanted to reverse the string and then display the string’s reverse they would select option 2 and then they would select option 7.

Example: If the string is: 2015
The inverse would be: 5102

¨If the user selects 3: Convert all of the characters in the string to uppercase. If the String contains numeric characters or special characters do not change them. NOTE: This option should actually change the string to all uppercase letters. Note this option does not display the changed string. If a user wanted to change the string to uppercase and then display the new string (in all uppercase) they would select option 3 and then they would select option 7.

¨ If the user selects 4: Call a function named countWords that counts the number of words in the current string and displays a message stating how many words are in the string. This option actually should print something.

Examples: The string “2015” has one word
The string “Hello World” has two words

¨ If the user selects 5: Call a function named countConsonants that counts the number of consonants in the current string and displays a message indicating how many consonants the string contains. So this option does print something. Consonants are letters that aren’t vowels (anything EXCEPT a, e, i, o, u).

Example: If the string is: Hello
The number of consonants is: 3
So display: “The number of consonants in the string is 3.”

¨ If the user selects 6: Let the user enter another string for processing. (This should just change the string stored in the original string variable you created at the beginning of the program)

¨ If the user selects 7: Print the String
o So if the original string was “Hello” and the user processed the string with option 3 followed by option 2, followed by option 8, the string would print out as “OLLEH” (This is hello first converted to uppercase and then reversed).

¨ If the user selects ‘Q’ or ‘q’: Quit the program

¨ Allow the user to continue processing strings (using the menu) until they select ‘Q’ or ‘q’ to quit

¨ If the user makes an invalid menu selection, print an error message to the screen

here's the code I have so far

Code:
```#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;

int countWords(string);

int main ()
{
string str;
int i = 0;
char choice;

cout << "Enter a word, sentence, or a string of numbers" << endl;
getline(cin, str);

cout << "---------------------------------------" << endl;
cout << "1) Inverse String " << endl;
cout << "2) Reverse String " << endl;
cout << "3) To Uppercase " << endl;
cout << "4) Count Number Words " << endl;
cout << "5) Count Consonants " << endl;
cout << "6) Enter a Different String " << endl;
cout << "7) Print the String " << endl;
cout << "Q) Quit " << endl;
cin >> choice;

if(choice == '1')
{
for( i = 0; i < str.length(); i++)
{
if(isupper(str[i]))
{
str[i] = tolower(str[i]);
}
else if(tolower(str[i]))
{
str[i] = toupper(str[i]);
}
}
str = str[1];
}
if(choice == '2')
{
for (i = str.length()-1; i >= 0; i--)
{
str = str[i];
}

}
if(choice == '3')
{
for(i = 0; i < str.length(); i++)
{
if(tolower(str[i]))
{
str[i] = toupper(str[i]);
}

}
cout << str <<endl;
}
if(choice == '4)
{
countWords(

system("PAUSE");
return 0;
}```
the question I have is:
1)how do I get it so it displays the new string whenever i press 7?

2)how would I construct the fuction countWords and countConsonants?

3)and in order for the menu reptition I would have to create a do while statement right? i would much appreciate a basic template, such as
do
if
statement
if
statement

something basic like that

I understand if the questions may seem too demeaning or me being lazy on my part, but I seriously suck at programming as my skills in word problems and basic problem solving seriously SUCKS.

2. It looks like you're getting there. How about having a go with the bits your unsure about.
Don't worry if you get it wrong, mistakes are all part of the learning process.

Make sure your code compiles before you go further though. Line 70 has an obvious error.

3. One thing I would suggest on this problem is doing the last three items on the menu first.

6) Enter a Different String
7) Print the String
Q) Quit

Tim S.

4. Thats a decent effort with nice formatting, you should take advantage of the member functions in std::string to accomplish your tasks, check the c++ reference pages for all of the built in parsing tools for the class

5. Originally Posted by stahta01
One thing I would suggest on this problem is doing the last three items on the menu first.

6) Enter a Different String
7) Print the String
Q) Quit

Tim S.
Would I have to make a do while program? And I still don't know how to make it so whenever I press 7 it prints the new string, I only know how to do it so whenever the user presses whatever number it prints the new string then instead of pressing 7.

6. ## at first glance

I noticed that you were using string, which is part of STL. If you have to use STL, here is something to consider...
basic_string<char> str2;
basic_string<char> ::iterator itr;

then we can iterate through the string using the iterator for(itr= str2.begin(); itr!=str2.end(); itr++) etc... But if you like using the getline() method, and use of STL doesn't matter, I would declare a basic string, use getline() to make sure we don't have any buffer overruns... better if you just see...
//excuse my sloppy code, I usually only use the win32 api for all my string manipulations (strsafe.h for instance) and I only use unicode, wow,
//this is a blast from the stone age!
int i;
string str1;
char* cstr;
getline(cin,str1);
i = str1.length();
cstr = new char[i];
cstr =(char*) str1.c_str();

//now you can index the string and make your changes. i is the max length you should iterate through the string. Other than that, I would make a separate function for the menu. Its been a long time since I viewed a single threaded app Whew!

Good luck and don't be affraid to consult me on any further topics. I wish you the best of luck! From the looks of your code I know you will make it! You are off to a great start, keep it up and help C++ thrive

7. Originally Posted by cowell_theodore
I noticed that you were using string, which is part of STL. If you have to use STL, here is something to consider...
basic_string<char> str2;
basic_string<char> ::iterator itr;

then we can iterate through the string using the iterator for(itr= str2.begin(); itr!=str2.end(); itr++) etc... But if you like using the getline() method, and use of STL doesn't matter, I would declare a basic string, use getline() to make sure we don't have any buffer overruns... better if you just see...
//excuse my sloppy code, I usually only use the win32 api for all my string manipulations (strsafe.h for instance) and I only use unicode, wow,
//this is a blast from the stone age!
int i;
string str1;
char* cstr;
getline(cin,str1);
i = str1.length();
cstr = new char[i];
cstr =(char*) str1.c_str();

//now you can index the string and make your changes. i is the max length you should iterate through the string. Other than that, I would make a separate function for the menu. Its been a long time since I viewed a single threaded app Whew!

Good luck and don't be affraid to consult me on any further topics. I wish you the best of luck! From the looks of your code I know you will make it! You are off to a great start, keep it up and help C++ thrive
Hey sorry but I did not understand a word of what you just said. As I said before, I'm a complete noob at this so I literally did not understand a single word of what you just said

8. I'm trying to make the function countWord but I'm having some problems with it. i put char countWords(string); the function name before the main function and i call it by doing

Code:
```if(choice == '4')
{
word = countWords (str)
}```
right? str if you look at my program is the variable for the user when he/she first enters the data

and then the actual function:

Code:
```char countWords (string)
{

string word;
for(int wordCount = 0; ss >> word; wordCount++);

stringstream ss(str);

}```
is this right? its saying its wrong and it wont compile

i also need to know hwo to make it so whenever the user presses 7 it prints the new string result

9. Originally Posted by cowell_theodore
I noticed that you were using string, which is part of STL. If you have to use STL, here is something to consider...
basic_string<char> str2;
basic_string<char> ::iterator itr;

then we can iterate through the string using the iterator for(itr= str2.begin(); itr!=str2.end(); itr++) etc...
You don't need to refer to basic_string for that. Just do:
Code:
```string str2;
string::iterator itr;```
The only time I've ever had a desire to reference basic_string was to make a tstring.