Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c "struct" error

Status
Not open for further replies.

troye

Technical User
May 16, 2003
21
MY
anyone know what is causing the error in this small program? I'm having a little trouble with Structs.
(copy/paste into compiler)

Thanks

Error:
d11.7.c:36: error: parse error before "part_item"
d11.7.c:37: error: parse error before "part_item"

Program:
---------------------

#include <stdio.h>
#include <string.h>


struct part_item
{
char number[10];
float price;
int qty;
};


void print_inv(stuct part_item );
float find_inv(stuct part_item );




void main(void)
{


struct part_item part;
struct part_item old_item;

part.price = 0.35;
strcpy (part.number, &quot;SMS0001&quot;);
part.qty = 20;
printf(&quot;What is the part number? &quot;);
gets(old_item.number);
printf(&quot;The Price? &quot;);
scanf(&quot;%f&quot;, &old_item.price);
printf(&quot;And the quantity? &quot;);
scanf(&quot;%d&quot;, &old_item.qty);


print_inv(part);
print_inv(old_item);
}

void print_inv(struct part_item prt)
{
float inventory;

inventory = find_inv(prt);
printf(&quot;%s\t%.2f\t%d\t%.2f\n&quot;, prt.number, prt.price, prt.qty, inventory+1);

}

float find_inv(struct part_item prt)
{
return (prt.price * prt.qty);
}

 
> void print_inv(stuct part_item );
> float find_inv(stuct part_item );
Check your spelling of struct (hint, it needs an 'r')

> void main(void)
main returns an int, and the normal return value from main is 0
Code:
int main(void) {
    // your code
    return 0;
}

> gets(old_item.number);
Never use gets(), it is simply not safe to do so.

In fact, mixing scanf() with other input methods invariably leads to grief at some point, mostly due to the way scanf() leaves unprocessed characters on the input stream.
Code:
#include <stdio.h>
#include <string.h>


struct part_item
{
  char number[10];
  float price;
  int qty;
};


void print_inv(struct part_item );
float find_inv(struct part_item );

int main(void)
{
    char buff[BUFSIZ];
    char *nl;
    struct part_item part;
    struct part_item old_item;

    part.price = 0.35;
    strcpy (part.number, &quot;SMS0001&quot;);
    part.qty = 20;
    
    printf(&quot;What is the part number? &quot;);
    fflush( stdout );                   // ensure output can be seen
//    gets(old_item.number);
    fgets( buff, sizeof buff, stdin );  // read a line
    nl = strchr(buff,'\n');             // find the '\n'
    if ( nl != NULL ) *nl = '\0';       // remove the '\n' if it was found
    strncpy(old_item.number,buff,sizeof old_item.number);   // safely copy the input
    old_item.number[sizeof old_item.number - 1] = '\0';

    printf(&quot;The Price? &quot;);
    fflush( stdout );
//    scanf(&quot;%f&quot;, &old_item.price);
    fgets( buff, sizeof buff, stdin );
    sscanf( buff, &quot;%f&quot;, &old_item.price);

    printf(&quot;And the quantity? &quot;);
    fflush( stdout );
//    scanf(&quot;%d&quot;, &old_item.qty);
    fgets( buff, sizeof buff, stdin );
    sscanf( buff, &quot;%d&quot;, &old_item.qty);

    print_inv(part);
    print_inv(old_item);
    return 0;
}

void print_inv(struct part_item prt)
{
    float     inventory;

    inventory = find_inv(prt);
    printf(&quot;%s\t%.2f\t%d\t%.2f\n&quot;, prt.number, prt.price, prt.qty, inventory+1);

}

float find_inv(struct part_item prt)
{
    return (prt.price * prt.qty);
}
Note:
Both fgets() and sscanf() return a status result which should be checked in any production code.

--
 
SONOFA!! thanks...i'm retarded.

Happy New Year
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top