# C++ cout "*"



## daybo (Aug 7, 2001)

i need a very simple C++ example printing a specified number of astericks from a variable(S) containing a user specied integer. integer to be 1-30. preferably using a FOR structure. not asking anyone to do my work for me, just an example to go by. what i got so far =(

cout << "Enter 1 value 1-30 #\n"; //prompt
cin >> num1; //read integer
cout << "*";

thanks


----------



## AlbertB (Nov 24, 2002)

Try this daybo, it runs here. 

As you said the number must be between 1 and 30 I have added a DO-WHILE loop to check for this and repeat the request if it is out of range. The printing is done as you wanted with a FOR loop.

The html on the page will not always accept the angle brackets again so check out the correctness of these in lines where they are used. It also does not like spaces at the beginning of lines and removes them, so I am afraid it may look untidy once posted. But then I suppose correcting this is a good way of checking your way through it.

#include [iostream] // replace [] with angle brackets
using namespace std ;

void main ()
{
int num = 0 ;
int loop = 0 ;

do // do-while performs the loop at least once
{
cout << "Enter an integer value 1-30: " ; //prompt
cin >> num; //read integer

if (num>30 || num<1) // range check on num
{
cout << endl << "Number must be between 1 and 30 inc, try again." << endl << endl ;
 }

} while (num>30 || num<1) ; // exit loop if num is valid, repeat if invalid

cout << endl ;

for (loop=1; loop<=num; loop++) // print "*" num times
cout << "*" ;

cout << endl << endl ;
}


----------



## daybo (Aug 7, 2001)

Albert, i really appreciate that code. alot more sophisticated than i could ever come up with, but nevertheless genius. thanks again.


----------



## daybo (Aug 7, 2001)

thanks again but what are thedouble vertical bars in this code. what are they called. is there any way to get around using them?thanks. Beggers cant be choosers=) 

if (num>30 || num<1)


----------



## AlbertB (Nov 24, 2002)

The double bars, ||, are the sign for logical "OR". The section you questioned reads "If num is greater than 30 OR less than 1". You will usually find them as [ shift ] + [ \ ].

Hope that helps.


----------



## daybo (Aug 7, 2001)

thanks alot for the speedy response


albert is the man @ tsg


----------



## daybo (Aug 7, 2001)

having trouble printing * for each variable num on seperate lines. sorry to keep bothering you
thanks

for (loop=1; loop<=num; loop++) // print "*" num times
cout << "*" ;
for (loop=1; loop<=num1; loop++) // print "*" num times
cout << "*" ;
for (loop=1; loop<=num2; loop++) // print "*" num times
cout << "*" ;
for (loop=1; loop<=num3; loop++) // print "*" num times
cout << "*" ;
for (loop=1; loop<=num4; loop++) // print "*" num times
cout << "*" ;


----------



## AlbertB (Nov 24, 2002)

I think what you mean is that you want 5 separate lines each containing a number of "*"s according to the value of num, num1, num2, num3, and num4. I hope I have that right.

All you need to do is to add after each cout << "*" ;

line, the following line:

cout << endl ;

This will force a newline after each line of "*"s.

If num=4, num1=8, num2=3, num3=10 and num4=7 your output should look like this:

****
********
***
**********
*******

If you wanted an extra line between each then just use:

cout << endl << endl ;

like the last line.


for (loop=1; loop<=num; loop++) // print "*" num times
cout << "*" ;
cout << endl ;
for (loop=1; loop<=num1; loop++) // print "*" num times
cout << "*" ;
cout << endl ;
for (loop=1; loop<=num2; loop++) // print "*" num times
cout << "*" ;
cout << endl ;
for (loop=1; loop<=num3; loop++) // print "*" num times
cout << "*" ;
cout << endl ;
for (loop=1; loop<=num4; loop++) // print "*" num times
cout << "*" ;
cout << endl << endl ;


----------



## daybo (Aug 7, 2001)

thankx again. i was putting the endl directly after the cout "*" << endl;

anyway i really appreciate it.


----------



## AlbertB (Nov 24, 2002)

Glad to help daybo. Stick at it!


----------



## daybo (Aug 7, 2001)

here are the two erros im getting and fix them. also sometimes i get unexpected end of file found. help.
thanks again and again

1. error C2059: syntax error : 'return'
2. warning C4508: 'main' : function should return a value; 'void' return type assumed

#include <iostream>

using std::cout ;
using std::cin ;
using std::endl ;

int main ()
{

int num = 0 ;

do {
cout << "Enter an integer value 1-30: " ; //prompt
cin >> num; //read integer

cout << "Enter an integer value 1-30: " ; //prompt
cin >> num; //read integer

cout << "Enter an integer value 1-30: " ; //prompt
cin >> num; //read integer

cout << "Enter an integer value 1-30: " ; //prompt
cin >> num; //read integer

cout << "Enter an integer value 1-30: " ; //prompt
cin >> num; //read integer

while ( ( num = cin.get() ) != EOF ) {

switch( num ) {

case 'num':
cout << " * ";
break;

case 'num1':
cout << " * ";
break;

case 'num2':
cout << " * ";
break;

case 'num3':
cout << " * ";
break;

case 'num4':
cout << " * ";
break;

default: 
cout << "Incorrect interger, try again." << endl;
break;
}
}
}

return 0; 
}


----------



## AlbertB (Nov 24, 2002)

There is a lot which seems to be amiss here daybo. Does the "do" and the "while" apply to the same code loop? If so your {} brackets are misplaced. If not your "do" is missing its "while"  .

You also are missing some ";" after your } towards the end of the code. I would start with a complete line by line check of the syntax of what you have written for simple faults like these.


----------



## lamstaldy (Dec 16, 2001)

I think your errors are occurring because the main function should be void and not int.

void main() instead of int main()

Also how do you want your program to function? Do you want to input five separate numbers and then print five rows of "*"? Or do you want to input one number and output a row of "*" and then input a second number and output a row of stars etc. I don't think you want to use the EOF when you are getting data from a user input, that is mainly for sensing the End-Of-File in a text file.


----------



## daybo (Aug 7, 2001)

thanks alot for all of your help


----------



## JohnWill (Oct 19, 2002)

> _Originally posted by lamstaldy:_
> *I think your errors are occurring because the main function should be void and not int.
> 
> void main() instead of int main()
> *


Of course, you could always return a value, that's how you have an exit code for a standard application.


----------

