Thread: simple For loop please help

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Exclamation simple For loop please help

    I need help creating a For loop that prints a string. it prints the char a number of times. e.g.( & and u want to print it 3 times '&&&'). I dont necessarily know how to do it as you can see im lost on the loop

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
        char ch;
        int n;
        cout << "This program takes the characters given and display them a given amout of times.\n";
        cout << "Please enter in a character you would like to use: \n";
        cin >> ch;
        cout << "How many times would you like to show this character: ";
        cin >> n;
    
        int n = 0;
         for (char ch = 0; ch < 0; ch++)
            cout << ch << ' ';
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    for (char ch = 0; ch < 0; ch++)
    A for loop is (generally)

    Code:
    for (initial counter variable setting; loop until this condition becomes false; increment counter variable)
    As you've written it, the code will never loop as ch equals 0, which is not less than 0. And, you don't want to use ch as your variable anyway. The loop-controlling variable in this case would be n.

    Just think about it a bit. It's not hard.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    i'm kind of new to c++ so you would have to bare with me and thank you for helping.
    Code:
    for (int n = 0; n <= 5; n++)
    ok since i am trying to print the char a certain amount of times i would have to use the for loop for the integer right? so if i was to do it for 5 i would go till it is or equal to 5?

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    3
    ok i think i fixed it but how do i make it to where the user would enter in the amount of times and thats how many times it displays it?

  5. #5
    Registered User inequity's Avatar
    Join Date
    Nov 2010
    Location
    Seattle, Washington
    Posts
    59
    Quote Originally Posted by d_nice View Post
    i'm kind of new to c++ so you would have to bare with me and thank you for helping.
    Code:
    for (int n = 0; n <= 5; n++)
    ok since i am trying to print the char a certain amount of times i would have to use the for loop for the integer right? so if i was to do it for 5 i would go till it is or equal to 5?
    ok, so if you want n to be the number of times the character is printed, try to structure your loop like this
    Code:
    for(int i = 0; i < n; i++)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple loop
    By Aash in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2010, 01:41 AM
  2. Simple loop
    By Aash in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2010, 11:15 PM
  3. Simple Loop
    By blk007 in forum C Programming
    Replies: 6
    Last Post: 05-28-2010, 08:06 AM
  4. Simple loop
    By chaos787 in forum C Programming
    Replies: 3
    Last Post: 02-25-2009, 09:58 PM
  5. simple while loop
    By free2rhyme2k in forum C++ Programming
    Replies: 38
    Last Post: 02-01-2008, 08:15 PM