Multiplication with a float and a string? [Homework]

Oct 15, 2015 at 6:13pm
This is my first post on the forums, so I must apologize if this goes against what the forum is for.

In my C++ course for HS, the assignment is to compile a horrendous set of code, and write the error messages, then explain what you think they mean. The part is getting to me, however is, after the compiling and explaining, you are to fix the code. I have done this for the most part, except that there is a segment that I am fighting with.

Troubling segment:
 
 cout << float x = 5.0f * str << end;



The point of struggle is at "cout << float x....." where it attempts to multiply float 5.0 and str. Is this something trivial that I am just missing, should it not exist at all, or is there something deep to this?
Oct 15, 2015 at 6:26pm
It's a nonsensical statement. It might make sense to multiply a string by an integer and thus repeat the string an integer number of times, but it doesn't make any sense to multiply a string by a floating point type.

Though, maybe the string contains a number and so you just need to convert the string to a float first. In that case it might be written as:
1
2
float x = 5.0f * your_conversion_function(str);
cout << x << endl;
Oct 15, 2015 at 6:33pm
I propose I should have included the entire "program," however all of my corrections were made and saved on /my/ desktop. Currently using an alt, so the best I have is the jumbled up mess of nonsense. But, the string is simply "Hello world!"


I apologize for you having to see this, I fixed all what I immediately remember;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

int main ( )
{
 string str = "Hello World!"

 cout << str << endl;

 cout << float x = 5.0f * str << end;

 int 65Num = 65;

 cout << "65Num = " < 65Num << endl;
}


After the "int main()" line, I touched nothing as I currently don't have the revisions I did in front of me. I am truly just thinking that the " * str " isn't supposed to be there.
Oct 15, 2015 at 6:40pm
cout << float x =

float x = 5.0f * str

Neither of these things are legal.
Oct 15, 2015 at 7:44pm
I think line 11 should just be completely removed.
Oct 15, 2015 at 9:10pm
Another poorly-designed homework.

Outside of someone saying "this is what the program is supposed to do", there is no way to know what "float times string" is intended to mean.

You might simply declare the float, assign it's value and print it, then print the string after that. (You might argue to your professor that << was meant instead of *.)

Good luck!
Oct 16, 2015 at 8:03pm
Thank you all who answered! At this moment, I'm going to go with Duoas' suggestion, and will report back with either a success, a failure, or a second attempt.
Oct 21, 2015 at 6:29pm
It was a success! Using Duoas' recommendation, an A+ sits proudly on my gradebook. Thank you all contributors!
Topic archived. No new replies allowed.