How do I output * next to numbers
Feb 1, 2015 at 2:59am UTC
Hello guys I am new to programming and am wondering how I can output a '*' next to a number. also the stars should equal the number so for example:
1 *
2 **
3 ***
and etc. Please help I really just want to know, this is my code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <iostream>
using namespace std;
int main() {
char a,b,c,d;
cout << "Enter Four Digit positive number: " <<endl;
cout<< "Input = " ;
cin>>a;
cout << a <<endl;
cin>>b;
cout << b<<endl;
cin>>c;
cout << c <<endl;
cin>>d;
cout << d <<endl;
return 0;
}
Feb 1, 2015 at 4:19am UTC
Please help guys..
Feb 1, 2015 at 4:30am UTC
1 2 3
std::cout << a << ' ' ;
for (int i = 0; i < a; ++i) std::cout << '*' ;
std::cout << std::endl;
or
1 2
std::string stars(a, '*' );
std::cout << a << ' ' << stars << std::endl;
Feb 1, 2015 at 6:07am UTC
I'm a little confused..
Feb 1, 2015 at 3:48pm UTC
On what part?
Feb 2, 2015 at 1:55am UTC
like where to add that piece of code. I was taught without using ' std:: ' so it looked a little odd to me.
Feb 2, 2015 at 2:11am UTC
std:: is to explicitly say it is part of the std namespace. As far as where to put it, I just showed you examples of how you would do it after getting input for your a variable.
I would personally do something like this:
1 2 3 4 5 6 7 8 9 10
int const digits = 4;
cout << "Please enter " << digits << " digits" << endl;
int digit = 0;
for (int i = 0; i < digits; ++i)
{
cin >> digit;
string temp(digit, '*' );
cout << digit << ' ' << temp << '\n' ;
}
Feb 2, 2015 at 6:57am UTC
I made a program for you that does what I think you need. Let me know please.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <iostream>
#include <String>
using namespace std;
string choice;
string CHOOSE1();
int a = 1, b = 2, c = 3, d = 4;
int main()
{
cout << "Enter a letter a, b, c, or d: \n" ;
cout << CHOOSE1();
}
string CHOOSE1()
{
for (int i = 1; i < 1000; i++)
{
cin >> choice;
if (choice == "a" )
{
cout << "Input = " ;
cout << " 1 *\n" ;
}
else if (choice == "b" )
{
cout << "Input = " ;
cout << " 2 **\n" ;
}
else if (choice == "c" )
{
cout << "Input = " ;
cout << " 3 ***\n" ;
}
else if (choice == "d" )
{
cout << "Input = " ;
cout << " 4 ****\n" ;
}
}
return 0;
}
Topic archived. No new replies allowed.