need help debugging C code.

imported_vr6

Platinum Member
Jul 6, 2001
2,740
0
0
Ok heres what i get when i try to compile my program.

irix[1]% gcc -ansi -Wall test.c -lm
test.c: In function 'main':
test.c:56: parse error before 'double'
test.c:73: parse error before 'void'
test.c: in function 'DrawRect':
test.c:132: parse error at the end of imput.

Heres the code, The faces indicat line 56,73, and 132 respectively.



#include <stdio.h>
#include <math.h>

int Area( int width, int height );
int Perimeter( int width, int height );
double diagonal( int width, int height );
void DrawRect( int width, int height, char diplay );
void PrintMenu( void );

int main()
{

float diagonal;
int width, height, perimeter, area;
char display, response;

printf( "Enter a width: " );
scanf( "%d",&width );

while ( width < 1 )
{
printf( "Please enter a positive integer." );
scanf( "%d",&width );
}

printf( "Enter a height: " );
scanf( "%d",& height );

while ( height < 1 )
{
printf( "Please enter a positive whole number." );
scanf( "%d",&height );
}


getchar();
printf( "Enter a character to be used to draw the rectangle: " );
display = getchar();

do
{
getchar();
PrintMenu();
response = getchar();

switch(response)
{
case 'A': area = Area( width, height);
printf("The area is %d\n",area);
break;

case 'P': perimeter = Perimeter( width, height);
printf("The perimeter is %d\n",perimeter);
break;

case 'D': diagonal = double diagonal( width, height); :confused:
printf("The diagonal is %f\n",diagonal);
break;

case 'R': DrawRect( width, height, display );
break;

case 'Q': printf(" Closing Program.\n ");
break;

default: printf(" Not a valid menu option. ");

}while( response != 'Q' );

return 0;
}

void PrintMenu(void) :confused:
{
printf( " Menu \n" );
printf( "A - Calculate area of the rectangle.\n" );
printf( "P - Calculate the perimeter of the rectangle.\n" );
printf( "D - Calculate diagonal of the rectangle.\n" );
printf( "R - Display the rectagle.\n" );
printf( "Q - Quit.\n" );
printf( "Selection: ");
}


int Perimeter( int width, int height)
{
int perimeter;

perimeter = ((2 * width) + (2 * height));
return perimeter;
}

int Area( int width, int height)
{
int area;

area = (width * height);
return area;
}

double diagonal(int width, int height)
{
float diagonal;

diagonal = sqrt((width * width) + (height * height));
return diagonal;
}

void DrawRect( int width, int height, char display )
{
int heightcounter, widthcounter;

heightcounter = 1;

while( heightcounter <= height )
{
widthcounter = 1;

while( widthcounter <= width )
{
printf(" %c%c",display);
widthcounter = widthcounter + 1;
}

printf("\n");
heightcounter = heightcounter + 1;

return;


}
:confused:


 

Kelvrick

Lifer
Feb 14, 2001
18,422
5
81
#include <stdio.h>
#include <math.h>

int Area( int width, int height );
int Perimeter( int width, int height );
double diagonal( int width, int height );
void DrawRect( int width, int height, char diplay );
void PrintMenu( void );

int main()
{

float diagonal;
int width, height, perimeter, area;
char display, response;

printf( "Enter a width: " );
scanf( "%d",&width );

while ( width < 1 )
{
printf( "Please enter a positive integer." );
scanf( "%d",&width );
}

printf( "Enter a height: " );
scanf( "%d",& height );

while ( height < 1 )
{
printf( "Please enter a positive whole number." );
scanf( "%d",&height );
}


getchar();
printf( "Enter a character to be used to draw the rectangle: " );
display = getchar();

do
{
getchar();
PrintMenu();
response = getchar();

switch(response)
{
case 'A': area = Area( width, height);
printf("The area is %d\n",area);
break;

case 'P': perimeter = Perimeter( width, height);
printf("The perimeter is %d\n",perimeter);
break;

case 'D': diagonal = diagonal( width, height);
printf("The diagonal is %f\n",diagonal);
break;

case 'R': DrawRect( width, height, display );
break;

case 'Q': printf(" Closing Program.\n ");
break;

default: printf(" Not a valid menu option. ");
}

}while( response != 'Q' );

return(0);
}

void PrintMenu(void)
{
printf( " Menu \n" );
printf( "A - Calculate area of the rectangle.\n" );
printf( "P - Calculate the perimeter of the rectangle.\n" );
printf( "D - Calculate diagonal of the rectangle.\n" );
printf( "R - Display the rectagle.\n" );
printf( "Q - Quit.\n" );
printf( "Selection: ");
}


int Perimeter( int width, int height)
{
int perimeter;

perimeter = ((2 * width) + (2 * height));
return perimeter;
}

int Area( int width, int height)
{
int area;

area = (width * height);
return area;
}

double diagonal(int width, int height)
{
float diagonal;

diagonal = sqrt((width * width) + (height * height));
return diagonal;
}

void DrawRect( int width, int height, char display )
{
int heightcounter, widthcounter;

heightcounter = 1;

while( heightcounter <= height )
{
widthcounter = 1;

while( widthcounter <= width )
{
printf(" %c%c",display);
widthcounter = widthcounter + 1;
}

printf("\n");
heightcounter = heightcounter + 1;

return;


}
}
 
Jun 18, 2000
11,164
740
126


<< you are missing a closing bracket >>


Actually 2 of them.

Edit:
2 more things:

1) You have a function named "diagonal" and have defined a float variable called "diagonal." This is a no no. You'll have to rename one of them.
2) You defined your "diagonal" function as a double, but the "diagonal" variable has been defined as float. This is also a no no. It'll compile with a couple warnings.
 

