Solved

An easy question

Posted on 1998-10-08
7
224 Views
Last Modified: 2010-04-02
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
Comment
Question by:CG
  • 3
  • 3
7 Comments
 
LVL 1

Expert Comment

by:Grailman
ID: 1174703
cout << a << ": " << setw(a+1) << setfill('*') << endl
       << b << ": " << setw(b+1) << setfill('*') << endl
       << c <<": " << setw(c+1) << setfill('*') << endl;
       
     
0
 

Author Comment

by:CG
ID: 1174704
Sorry, your answer is not correct. Here is your output:
1: *
10: **********
12: ************
I need the output :
       1: *
     10: **********
     12: ************
0
 

Author Comment

by:CG
ID: 1174705
I'm glad that I have solved my problem.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Accepted Solution

by:
dinom earned 10 total points
ID: 1174706
#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
 

Expert Comment

by:dinom
ID: 1174707
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 Comment

by:CG
ID: 1174708
If the user inputs number 100, your program will not be correct.
0
 

Expert Comment

by:dinom
ID: 1174709
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now