Thread: integers and char

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    5

    integers and char

    Im looking to write a program that will read a number input from the user between 1 and 30. After I get the input I want to print that number of stars and lines in this pattern.

    Lets say the user inputs the number 3: (pattern should look like this)
    *** *** ***
    *** ***
    *** *** ***

    There should be 3 lines since the number input was 3 and 3 stars in each group.

    Im having trouble converting the number input, to the stars and lines.

    Any help?

  2. #2
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    2 for loops - nested, each looping the number of times in input.

    The inner loop prints the *'s and the outer loop prints the ' '. (space).

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    5

    thanks

    Thanks. Thats what I thought to do but wasn't sure. I'm still having trouble with the stars and spaces though. This is what I'm stuck on.....

    Lets say the user enters the number 5....
    How do output 5 *'s on the screen? (or 5 spaces)?

    Also, is possible to use if or while loops for this instead of for loops? I know it would be more work but i was just wondering how that would work.

    Thanks for the help though.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    18
    Quote Originally Posted by ksample23 View Post
    Lets say the user enters the number 5....
    How do output 5 *'s on the screen? (or 5 spaces)?
    By using loops whose loop-counter corresponds to your input. As said above, if you use a loop to print the characters you will print 5 characters if the user's input was 5, and 10 accordingly if the user input was 10. E.g.

    Code:
    int i;
    for (i = 0; i < <user_input>; i++)
    {
           print the character;
    }
    Quote Originally Posted by ksample23 View Post
    Also, is possible to use if or while loops for this instead of for loops?
    you could transform each for loop into a while loop. But, I don't really know what you mean by IF. IF isn't a loop statement ...

    - Andi -

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by ForzaItalia2006 View Post
    But, I don't really know what you mean by IF. IF isn't a loop statement ...
    Code:
    start_of_loop:
        // Do stuff
        if(condition) goto start_of_loop;
    (And before anyone jumps at my throat, it was a joke)

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    18
    Quote Originally Posted by _Mike View Post
    (And before anyone jumps at my throat, it was a joke)
    I shortly hesitated to list that example, but I completely feared the reactions

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    And if you really want to risk people yelling at you for bad coding practices you could do a loop with SetThreadContext()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. strcat - cannot convert char to const char
    By ulillillia in forum C Programming
    Replies: 14
    Last Post: 12-07-2006, 10:00 AM
  3. Replies: 6
    Last Post: 06-30-2005, 08:03 AM
  4. Reading in integers into char Array
    By fortune10 in forum C Programming
    Replies: 1
    Last Post: 04-18-2002, 08:32 AM
  5. integers to char *
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-19-2001, 09:14 AM