Okay. This is a homework question. I have done the work to research. I'm just extremely thick headed I guess. Just can't get this thing to do what I want which is cout a picture using @ decrementing one less each row until num=0 but checking for odd and range first. Any help would be so much appreciated. The thickheadedness will pay off cause I ain't gonna give up. :)
int main()
{
int num=0;
cout << "Enter an odd number between 5 to 21" << endl;
cin >> num;
while (num % 2 ==1 && num >4 && num < 22)
{
for (int n=1; n<=num; n--)
{
cout << "@";
}
}
system ("pause");
return 0;
}
#include <iostream>
#include <windows.h>
usingnamespace std;
int main()
{
int num=0;
cout << "Enter an odd number between 5 to 21" << endl;
cin >> num;
while ((num % 2 ==1) && (num > 4) && (num < 22))
{
for (int n=1; n<=num; num--)
{
cout << "@";
}
}
system ("pause");
return 0;
}
I am not sure... but your for loop was off... so you set n = 1, and you said while n <= num then continue, and everytime through, reduce n by 1 hence the value of n for the first time = 1 and the second = 0 and the thrid = -1, so I changed it to num--, so that n = 1 every time and num is reduced... if I messed up what you wanted.. let me know : )
If 5 is entered, you want to get
@@@@@
@@@@
@@@
@@
@
, right? That is, should the oddness and range checks be done only on the input?
I'll assume so.
The most obvious problem in your code is that num never changes. That could be easily fixed by adding num--; after the for loop. However, then another error arises. If num = 5, ass conditions of the while loop are met and you get one line printed but after num--, num = 4 thus test num % 2 == 1 fails. The problem here is that you used wrong conditions for while loop. That condition is only meant for the number user entered. If num changes, you don't need to check that new value. You should instead make this an if statement instead of while, so that it is executed only once. Note than then you need one more loop. It could be either while or for. These two are interchangeable. The condition should be num > 0.
So very kind of you. Massive sigh of relief. I think I understand now. I have been chasing my tail for days now. Thank you for your lesson in logic. Exactly what I needed. Bless you.