Thread: Homework problem!

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    16

    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):

    USE THIS MENU TO MANIPULATE YOUR STRING
    ----------------------------------------
    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 << "USE THIS MENU TO MANIPULATE YOUR STRING" << endl;
            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
    menu
    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.

    Thanks in advance

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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

    Because, it would help you to test your program faster.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    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
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    Quote Originally Posted by stahta01 View Post
    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

    Because, it would help you to test your program faster.

    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. #6
    Registered User
    Join Date
    Apr 2012
    Location
    Michigan
    Posts
    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
    Last edited by cowell_theodore; 04-22-2012 at 08:28 PM.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    Quote Originally Posted by cowell_theodore View Post
    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. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    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. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by cowell_theodore View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with homework
    By somniferium in forum C Programming
    Replies: 21
    Last Post: 10-15-2011, 12:09 PM
  2. homework problem Please Help!
    By alexwink in forum C Programming
    Replies: 24
    Last Post: 10-27-2006, 08:20 AM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Homework problem
    By bacon in forum C Programming
    Replies: 1
    Last Post: 04-24-2002, 07:21 AM