Warning

 

Close

Confirm Action

Are you sure you wish to do this?

Confirm Cancel
BCM
User Panel

Site Notices
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.
Link Posted: 9/15/2013 1:44:44 AM EDT
[#1]
Link Posted: 9/15/2013 3:42:11 AM EDT
[#2]
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.

Link Posted: 9/15/2013 5:09:19 AM EDT
[#3]
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?
Link Posted: 9/15/2013 5:48:05 AM EDT
[#4]
Discussion ForumsJump to Quoted PostQuote History
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?
View Quote

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.
Link Posted: 9/15/2013 12:05:50 PM EDT
[#5]
Discussion ForumsJump to Quoted PostQuote History
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.
View Quote


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.
Link Posted: 9/15/2013 2:31:17 PM EDT
[#6]
Discussion ForumsJump to Quoted PostQuote History
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.
View Quote View All Quotes
View All Quotes
Discussion ForumsJump to Quoted PostQuote History
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...
Link Posted: 9/15/2013 2:33:14 PM EDT
[#7]
Discussion ForumsJump to Quoted PostQuote History
Quoted:
Now figure out how to do it in about 5 lines with a loop...
View Quote

Or for a string with an unknown amount of delimiters, from 0 to n (might provide a little more direction)
Link Posted: 9/16/2013 1:07:28 AM EDT
[#8]
I wasn't allowed to use loops.  That's where the trouble started.  Thanks for the help guys.
Link Posted: 11/4/2013 1:43:06 AM EDT
[#9]
As for me it`ll better to use arrays with char data type

for example
...
char *strings[2] = {"first word", "second word"};
Close Join Our Mail List to Stay Up To Date! Win a FREE Membership!

Sign up for the ARFCOM weekly newsletter and be entered to win a free ARFCOM membership. One new winner* is announced every week!

You will receive an email every Friday morning featuring the latest chatter from the hottest topics, breaking news surrounding legislation, as well as exclusive deals only available to ARFCOM email subscribers.


By signing up you agree to our User Agreement. *Must have a registered ARFCOM account to win.
Top Top