x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 262

# An easy question

If the user inputs 1, 10, 12 , the output of the following program are:
1: *
*******10: **********
*******12: ************

How can I get the following output?
1: *
10: **********
12: ************

#include <iostream.h>
#include <iomanip.h>
int main(){
int a,b,c;
cout << "Please enter three integers:" ;
cin >> a >> b >> c ;
cout <<setw(9) <<  a << ": " << setw(a+1) << setfill('*') << endl
<<setw(9) <<  b << ": " << setw(b+1) << setfill('*') << endl
<<setw(9) << c <<": " << setw(c+1) << setfill('*') << endl;

return 0;
}
0
CG
• 3
• 3
1 Solution

Commented:
cout << a << ": " << setw(a+1) << setfill('*') << endl
<< b << ": " << setw(b+1) << setfill('*') << endl
<< c <<": " << setw(c+1) << setfill('*') << endl;

0

Author Commented:
1: *
10: **********
12: ************
I need the output :
1: *
10: **********
12: ************
0

Author Commented:
I'm glad that I have solved my problem.
0

Commented:
#include <iomanip.h>
#include <iostream.h>

int main(){

int a,b,c;
cout << "Please enter three integers:" ;
cin >> a >> b >> c ;
cout <<setw(2) <<a << ": " << setw(a+1) << setfill('*') << endl
<<setw(2) <<b << ": " << setw(b+1) << setfill('*') << endl
<<setw(2) << c <<": " << setw(c+1) << setfill('*') << endl;

return 0;
}
0

Commented:
I'm sorry, didn't realize you needed the leading spaces, this should fix that.

#include <iomanip.h>
#include <iostream.h>

int main(){

int a,b,c;
cout << "Please enter three integers:" ;
cin >> a >> b >> c ;
cout << "    " <<setw(2) <<a << ": " << setw(a+1) << setfill('*') << endl
<< "    " <<setw(2) <<b << ": " << setw(b+1) << setfill('*') << endl
<< "    " <<setw(2) << c <<": " << setw(c+1) << setfill('*') << endl;

return 0;
}

0

Author Commented:
If the user inputs number 100, your program will not be correct.
0

Commented:
Why? Because the screen isn't 100 chars wide?  I don't remember reading anything in the specs that said the goofy program had to handle input of 100.  The numbers given were 1, 10, 12.  How can you expect me to write a program to generate "correct" results if the "correct result" was never specified?  What the hell do you need this for anyway?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.