Kelvrick

Lifer
Feb 14, 2001
18,422
5
81


<<

<< you are missing a closing bracket >>


Actually 2 of them.
>>


He was missing one at the end, and one at the end of the Do-while loop/switch.
 

imported_vr6

Platinum Member
Jul 6, 2001
2,740
0
0
hymmm howcome when i call the function i don't have to include the double? I thought the function was called double diagonal, how come when i call it, i drop the double?
 
Jun 18, 2000
11,164
740
126
I think you are misunderstanding the purpose of keyword double. The keyword at the beginning of the function definition just tells the compiler what the function will return. You are defining a function called "diagonal" that will return a number of type "double."

When you actually call the function, you only need to use the name - "diagonal."

Here is your code, slightly revised (the changes I made are in bold):

#include <stdio.h>
#include <math.h>

int Area( int width, int height );
int Perimeter( int width, int height );
double diagonal( int width, int height );
void DrawRect( int width, int height, char diplay );
void PrintMenu( void );

int main()
{
       
       double localDiagonal;
       int width, height, perimeter, area;
       char display, response;

       printf( "Enter a width: " );
       scanf( "%d",&width );

       while ( width < 1 )
       {
              printf( "Please enter a positive integer." );
              scanf( "%d",&width );
       }

       printf( "Enter a height: " );
       scanf( "%d",& height );

       while ( height < 1 )
       {
              printf( "Please enter a positive whole number." );
              scanf( "%d",&height );
       }


       getchar();
       printf( "Enter a character to be used to draw the rectangle: " );
       display = getchar();

       do
       {
              getchar();
              PrintMenu();
              response = getchar();

              switch(response)
              {
                     case 'A': area = Area( width, height);
                     printf("The area is %d\n",area);
                     break;

                     case 'P': perimeter = Perimeter( width, height);
                     printf("The perimeter is %d\n",perimeter);
                     break;

                     case 'D': localDiagonal = diagonal( width, height);
                     printf("The diagonal is %f\n",diagonal);
                     break;

                     case 'R': DrawRect( width, height, display );
                     break;

                     case 'Q': printf(" Closing Program.\n ");
                     break;

                     default: printf(" Not a valid menu option. ");
              }

       }while( response != 'Q' );

       return(0);
}

void PrintMenu(void)
{
       printf( " Menu \n" );
       printf( "A - Calculate area of the rectangle.\n" );
       printf( "P - Calculate the perimeter of the rectangle.\n" );
       printf( "D - Calculate diagonal of the rectangle.\n" );
       printf( "R - Display the rectagle.\n" );
       printf( "Q - Quit.\n" );
       printf( "Selection: ");
}


int Perimeter( int width, int height)
{
       int perimeter;

       perimeter = ((2 * width) + (2 * height));
       return perimeter;
}

int Area( int width, int height)
{
       int area;

       area = (width * height);
       return area;
}

double diagonal(int width, int height)
{
       double diagonal;

       diagonal = sqrt((width * width) + (height * height));
       return diagonal;
}

