In: Computer Science
Using C++ : What am I doing wrong!?!?
Problem Instructions: Implement a program that repeatedly outputs an I of a size entered by the user. Prompt the user for a size. If -1 is entered, quit the program. If an even number or a number smaller than 3 is entered, prompt the user again. Then output a shape of # characters that represent an I. Repeat the program until -1 is entered.
What I have so far:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int num;
cout << endl;
cout << " Please enter an ODD number greater than 2: " << endl;
cin >> num;
if (num!= -1)
{
while (num >= 3 && num % 2 == 1)
{
do //Runs if num is invalid
{
if (num %2 == 0)
{
//header
for (int i = 1; i <= num; i++)
{
cout << "#";
}
//middle
for (int i = 1; i <= num; i++)
{
cout << endl;
for (int i = 1; i <= num / 2; i++)
{
cout << " ";
}
cout << "#";
}
cout << endl;
//footer
for (int i = 1; i <= num; i++)
{
cout << "#";
}
}
} while (num >= 3 && num % 2 == 1);
}
}
return 0;
}
If you need any corrections/clarifications kindly comment.
Please give a Thumps Up if you like the answer.
Program
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int num;
cout <<"Please enter an ODD number greater than 2: ";
cin >> num;
cout << endl;
while(num!= -1)
{
if ((num %2!= 0)&&(num>2))
{
//header
for (int i = 1; i <= num; i++)
{
cout << "#";
}
//middle
for (int i = 1; i <= num; i++)
{
cout << endl;
for (int i = 1; i <= num / 2; i++)
{
cout << " ";
}
cout << "#";
}
cout << endl;
//footer
for (int i = 1; i <= num; i++)
{
cout << "#";
}
}
cout <<"\nPlease enter an ODD number greater than 2: "
;
cin >> num;
cout << endl;
}
cout <<"Quitting!!!"<<endl;
return 0;
}
Output