-3

I need to ask the user how high and wide a comment box to draw and then when I run the compiler it will draw such a box of the appropriate size. Here is my code that I am using:

int main() {
    int i;

    writePattern('/', '*', '/', ' ', ' ',1, LENGTH-2,1,0,0, LENGTH); //top of box being created

    for(i=1; i<=5; i++)
        writePattern('/', '*', ' ', '*', '/', 1,1, LENGTH-4, 1,1, LENGTH); //sides of box

    writePattern('/', '*', '/', ' ', ' ', 1 , LENGTH-2,1,0,0,LENGTH); //bottom of box

    return 0;
}

So how would I do this? I am new to C so I need some help. I know I will have to use the printf and scanf functions do read in user input but I am not sure how to do this.

Tucker Sampson
  • 41
  • 1
  • 2
  • 7
  • The `writePattern(...)` function is not implemented yet? Do you want it to be implemented? – Sergey Vyacheslavovich Brunov Jan 25 '13 at 18:50
  • I didn't show all of the code...but it is implemented. This is just the part of the code that I need to use to ask the user how high and wide they want the comment box to be and then when I run the compiler it will draw the box of the specified size. – Tucker Sampson Jan 25 '13 at 18:53
  • 1
    Don't edit the title and question to complete nothingness, either accept an answer or leave it open so other people can search and find help. – Christopher Marshall Jan 25 '13 at 21:59

3 Answers3

0

Do you just want help on the printing and scanning?

int x, y;
printf("How wide do you want your box?");
if (!scanf("%d", &x))
    //printf that they did it incorrectly and try again, probably in a while loop
printf("how wide?");
if (!scanf("%d", &y))
    //printf that they did it incorrectly and try again, probably in a while loop

where %d would take or print a number, %s a string, %f a fraction, etc you can look any others up.

EDIT: woops i forgot the & to reference the addresses of the variables

EDIT2: added suggested error handling

DanielCardin
  • 545
  • 2
  • 8
  • 17
0

Please try this code:

printf("Enter the width: ");
int width;
if (scanf("%d", &width) != 1) {
    printf("Invalid number!");
    return 1; // Just return nothing or some error code?
}

printf("Enter the height: ");
int height;
if (scanf("%d", &height) != 1) {
    printf("Invalid number!");
    return 1; // Just return nothing or some error code?
}
0

Before going any further ... It is a very wise thought to learn such a beautiful language ! But when you get started, make sure you do the right thing right from the beginning.

I would advise you not to use the scanf() function. It will lead to your program failure if the user does not type in the requested thing. You would rather call general stream functions such as fgets() and then parse the string you get. Take a look at this other so question : Difference between scanf() and fgets()
You might get useful tips over there.

Community
  • 1
  • 1
Rerito
  • 5,886
  • 21
  • 47