Issue with strings

Hi,

I am currently trying to make my output display but I can't get it working

I would like it to display like this:

***********
*--------------*//ignore the - signs as I had to use them to hold position of *
* 5! is: 120 *
*--------------*//ignore the - signs as I had to use them to hold position of *
***********

and I am trying to do this as a function. Here is my code but it won't compile, I think it has something to do with using int value in the string "message"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

//number is entered by user and is of type int and it is this number my program calculates the factoral for.
//factoral is of type int and is the value of the factoral of the number entered


void print_factoral (int number, int factoral)
{
    const string message = "" + number + "! is: " + factoral + "";
    const string spaces (message.size(), " ");//used to build the 2nd and 4th line of output
    const string second = "* " + spaces + " *";//creates 2nd and fourth line of output
    const string first (second.size(), "*");//creates the 1st and 5th line of output
    
    cout << endl;
    cout << first << endl;
    cout << second << endl;
    cout << "* " << message << " *" << endl;
    cout << second << endl;
    cout << first << endl;
}
Try using a stringstream.
1
2
3
    ostringstream oss;
    oss << " " << number << "! is " << factorial << " ";
    string msg = oss.str();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void print_factoral (int number, int factoral)
{

	ostringstream oss;//creating a stringstream
	oss << "" << number << "! is " << factoral << "";//adding display message to stringstream
    const string message = oss.str();//assigning stringstream to message
    const string spaces (message.size(), " ");//used to build the 2nd and 4th line of output
    const string second = "* " + spaces + " *";//creates 2nd and fourth line of output
    const string first (second.size(), "*");//creates the 1st and 5th line of output
    
    cout << endl;
    cout << first << endl;
    cout << second << endl;
    cout << "* " << message << " *" << endl;
    cout << second << endl;
    cout << first << endl;
    
}


It isn't working for me but I have no experience of using stringstream so not sure if I have done it correctly.

The program isn't compiling and I am getting an error saying invalid conversion from const char to char.

I am getting this message whether or not I use string or const string.
The error is here:
 
    const string spaces (message.size(), " "); // double quotes - error 

should be:
 
    const string spaces (message.size(), ' '); // single quotes - correct  


minor point, you have:
 
    oss << "" << number << "! is " << factoral << "";

but could simplify to:
 
    oss << number << "! is " << factoral;
Last edited on
Thanks very much for all the help mate, really appreciate it.
Topic archived. No new replies allowed.