i need to use a program that shows on screen numbers from 1 to x but i cant display multiples of 3 and i need to repeat it a 100 times and display a 10 by 10 grid so I used this program but for some reason it wont repeat 100 times it stops when the value displayed on screen is 100 could someone hel p me figure it out.
here is the code
#include <iostream>
using namespace std;
int main()
{
// n is the number displayed on screen and cL is a counter for how many numbers i´ve written on that line .
int n=0, cL=1;
// x is the number of repetitions the proogram should do.
for (int x=0; x<100; x++)
{
// it adds 1 to n and if it´s not a multiple of 3 displays it.
n+= 1;
if (n%3 != 0)
{
// if the counter of line is equal to 10 skips a line and displays the next number and turns cL into 0
if (cL==10)
{ cout << " " <<endl;
cout << n;
cL = 0;
}
else
// it just displays the number.
cout << n << ", ";
cL = cL + 1;
}
}
}
this program it´s supposed to be repeated a hundred times but instead it only repeats if n < than 100 plis post a solution or an observation or something :S
it seems that it does run 100 times if you count x starts at 0 and ends at 99. i ran program you listed and printed out the value of x and the for loop runs 100 times.
I have another question for you is the program supposed to run the loop 100 times or do 100 calculations.
from the output i get it prints out all numbers 0-100 that are not divisible by 3 from what i see the only thing i see is that the output needs to be cleaned up after a newline is printed out. but other than that it seems to work fine.
the only other thing i have a question about is n+= 1; seems to be wrong should you just use n++;
Uhh... what's wrong with n += 1;? It's perfectly acceptable.
I know but just thought that n++ was just better but the other is not wrong.
Disch i agree a while loop is probably a better solution but a for loop also works. and as i stated the program seems to be working properly. if you copy the code and compile and run it seems to run the way it should. it just doesn't look like it as after a new line is reached the first couple of numbers are jumbled together then the output clears up. all i am asking is if the program is for the numbers 0-100 or he needs 100 numbers on the grid because if he needs a 10 x 10 grid the out put is wrong. the for loop needs to be adjusted if he definitely needs the for loop. The while loop is a better solution like you stated you only increment x when a number is printed with the while loop.
jesuck if you must use a for loop you need to up the high value of x to get 100 numbers in the grid. the problem is not that n=100 it is that x reaches 100 before you get 100 numbers.
The while idea was the one...
i found out why it was wrong, it accomplished the loop 100 times but ( important point) it counted even thought the value wasn't printed so
the program made 100 loops but it only printed like 80 values. A small error pff
thanks jaydr and Disch for the replies