Thread: How to loop a single character?

  1. #1
    Registered User spcx's Avatar
    Join Date
    Aug 2011
    Posts
    2

    How to loop a single character?

    Hello,

    I have a little over 3 days of C experience so bear with me!

    I understand how to print 1-100:
    Code:
    for (i = 1; i < 100; i++)
    printf("%d\n", i);
    And also A-Z:
    Code:
    for (ch = 'A'; ch <= 'Z'; ch++)
    printf("%c\n", ch);
    My question is: How can I print a single character for x times?
    For example the letter A, 100 times?:

    A
    A
    A
    A
    A
    ...

    I tried:
    Code:
    for (ch = 'A'; ch = 'A'; ch++)
    printf("%c\n", ch);
    But it gives an infinite loop! How can I tell it to only print 100 times or any other ammount?

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Umm.....
    Code:
    for(int i=0;i<100;i++)
         printf("A\n");
    Keep reading through your book or whatever tutorials you are using.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User spcx's Avatar
    Join Date
    Aug 2011
    Posts
    2
    Quote Originally Posted by AndrewHunter View Post
    Umm.....
    Code:
    for(int i=0;i<100;i++)
         printf("A\n");
    Keep reading through your book or whatever tutorials you are using.
    Thank you kindly AndrewHunter. I was having a hard time grasping the logic of it but examples like these makes it very easy to follow and understand. Thanks again!

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    36
    You don't need to increment ch, you just need to display the char 'A' 100 times.

    edit: I'll include some code
    Code:
    int i;
    char ch = 'A';
    
    for (i = 0; i <= 100; i++) {
         printf("%c", ch);
    }
    the variable i, in this code, is to determine how many times the loop will run.
    Last edited by Darkroman; 08-23-2011 at 10:42 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Darkroman: note that because you used i <= 100, your loop runs 101 times.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to take a SINGLE UNKOWN character from a string
    By stmty9 in forum C Programming
    Replies: 8
    Last Post: 12-01-2010, 10:49 PM
  2. Need help with Single Character
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 10-05-2010, 01:29 AM
  3. Single Character Input.
    By mintsmike in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2009, 08:16 AM
  4. get single character from string
    By deltaxfx in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 01:21 PM
  5. How to strcopy a single character from a string
    By Stuu in forum C++ Programming
    Replies: 8
    Last Post: 04-18-2002, 01:45 AM