Hi. I am so confused on the logic of this program. here is the assignment per in
ID: 3541423 • Letter: H
Question
Hi. I am so confused on the logic of this program. here is the assignment per instructor.
Hi. I am supposed to write an assignment per the instructions below. Now, I'm okay with using the while loop and if/else statement. That's no problem. I just don't get the logic of incorporating strings into objects using the loops and if/else statements. Can anyone give me any pointers? The problem is below.
"Read" means to use console input. Be sure to use understandable prompts, and remember to use a string buffer to read the "size". Include the 1-20 range in your prompt, so that the user knows what to expect. Check the size value, and if it's outside the 1-20 expected range, print no square.
As an enhancement, use a sentinel-controlled loop, to print squares until the user enters a size outside the 1-20 range. For example, the user enters 5 for size -- print a square of size 5, and prompt for a new size. Continue until the entered size is outside 1-20, and break out of the loop to end the program -- do not return from inside the loop. Note that since string buffers and atoi are being used, any non-numeric entry for size will be interpreted as zero, and will end the program.
Explanation / Answer
#include<iostream>
int main()
{
//u said to use atoi i assumed an array and than convert it into int
char size[3];
while(1)
{
std::cout<<"Enter size of Square : ";
std::cin>>size;
//numsize will have size of square
int numSize = atoi(size);
if(numSize<1||numSize>20)
break;
//Draw squaRE
for(int i=1;i<=numSize;i++)
{
for(int j=1;j<=numSize;j++)
{
//this if condition is true jut for first and last column and row
// otherwise else will be exicuted
if(i==1||i==numSize||j==1||j==numSize)
std::cout<<"*";
else
std::cout<<" ";
}
//for new line after each row
printf(" ");
}
}
getchar();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.