for loop

Oct 25, 2009 at 1:16am
I need to write a program that asks for an integer and a character and prints out that many characters with only 10 per column. I can't get mine to work.
int main ()

{

int number;
int line;
char character;

cout << "please enter a positive integer" << endl;
cin >> number;
cout << "please enter a character" << endl;
cin >> character;
if (number > 0)
for ( int line=0; line< number; line++)

{ cout << character << endl;

for (int number=0; number < 9; number++)
{
cout << character;

}
Oct 25, 2009 at 1:32am

Firstly, your for loops' braces don't close.

Secondly, your int number=0 removes the inputted value of number.

Is the point of the program to print the character the number's times?
For example, 15 and c would yield:
cccccccccc
ccccc

If so you would need do subtract 10 from number on each loop of the outer loop.

Hope this helps.
Oct 25, 2009 at 2:08am
So I`ve fixed most of the problems I can see but now the only thing is, is that it stops at printing only 10 characters. Thank you for your help

int main ()

{

int number;
int line;
char character;

cout << "please enter a positive integer" << endl;
cin >> number;
cout << "please enter a character" << endl;
cin >> character;
if (number > 0)
for ( int line=0; line > 10; line++)

{ cout << character;
number = number - 10;
}
for (int number; number < 10; number++)
{
cout << character;
}






return 0;

}
Oct 25, 2009 at 2:53am
It's because
for (int number; number < 10; number++)
If you examine it, it only prints 10 times.
I suggest a for() loop -
for(int i = 0; i < number; i++)
Print character. If i is divisible by 9 without remainder (hint: modulo) because that's the 10th character, print a newline.
Oct 25, 2009 at 4:55am
I'm not sure how to do the modulo correctly. I've got it printing the right amount of characters but now it doesn't cut off at only ten per line.

int number;
int line;
char character;

cout << "please enter a positive integer" << endl;
cin >> number;
cout << "please enter a character" << endl;
cin >> character;
if (number > 0)
for ( int j=0; j > character; j++)

{ cout << character << endl;
number = number - 10;
}
for (int i; i < number; i++)
{
cout << character;
if (i%9==0);

}
Oct 25, 2009 at 5:10am
All you need is this loop -

1
2
3
4
5
6
7
8
9
if (number > 0)
{
    for (int i = 0; i < number; i++)
    {
        std::cout << character;
        if (i % 9 == 0) // if its the tenth character
            std::cout << "\n"; // newline
    }
}


Also I don't know if it's just me... but I see over 5 threads on this same topic in this forum...
Oct 25, 2009 at 5:36am
Thank you! I'm almost there I just can't get rid of the single character printing first.
It comes out:

integer = 12
character =r

r
rrrrrrrrrr
r
Oct 25, 2009 at 5:39am
That is because 0 mod anything is 0, try starting your loop at one and running to <= num, and change your modulus accordingly.
Oct 25, 2009 at 5:57am
Thank you so much, I've finally got it! I am VERY new to this so thank you for your patience.
Oct 25, 2009 at 9:20pm
I was wondering spazamatic2 how did you get your program to not print out a single character on the first line
Oct 25, 2009 at 10:06pm
I had to change my int i=1 instead of 0 or else the modulo is always 0.
Oct 25, 2009 at 10:09pm
I did that but it still prints 1 character on the first line

this is what i got

for (int i = 1; i <= posNumber; i++)
{
std::cout << letter;
if (i % 9 == 0)
std::cout << "\n";
Oct 25, 2009 at 10:14pm
just change it to i%10 instead of 9
Oct 25, 2009 at 10:54pm
Thanks a lot it works now!
Topic archived. No new replies allowed.