homework help for printing patterns

Mar 17, 2017 at 10:14pm
I may had written it wrong. but the X,+,*,& that I used in the code is what my professor want us to use to make the 2 big asterisks.
Task:
I. The program asks user to enter a positive odd integer 9 <= num <= 15. If
the input value is incorrect, the program repeatedly force user to input values
until num is within the expected value.
II. The program then prints out two big “asterisks” with four spaces in
between like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 #include <iostream>

using namespace std;

int main(){

int num;

while(true){

cout << "Enter an odd number between 9 and 15 (inclusive) :";

cin >> num;

if(num%2==1 && num >= 9 && num <= 15)

break;

}

if (c<(num+1)/2&& r==(num+1)/2) {
cout <<"x";
}

else if(c>(num+1)/2&&r==(num+1)/2){
cout<<"+";
}

if (r>(num+1)/2&&c==(n+1)/2){
cout<<"&";
}                     
else if (r<(num+1)/2&&c==(n+1)/2){
cout<<"&";
}
 else cout<<" ";
cout << endl;

return 0;
}
Last edited on Mar 17, 2017 at 11:05pm
Mar 18, 2017 at 3:37am
I'm not sure what you mean but while not necessarily bad programming practice it's better to use a bool to break out of the while instead of the break keyword because it's not always reliable and its easier to identify problems with a bool. I've shown you below.

If you elaborate on your question further i can attempt to help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{

int num;
bool wrongNum = true;

while(wrongNum = true)
{
    cout << "Enter an odd number between 9 and 15 (inclusive) :";
    cin >> num;

    if(num%2==1 && num >= 9 && num <= 15)
    {
        wrongNum = false; 
    }
}

//Continue code
}
Last edited on Mar 18, 2017 at 3:38am
Mar 18, 2017 at 3:56am
@kingkush

Shouldn't line 7 in your code be..
while(wrongNum == true)?
You're comparing, NOT assigning.
Mar 18, 2017 at 7:07am
Yes cant believe i made such a trivial mistake haha sometimes don't use a compiler to remind of these things lol.
Mar 18, 2017 at 8:03am
Rather:

while(wrongNum) { // it's a bool no need to compare

A false version:

while (!Num) {
Last edited on Mar 18, 2017 at 9:30am
Mar 18, 2017 at 10:54am
Hello bebe1194,

The problem you may or may not have with the while loop are moot because everything after the while loot does not work.

The program does not compile as you have it. And the variables "c", "r" and "n" are never defined or given a value to use. Also all the "int" variables should be initialized when they are defined, its just good form.

After the while loop "c", "r" and "n" are never given a value to use making it hard for the "if" statements to work.

I am not sure exactly what you are expecting from the program, but it will only print one character before it ends and that is not what you want.

Hope that helps,

Andy
Mar 18, 2017 at 7:53pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

using namespace std;

int main(){

int num;

while(true){

cout << "Enter an odd number between 9 and 15 (inclusive) :";

cin >> num;

if(num%2==1 && num >= 9 && num <= 15) 
{
cout << "Enter again: ";
cin >> num;
}

// remove this, this is so bad. break;

}
//obviously this if statement might catch the break but this is not a complete program
if (c<(num+1)/2&& r==(num+1)/2) {
cout <<"x";
}

else if(c>(num+1)/2&&r==(num+1)/2){
cout<<"+";
}

if (r>(num+1)/2&&c==(n+1)/2){
cout<<"&";
} 
else if (r<(num+1)/2&&c==(n+1)/2){
cout<<"&";
}
else cout<<" ";
cout << endl;

return 0;
}
Last edited on Mar 18, 2017 at 7:55pm
Topic archived. No new replies allowed.