![]() |
| | #1 |
| Registered User Join Date: Sep 2007
Posts: 48
| Help! Homework using Strings 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);
}
|
| wco5002 is offline | |
| | #2 |
| Registered User Join Date: Apr 2006
Posts: 1,307
| 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. |
| King Mir is offline | |
| | #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' |
| wco5002 is offline | |
| | #4 |
| Just Lurking 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.* |
| Dave_Sinkula is offline | |
| | #5 |
| Registered User Join Date: Sep 2007
Posts: 48
| I'm sorry but I'm not sure what you're getting at. |
| wco5002 is offline | |
| | #6 |
| Registered User Join Date: Apr 2006
Posts: 1,307
| 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. |
| King Mir is offline | |
| | #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 . . . |
| wco5002 is offline | |
| | #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 |
| Chaosreborn is offline | |
| | #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. |
| wco5002 is offline | |
| | #10 |
| Just Lurking 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); 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.* |
| Dave_Sinkula is offline | |
| | #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. |
| wco5002 is offline | |
| | #12 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,296
| 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. Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #13 |
| Just Lurking 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.* |
| Dave_Sinkula is offline | |
| | #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. |
| wco5002 is offline | |
| | #15 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,296
| You can take a look at getline(). The standard library provides a stack, so you could use a std::stack<std::string>.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strings Program | limergal | C++ Programming | 4 | 12-02-2006 03:24 PM |
| Programming using strings | jlu0418 | C++ Programming | 5 | 11-26-2006 08:07 PM |
| Reading strings input by the user... | Cmuppet | C Programming | 13 | 07-21-2004 06:37 AM |
| damn strings | jmzl666 | C Programming | 10 | 06-24-2002 02:09 AM |
| menus and strings | garycastillo | C Programming | 3 | 04-29-2002 11:23 AM |