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.
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
}
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.
#include <iostream>
usingnamespace 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";
}
elseif(c>(num+1)/2&&r==(num+1)/2){
cout<<"+";
}
if (r>(num+1)/2&&c==(n+1)/2){
cout<<"&";
}
elseif (r<(num+1)/2&&c==(n+1)/2){
cout<<"&";
}
else cout<<" ";
cout << endl;
return 0;
}