void DrawRect( int width, int height, char display )
{
       int heightcounter, widthcounter;

       heightcounter = 1;

       while( heightcounter <= height )
       {
              widthcounter = 1;

              while( widthcounter <= width )
              {
                     printf(" %c%c",display);
                     widthcounter = widthcounter + 1;
              }
       }

       printf("\n");
       heightcounter = heightcounter + 1;

       return;
}
 

Kelvrick

Lifer
Feb 14, 2001
18,422
5
81


<< hymmm howcome when i call the function i don't have to include the double? I thought the function was called double diagonal, how come when i call it, i drop the double? >>


Ehh... Double is the type of function it is. There are int, double, and however many other kinds of functions. You jsut call the name. Like when you called the function Area, you didn't call "int Area", you called "area."
 

Kelvrick

Lifer
Feb 14, 2001
18,422
5
81
KnightBreed's code should be right. I realize mine is wrong after I looked at his. I was just looking for some plain mistakes without looking at the code. What you get for asking help on ATOT. You should know people here are watching Star Wars, the Laker game, and trying to debug code. :)
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0


<<

<< you are missing a closing bracket >>


Actually 2 of them.
>>



whoops :eek:

i just clicked on "format code" and looked at the end to see how far off it was :eek:
 
Jun 18, 2000
11,164
740
126


<< i just clicked on "format code" and looked at the end to see how far off it was :eek: >>


lol, format code? I just did a find/replace of all '{', and then for all '}'. VC++ found 12 opening braces and only 10 closing braces. Very scientific. :) :D

Your welcome for the help, Kwan. I'm hittin' the hay. G'nite all.
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
lol, format code? I just did a find/replace of all '{', and then for all '}'. VC++ found 12 opening braces and only 10 closing braces. Very scientific. :) :D

damn, that's good! i'm jealous i didn't think of that earlier :|
 

imported_vr6

Platinum Member
Jul 6, 2001
2,740
0
0
ok i used knightbreeds code and it compiled.

But when i tried to get the value for the diagonal, it just kept giving me 0.0000

and whe i tried to draw the rectangle, it just keeps printing the symbol over and over again until i terminate it.. any ideas?

I am going to bed, i be back tomorrow though. THanks for the help everyone.
 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
Couldn't resist but help :)

Code is here

I've removed a few redundant variables, and renamed a few using Hungarian Notation (always good to learn btw) helps keep the variables and function names separate. :) The diagonal and display functions now work too.

 

Chooco

Banned
Apr 5, 2002
731
0
0
if yer in Windows you should dl a compiler called DevC++

not to compile (you probably have something better) but to check for errors in the program, it tells you what line the error is on and what the error is such as "parse error before 'int'"
 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
With the likes of Visual C++ the errors it throws back sometimes are a bit of a oxymoron. To understand some of them you have to be really good at C, but if you are good enough you shouldn't be making those errors. ;) The amount of errors you can get back for missing a brace or a semi colon is funny at times. 1 thing causing 100+ errors ;)

 

manly

Lifer
Jan 25, 2000
12,268
3,173
136
Hungarian Notation has questionable value for higher-level languages, i.e. C++

It's popular largely because it was popularized at MS. Many experts disagree with its general value. Personally, I can see some uses for it (i.e. if you have to program with the Win32 API), but basic classroom code and homework isn't one of them.

Also, to clarify (what Kelvrick said), double is not the type of the function. It's simply the type of the return value.

With a language with method overloading, the return type generally does not parameterize/distinguish the method. The type of the method is distinguished by its formal parameters, aka its signature.

If a professor isn't doing a good enough job, you should find a book that does a better job. If you want the best C language book in existence, get Kernighan and Ritchie's The C Programming Language. It's a great, clear book but it might be a little harsh for newbies.
 

Chooco

Banned
Apr 5, 2002
731
0
0
yeah, this dude named Budfroggy recommended that book to me also

you could also try the booked called "C#", i don't know the author but the publisher is O'Reilly which means it's one of those books with all white background and just a picture of some kind of animal on it.........they stick out like a sore thumb so you won't have trouble finding them :)
 

imported_vr6

Platinum Member
Jul 6, 2001
2,740
0
0
man i love this place! so much love going on here! Thanks everybody for their help,snapster, knightbreed, kelverick, and everybody else that helped! I finally got everything to work!

this is my first programming class ( CMSC 104 ) and i thinks its fun but hard!

I and gonna get a book this summer and read more about C language. hopefully it'll help me out when i get into cmsc201 and 202. Thanks for the help guys, i appreaciate it!.

Kwan1