Thread: Help! Homework using Strings

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    48

    Help! Homework using Strings

    Hi, I'm a beginner programmer, and I need to write this program for homework for a college-level class. I've tried to run it numerous times, but I continually get the errors. I want to know if someone can check it over and let me know what I'm doing wrong, which is most likely everything. Thanks for any help!


    Write a program that reads a string containing exactly four words, (separated by * symbols) into a single string object. Next, extract each word from the original string and store each word in a string object. Then concatenate the words in reverse order to form another string. Display both the original and final strings. (Hint: To extract the words, you should use the 'find' member function to find each symbol *, assign the characters up to the * to one of the four string objects, and then remove those characters from the original string.)

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
    //First, I will declare the variables
    
    	string				original;
    	int				remove_1;
    	int				remove_2;
    	int				remove_3;
    	int				remove_4;
    	string				star;
    	string				word_1;
    	string				word_2;
    	string				word_3;
    	string				word_4;
    	string				reverse_1;
    	string				reverse_2;
    	string				reverse_3;
    	string				reverse_4;
    	string				final;
    
    //Next, I will gather the inputs
    
    	cout << "Please enter your words..." << endl;
    	cin >> original;
    	cout << "Please enter your method of spacing..." << endl;
    	cin >> star;
    
    //Next, I will separate the words
    
    	remove_1 = original.find(star);
    	reverse_1.assign(original, 0, remove_1);
    	original.erase(0, remove_1);
    	remove_2 = original.find(star);
    	reverse_2.assign(original, 0, remove_2);
    	original.erase(0, remove_2);
    	remove_3 = original.find(star);
    	reverse_3.assign(original, 0, remove_3);
    	original.erase(0, remove_3);
    	remove_4 = original.find(star);
    	reverse_4.assign(original, 0, remove_4);
    	final = reverse_4 + "*" + reverse_3 + "*" + reverse_2 "*" + reverse_1;
    
    //Next, I will compile the new string and display it on the screen
    
    	cout << "The original string was: " << original << endl;
    	cout << "The reversed string is: " << final << endl;
    
    return(0);
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    What errors are you getting?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    Code:
    1>c:\users\william\documents\visual studio 2005\projects\homework 4 number 9\homework 4 number 9\homework 4 number 9.cpp(45) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
    1>c:\users\william\documents\visual studio 2005\projects\homework 4 number 9\homework 4 number 9\homework 4 number 9.cpp(48) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
    1>c:\users\william\documents\visual studio 2005\projects\homework 4 number 9\homework 4 number 9\homework 4 number 9.cpp(51) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
    1>c:\users\william\documents\visual studio 2005\projects\homework 4 number 9\homework 4 number 9\homework 4 number 9.cpp(54) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
    1>c:\users\william\documents\visual studio 2005\projects\homework 4 number 9\homework 4 number 9\homework 4 number 9.cpp(56) : error C2143: syntax error : missing ';' before 'string'

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    	final = reverse_4 + "*" + reverse_3 + "*" + reverse_2 + "*" + reverse_1;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    I'm sorry but I'm not sure what you're getting at.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You're missing a +.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    Thanks, that did help some, but I'm still not getting the results that I want.

    Here is how I ran the program:

    Code:
    Please enter your words...
    
    word1*word2*word3*word4
    
    Please enter your method of spacing...
    
    *
    
    The original string was: *word2*word3*word4
    The reversed string is: ***word1
    
    Press any key to continue . . . 

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    12
    you should not declare variables in a list like that. like this is cleaner: int sum,product,remainder,etc;
    and i would try switching int to double

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    Well, that's mostly a cosmetic effect, I just need more help implementing the program. As far as I can see changing from int to double won't affect it, since the word won't be that long. If someone can point me in the right direction to get this implemented correctly. The question is listed in the beginning of the program I would greatly appreciate it.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Are you supposed to do this silliness?
    Code:
    	remove_1 = original.find(star);
    	reverse_1.assign(original, 0, remove_1);
    	original.erase(0, remove_1);
    	remove_2 = original.find(star);
    	reverse_2.assign(original, 0, remove_2);
    	original.erase(0, remove_2);
    	remove_3 = original.find(star);
    	reverse_3.assign(original, 0, remove_3);
    	original.erase(0, remove_3);
    	remove_4 = original.find(star);
    	reverse_4.assign(original, 0, remove_4);
    std::cin is whitespace delimited, so something like this is kinda close:
    Code:
    	cout << "Please enter your words..." << endl;
    	cin >> word_1 >> word_2 >> word_3 >> word_4;
    	cout << "Please enter your method of spacing..." << endl;
    	cin >> star;
    
       original = word_1 + " " + word_2 + " " + word_3 + " " + word_4;
    	final = word_4 + star + word_3 + star + word_2 + star + word_1;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    Well, that would be much easier, but our professor wants us to be able to enter four words in a single string separated by (*)'s and have the program reverse the order and display that. For example, if you user enters word1*word2*word3*word4, it should be displayed as word4*word3*word2*word1, so we can't enter multiple strings, only a single one. Also, I put a second step in there that allows the computer to search out the separator, but the professor wants that step eliminated from the user. So, all the user should input is the original string, and have the output of the reversed order, they shouldn't have to enter the (*) separator.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    std::string::find() is fine, but as an alternative you can consider std::getline() with '*' as the delimiter. One possibility is to read into a stack, then after processing the string, just pop everything from the stack and print.

    For example, if you user enters word1*word2*word3*word4, it should be displayed as word4*word3*word2*word1, so we can't enter multiple strings, only a single one. Also, I put a second step in there that allows the computer to search out the separator, but the professor wants that step eliminated from the user. So, all the user should input is the original string, and have the output of the reversed order, they shouldn't have to enter the (*) separator.
    So, the user enters "word1 word2 word3 word4"? If so, what is the point of the * as a separator?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Aha. I need that reading for comprehension book again.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    laserlight:

    I'm not sure, it's just the problem I know it seems kind of silly. How do you use the commands that you mentioned. Sorry, I'm just getting started with this and I really am not that well versed. Thanks.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can take a look at getline(). The standard library provides a stack, so you could use a std::stack<std::string>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM