Solved

An easy question

Posted on 1998-10-08
7
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ question 3 82
Need some help with Microsoft Visual Studio C++ 2003 5 87
White board coding practice 3 98
HTTPSendRequest with WinINet delays on first call 11 119
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

739 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