Thread: Help display ***** for the value of an int

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    26

    Help display ***** for the value of an int

    Lets say int health=6;

    I would like to display ***** symbles for the value of health

    like int health=6;
    display ******

    if health were to equal 4
    display ****


    I would like to do this without using IF statments becouse that would take up a lot of space and take forever if I had like health=100;

    what would the best way of doing this be, thanks!

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    You might want to research basic looping structures.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    yeah, that was the first thing I did, I went to
    http://www.cprogramming.com/tutorial/lesson3.html

    but I still dont know how to print a number of asterisks for an int value

    all those seem to do is keep repeating somthing, but I need to print astricks like this ******

    and not like
    *
    *
    *
    *
    *

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > and not like
    So don't print newlines then
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    f-f-f-f-f-f-f-f-for loop

    Code:
    for (int i = 0; i < hp; i++){
    cout << "*";
    }
    M-m-m-m-m-m-magic.

  6. #6
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    You need a for loop.
    An example is:
    Code:
    for(int i=0; i<PlayerHealth; i++){
    cout<<"*"}
    Edit: Krak posted before me!
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    Thanks guys

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Either Jlou or Grib posted this solution a while ago.
    Code:
       cout << string(health,'*') << endl;

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since we're providing fun solutions:
    Code:
    char buf[BUFSIZ] = {0};
    memset( buf, '*', health );
    
    cout << buf ;
    [edit]
    You could even combine the memset line with the output line...
    Code:
    cout << static_cast<char*>(memset( buf, '*', health ));
    That looks about right.
    [/edit]

    Quzah.
    Last edited by quzah; 02-03-2005 at 03:59 PM.
    Hope is the first step on the road to disappointment.

  10. #10
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Oooh! Oooh! Pick me!
    Code:
    char DispHealth[hp];
    for(int i=0; i<hp; i++){DispHealth[i]='*'}
    
    int toggle=0;
    for(int i=0; i<hp; i++){
    if(toggle==0){
    cout<<DispHealth[i];
    toggle=1;}
    else if(toggle==1){
    cout<<"*";
    toggle=0;
    }
    else{
    cout<<"\n\nSomebody has problems!\n*Points at Bill Gates* Fix it right now!"}
    }
    Okay, maybe I went overboard

    This has different methods of displaying the asterisk, one just displaying it and the other reading from a char. Of course, you should know that from looking at my code, but some people...

    Anyways, these alternate with the methods they use. I could do murmer murmer murmer...

    Hope that confuses you ,
    fuh
    Last edited by fuh; 02-04-2005 at 07:00 AM.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Okay, maybe I went overboard
    You also got a little bug:
    >else if(toggle=1){

  12. #12
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Oops! Problem solved. I just pulled the code up right on the spot.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, no, you got it all wrong. You need to do:
    Code:
    std::fill_n(std::ostream_iterator<char>(std::cout), health, '*');
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM