Error with stringstream

Hi, I'm just doing a test with stringstreams for another program I need to write so that I know I understand what stringstream does. I'm getting a message at the end of the loop for this program though saying "std::stringstream' does not define this operator or a conversion to a type acceptable to the predefined operator"

Here's my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main()
{
	stringstream message, fMessage;
	string y, z;	//y is message output, z is fMessage output
	int added=2, i, x;	//added is the shift for message, i is loop index, x is converter

	message<<"I am a stringstream 2";


	y=message.str();

	cout<<y<<"\n";

	for(i=1;i<20;i++)   //converts message with shift
	{
		x=(int)y[i];
		x+=added;
		fMessage[i]=(char)x;  //where the error was flagged
	}
	z=fMessage.str();

	cout<<z;

	return 0;
}


I don't understand the error. Could someone explain it to me and attempt to help me fix it so that the program will work?
Last edited on
Line 7 you mean >>, not <<.

No, it's suppose to be <<. The error's on line 18 anyhow.
Last edited on
Well, obviously, std::stringstream has no [] operator.
http://www.cplusplus.com/reference/iostream/stringstream/
Ok. When I put [20] for message and fMessage, I got even more errors. So that didn't work.
I don't understand what you are trying to do. There is no need for stringstreams if all you want to do is operate on them as if they were strings!
This is a test program. The actual program I need to write is as follows:

"Write a C++ program that encodes the user's string so that he can send a secret message to his friends. In order to encode the string, we will use an integer offset value that is added to each letter's ASCII value thus making it a new ASCII character. The letter 'A' is ASCII 65. If we added 10 to it, it becomes ASCII 75, which is the letter K. For example, if our message was MEET ME AT 8, adding 10 to each letter results in the message WOO^*WO*K^*B.

"You'll need to write a function named AskForString that obtains the user's string. Request the user to use capital letters, not lowercase. Numbers are fine too. The main function generates the encoding integer (also known as the encryption key) which is a random number between 1 and 35. Pass it and the string to the Encoder function. It returns the encoded string. We need a decoder function too. It is passed the key and the encoded message--which returns the original message in a string. Write the original, encoded and decoded message and key value to the screen from main. (Hint: to encode/decode you'll need to pull each character out of the string, adjust it, and then use a stringstream object to obtain the new string.)"

As you can see, the program calls for the use of stringstream. Now whether I'm using it correctly, that's for someone else to tell me. I'm getting an error, and that generally says to me that I'm doing something wrong. Would someone like to tell how I can do something right?
I'm not asking for people to do my assignment. I just want to know how I can get the stringstream to work in the test that I'm doing in the first post.
What an unusual request... Anyway, it sounds as if you are to have a string and use a for loop and the string's [] operator to get each char from the string and then use the << operator on a stringstream to add a char at a time to it and finally call the stringstream's str() method to get the string. That's the only thing I could think of, but that still doesn't change the fact that it's kind of dumb!
Seymore, your wording it confusing. Could you write out an example in code?
Anyone else that has any idea on what to do would be great as well.
Why is this still here?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <sstream>
#include <string>

int main(){
	std::stringstream message, fMessage;
	std::string y, z;	//y is message output, z is fMessage output
	int added=2, i, x;	//added is the shift for message, i is loop index, x is converter
	message<<"I am a stringstream 2";


	y=message.str();

	std::cout<<y<<"\n";

	for(i=1;i<20;i++)   //converts message with shift
		y[i]+=added;
	fMessage<<y;

	std::cout <<fMessage.str();

	return 0;
}

Next time use strings and get it over with.
Well gee. Thanks for the help. The reason I was posting this problem in the first place though is well, let me think, was because I didn't know what I was doing. If I knew to do this in the first place, do you think I would've bothered to post here? Don't think so.

Next time, skimp the attitude, k?
Well, I did say that stringstreams didn't have the [] operator, didn't I? I would have expected you to make the connection that you have to convert to a string (the stringstream seems, therefore, like an unnecessary middle man to me, but what the hell. It's your design. Just make sure never to put anything like that inside a loop).
I barely know what I'm doing. What do you think I'm in a help forum for? And I'm just doing what the assignment wants me to do.
Topic archived. No new replies allowed.