Posted: 9/15/2013 1:40:22 AM EDT
|
I'm working on a program for my C++ class and I'm having some trouble. I've never asked The Hive for help before, but I need to get this done.
The problem statement reads: 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. 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). I have been able to prompt to input and display the original input, but I don't know how to do anything in between. This is my source code so far: //Functions.cpp #include <string> #include <iostream> using namespace std; int main() { string singlestring;//Input: all four words //in one string object string word1;//Output: first word string word2;//Output: second word string word3;//Output: third word string word4;//Output: fourth word //Read user's input. cout << "Enter four words separated by * :"; cin >> singlestring; //Display original string. cout << singlestring; return 0; } Thanks. |
|
Use this:
http://www.cplusplus.com/reference/string/string/find/ And this: http://www.cplusplus.com/reference/string/string/substr/ You can solve the problem with a combination of those two. |
|
I now have this:
//Functions.cpp #include <string> #include <iostream> using namespace std; //Functions used: void getwords();//Find and assign words int main() { string originalstring;//Input: all four words //in one string object string star;//Spacing method string inputword1; string inputword2; string inputword3; string inputword4; string outputword1;//Ouput: first word string outputword2;//Output: second word string outputword3;//Output: third word string outputword4;//Output: fourth word string final;//Output: final statement cout << "Enter four words, each followed by * :"; cin >> inputword1, inputword2, inputword3, inputword4; originalstring = inputword1 + inputword2 + inputword3 + inputword4; //Read user's input. getwords(); //Display original string. cout << originalstring << endl; //Display final string. cout << final << endl; return 0; } //Find each word in original string. Remove each word from original string as it is identified. void getwords() { string originalstring;//Input: all four words double word1;//Object assign: first word double word2;//Object assign: second word double word3;//Object assign: third word double word4;//Object assign: fourth word string outputword1; string outputword2; string outputword3; string outputword4; string final; word1 = originalstring.find("*"); outputword1.assign(originalstring, 0, word1); originalstring.erase(0, word1); word2 = originalstring.find("*"); outputword2.assign(originalstring, 0, word2); originalstring.erase(0, word2); word3 = originalstring.find("*"); outputword3.assign(originalstring, 0, word3); originalstring.erase(0, word3); word4 = originalstring.find("*"); outputword4.assign(originalstring, 0, word4); final = outputword1 + ", " + outputword2 + ", " + outputword3 + ", " + outputword4; } But I am getting a compiler error: passing `double' for converting 2 of `std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::erase(typename _Alloc::size_type, typename _Alloc::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' Any idea what I need to change? |
|
Quoted:
I now have this: //Functions.cpp #include <string> #include <iostream> using namespace std; //Functions used: void getwords();//Find and assign words int main() { string originalstring;//Input: all four words //in one string object string star;//Spacing method string inputword1; string inputword2; string inputword3; string inputword4; string outputword1;//Ouput: first word string outputword2;//Output: second word string outputword3;//Output: third word string outputword4;//Output: fourth word string final;//Output: final statement cout << "Enter four words, each followed by * :"; cin >> inputword1, inputword2, inputword3, inputword4; originalstring = inputword1 + inputword2 + inputword3 + inputword4; //Read user's input. getwords(); //Display original string. cout << originalstring << endl; //Display final string. cout << final << endl; return 0; } //Find each word in original string. Remove each word from original string as it is identified. void getwords() { string originalstring;//Input: all four words double word1;//Object assign: first word double word2;//Object assign: second word double word3;//Object assign: third word double word4;//Object assign: fourth word string outputword1; string outputword2; string outputword3; string outputword4; string final; word1 = originalstring.find("*"); outputword1.assign(originalstring, 0, word1); originalstring.erase(0, word1); word2 = originalstring.find("*"); outputword2.assign(originalstring, 0, word2); originalstring.erase(0, word2); word3 = originalstring.find("*"); outputword3.assign(originalstring, 0, word3); originalstring.erase(0, word3); word4 = originalstring.find("*"); outputword4.assign(originalstring, 0, word4); final = outputword1 + ", " + outputword2 + ", " + outputword3 + ", " + outputword4; } But I am getting a compiler error: passing `double' for converting 2 of `std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::erase(typename _Alloc::size_type, typename _Alloc::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' Any idea what I need to change? word1-word4 need to be integers, not doubles. Double is a floating point value, you can't use a floating point value as a string index. Just my guess. I didn't look at the method definitions. |
|
Quoted:
word1-word4 need to be integers, not doubles. Double is a floating point value, you can't use a floating point value as a string index. Just my guess. I didn't look at the method definitions. You're right. I am using those to store markers, not actual words--they should be integers. Thanks. I took out the extraneous void function too, and it runs like a champ. |
|
Quoted:
You're right. I am using those to store markers, not actual words--they should be integers. Thanks. I took out the extraneous void function too, and it runs like a champ. Quoted:
Quoted:
word1-word4 need to be integers, not doubles. Double is a floating point value, you can't use a floating point value as a string index. Just my guess. I didn't look at the method definitions. You're right. I am using those to store markers, not actual words--they should be integers. Thanks. I took out the extraneous void function too, and it runs like a champ. Now figure out how to do it in about 5 lines with a loop... |