[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Struct Help

Posted on 2006-04-22
36
Medium Priority
?
734 Views
Last Modified: 2013-12-14
Hello Everyone Again

I have another question. We are now doing structs and the idea is simple but pointing and bringing in a struct as a parameter is tricky.

Here below is a sample code of what Ive done so far. Im really just trying to take steps with program as it kind of a long one. If I can understand this one I should be fine with my final exam. Purpose of the program is store data within a struct! Then the user is given a menu that will ask the user what they would like to do with the data..i.e. store, delete, clear all, edit and etc.

Here is what I have so far. Keep in mind We were told if we were having trouble loading and saving we should just hardcode values in for the struct. So currently my program sits as trying to point to the hardcode function and then print the data out.


---------------------------------------------------------------
#include<stdio.h>
#include<string.h>
#define SIZE 20
#define MAX 100

typedef struct{
      char brand[SIZE];
      char company[SIZE];
      int inStock;
      char lastShipDate[SIZE];
      double cost;
      double price;
} bev_t;



int main()
{
      int *size=0;
      int i=0;

      HardCodeEntries(bev_t *entry[], int *size);


      while(i<*size)
      {

            printf("%lf\n", *entry[i].brand);
            printf("%s\n", *entry[i].company);
            printf("%lf\n", *entry[i].inStock);
            printf("%s\n", *entry[i].lastShipDate);
            printf("%lf\n", *entry[i].cost);
            printf("%lf\n", *entry[i].price);
            i++;
      }







void HardCodeEntries(bev_t entry[], int *size)
{
      //for charater arryas you need to use string copy function
       strcpy(entry[0].brand,"BUDWEISER");
       strcpy(entry[0].lastShipDate,"04/15/06");
       entry[0].cost=5.00;
       strcpy(entry[0].company,"BUSCH");
       entry[0].price=6.00;
       entry[0].inStock=59;

      *size +=1;

       strcpy(entry[1].brand,"COORS");
       strcpy(entry[1].lastShipDate,"04/15/06");
       entry[1].cost=5.00;
       strcpy(entry[1].company,"COORS");
       entry[1].price=6.00;
       entry[1].inStock=59;

      *size +=1;

       strcpy(entry[2].brand,"LITE");
       strcpy(entry[2].lastShipDate,"04/15/06");
       entry[2].cost=5.00;
       strcpy(entry[2].company,"MILLER");
       entry[2].price=6.00;
       entry[2].inStock=59;

      *size +=1;

       strcpy(entry[3].brand,"HEINEKEN");
       strcpy(entry[3].lastShipDate,"04/15/06");
       entry[3].cost=5.00;
       strcpy(entry[3].company,"HEINEKEN");
       entry[3].price=6.00;
       entry[3].inStock=59;
      
      *size +=1;

       strcpy(entry[4].brand,"MILLER");
       strcpy(entry[4].lastShipDate,"04/15/06");
       entry[4].cost=5.00;
       strcpy(entry[4].company,"MILLER");
       entry[4].price=6.00;
       entry[4].inStock=59;

      *size +=1;
      
}


--------------------------------------

Thanks Again!
0
Comment
Question by:Redevo7
  • 23
  • 12
36 Comments
 
LVL 14

Expert Comment

by:cwwkie
ID: 16516035
First look at the declaration of HardCodeEntries:
     void HardCodeEntries(bev_t entry[], int *size);
So the first parameter is an array of bev_t (or actually a pointer of the first element of the array)

You must first define such an array, and then you can call HardCodeEntries.

btw, there are two errors in this line:
      printf("%lf\n", *entry[i].brand);
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 16516447
Also, in main() size wants to be an integer, not a pointer to an integer.
Then you will pass the address of size to HardCodeEntries().

0
 

Author Comment

by:Redevo7
ID: 16517131
Whats wrong here?

printf("%lf\n", *entry[i].brand);
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Redevo7
ID: 16517142
oh wait I saw it...oops.

Let me try to finish the function then I can get to the menu.
0
 

Author Comment

by:Redevo7
ID: 16517143
Ok What I am stuck is how do you pass an array to a function defition's parameter?
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 16517177
> Ok What I am stuck is how do you pass an array to a function defition's parameter?

You probably did not recognise it, but you are already passing an array of char to strcpy!
the prototype of strcpy is
     char *  strcpy ( char * dest, const char * src );
which is equivalent to:
     char *  strcpy ( char dest[], const char src[] );

char s1[20]="one";     // array of char
char s2[10]="two";
strcpy(s1, s2);

You need to do something simular, but with bev_t as base type.
0
 

Author Comment

by:Redevo7
ID: 16517207
Yikes that seems a little complicated. Let me start again with my questions as I'm not following the syntax yet.

lets say this was my struct:
typedef struct{
.
.
.
}bev_t;

For this to be a struct of array type what would I need to do to make it so?

then comes my int main( )

do I place:

bev_t entry[20];

within it?Or does it belong outside my int main?

Also what does this message mean?

warning C4101: 'entry' : unreferenced local variable

This occurs if I place:

bev_t entry[20];

in the int main()

Should I consider this as a declaration?


Thanks again.
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 16517222
>   bev_t entry[20];

That's fine. It is better to place it inside main, although it will work too if you define it outside main.
You get the warning because you don't use it yet. When you call HardCodeEntries, you are using it, so the warning will go away.

Otherwise, post the new code, so we can give comments on it.
0
 

Author Comment

by:Redevo7
ID: 16517334
so is:

 void HardCodeEntries(entry[], int *size)
{ }

a good function name?

But how do I call it out in my int main?

HardCodeEntries(bev_t entry[], int *size);

?
0
 

Author Comment

by:Redevo7
ID: 16517350
Here is what I have so far.

Im trying to just make my menu work before anything and have some values for the structs.

-----------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
#define SIZE 20
#define MAX 100

typedef struct{
      char brand[SIZE];
      char company[SIZE];
      int inStock;
      char lastShipDate[SIZE];
      double cost;
      double price;
}bev_t;




void HardCodeEntries(bev_t entry[SIZE],int size);


char Menu();

int main()
{
      int *size=0;
      int i=0;
      char choice;

      bev_t entry[SIZE];


      void HardCodeEntries(bev_t entry[SIZE], *size);


      choice=Menu();

      while(choice != 'q')
      {
            if(choice=='a')
            {
                  printf("\nYou have chosen AddEntry\n\n");
                  //AddEntry(entry[*size]);
                  Menu();

            }


            else if(choice=='d')
            {
                  printf("\nYou have chosen DelEntry\n\n");
                  //DeleteEntry(entry[*size]);
                  Menu();
            }


            else if(choice=='e')
            {
                  printf("\nYou have chosen EditEntry\n\n");
                  //EditEntry(entry[*size]);
                  Menu();
            }


            else if(choice=='p')
            {
                  printf("\nYou have chosen PrintEntry\n\n");
                  //PrintEntrys(entry[*size]);
            }


            else if(choice=='s')
            {
                  printf("\nYou have chosen SaveEntry\n\n");
                  //SaveEntrys(entry[*size]);
            }


            else if(choice=='c')
            {
                  printf("\nYou have chosen ClearEntry\n\n");
                  //ClearEntrys(entry[*size]);
            }


            else if(choice=='l')
            {
                  printf("\nYou have chosen LoadEntry\n\n");
                  //LoadEntrys(entry[*size]);
            }


            else            
            printf("Invalid entry; please try again");
      }




      /*while(i<*size)
      {
            printf("hello\n\n");
            printf("%s\n", entry[i].brand);
            printf("%s\n", entry[i].company);
            printf("%d\n", entry[i].inStock);
            printf("%s\n", entry[i].lastShipDate);
            printf("%f\n", entry[i].cost);
            printf("%f\n", entry[i].price);
            i++;
      }*/
      
      return 0;
}


void HardCodeEntries(entry[], int *size)
{
      //for charater arryas you need to use string copy function
       strcpy(entry[0].brand,"BUDWEISER");
       strcpy(entry[0].lastShipDate,"04/15/06");
       entry[0].cost=5.00;
       strcpy(entry[0].company,"BUSCH");
       entry[0].price=6.00;
       entry[0].inStock=59;

      *size +=1;

       strcpy(entry[1].brand,"COORS");
       strcpy(entry[1].lastShipDate,"04/15/06");
       entry[1].cost=5.00;
       strcpy(entry[1].company,"COORS");
       entry[1].price=6.00;
       entry[1].inStock=59;

      *size +=1;

       strcpy(entry[2].brand,"LITE");
       strcpy(entry[2].lastShipDate,"04/15/06");
       entry[2].cost=5.00;
       strcpy(entry[2].company,"MILLER");
       entry[2].price=6.00;
       entry[2].inStock=59;

      *size +=1;

       strcpy(entry[3].brand,"HEINEKEN");
       strcpy(entry[3].lastShipDate,"04/15/06");
       entry[3].cost=5.00;
       strcpy(entry[3].company,"HEINEKEN");
       entry[3].price=6.00;
       entry[3].inStock=59;
      
      *size +=1;

       strcpy(entry[4].brand,"MILLER");
       strcpy(entry[4].lastShipDate,"04/15/06");
       entry[4].cost=5.00;
       strcpy(entry[4].company,"MILLER");
       entry[4].price=6.00;
       entry[4].inStock=59;

      *size +=1;      
}

char Menu()
{

      char choice='q';

      printf("\n\nPlease choose a selection from the options below\n");
      printf("\t\ta.  Add a New Entry\n");
      printf("\t\td.  Delete Entry\n");
      printf("\t\te.  Edit an Entry\n");
      printf("\t\tp.  Display Current Data Stored\n");
      printf("\t\ts.  Save Current Data\n");
      printf("\t\tc.  Clear the Entire Inventory\n");
      printf("\t\tl.  Load Current Sales From a File\n");
      printf("\t\tq.  Quit\n\n>>");

      scanf("%c", &choice);

      printf("\n\n");

      return choice;
}


--------------------------------------------------------------------------


Im getting some errors.... and just can't quite grasp the struct stuff. I guess just getting a pointer back to each struct is whats confusing me right now. Im not sure if Im using the right syntax within the int main, prototype and then the function definition (both parameters of and the syntax within the function.)

Thanks again.
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 16518247
You have to call it this way:

     int size=0;    // see comment of brettmjohnson {http:16516447}
     bev_t entry[SIZE];

     void HardCodeEntries(entry, &size);
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 16518282
And to display the result, it is better to use a for loop:
     for(i=0; i<size; i++)
     {
          printf("hello\n\n");
          printf("%s\n", entry[i].brand);
          printf("%s\n", entry[i].company);
          printf("%d\n", entry[i].inStock);
          printf("%s\n", entry[i].lastShipDate);
          printf("%lf\n", entry[i].cost);     // %lf because it is a double
          printf("%lf\n", entry[i].price);    // %lf because it is a double
     }

If you have compiler errors, please copy them exact, and mark the corresponding line in the sourcecode, because I cannot see the line numbers.
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 16518306
>>> HardCodeEntries
>>> a good function name?

I would choose something like InitializeBeverageArray, but that's not the most important part.

#define SIZE 20
#define MAX 100

I think you should use better names for these. It is not clear which size and which maximum you mean here. And you use SIZE in two different places, with a different meaning. That's not good either.
0
 

Author Comment

by:Redevo7
ID: 16519181
cwwkie:

where does

void HardCodeEntries(entry, &size);

go. Does it go in my in main, is that its prototype?

I just can't get past the errors!!!!


Give me a minute to post my syntax.
0
 

Author Comment

by:Redevo7
ID: 16519221
Below is my coding.
----------------------------#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100



typedef struct{
      char brand[20];
      char company[20];
      int inStock;
      char lastShipDate[20];
      double cost;
      double price;
}bev_t;

char Menu();

void PrintEntrys(bev_t,int &size);

void InitialBevEntry(bev_t,int &size);


int main()
{
      int *size=0;
      int i=0;
      char choice;

      bev_t entry[6];


      void InitialBevEntry(entry, *size);


      choice=Menu();

      while(choice != 'q')
      {
            if(choice=='a')
            {
                  printf("\nYou have chosen AddEntry\n\n");
                  //AddEntry(entry[*size]);
                  //Menu();
            }


            else if(choice=='d')
            {
                  printf("\nYou have chosen DelEntry\n\n");
                  //DeleteEntry(entry[*size]);
                  //Menu();
            }


            else if(choice=='e')
            {
                  printf("\nYou have chosen EditEntry\n\n");
                  //EditEntry(entry[*size]);
                  //Menu();
            }


            else if(choice=='p')
            {
                  printf("\nYou have chosen PrintEntry\n\n");
                  PrintEntrys(entry[6], *size);
            }


            else if(choice=='s')
            {
                  printf("\nYou have chosen SaveEntry\n\n");
                  //SaveEntrys(entry[*size]);
            }


            else if(choice=='c')
            {
                  printf("\nYou have chosen ClearEntry\n\n");
                  //ClearEntrys(entry[*size]);
            }


            else if(choice=='l')
            {
                  printf("\nYou have chosen LoadEntry\n\n");
                  //LoadEntrys(entry[*size]);
            }


            else            
            printf("Invalid entry; please try again");

            Menu( );
      }


      
      return 0;
}


void InitialBevEntry(bev_t entry, int *size)
{
      //for charater arryas you need to use string copy function
       strcpy(entry[0].brand,"BUDWEISER");
       strcpy(entry[0].lastShipDate,"04/15/06");
       entry[0].cost=5.00;
       strcpy(entry[0].company,"BUSCH");
       entry[0].price=6.00;
       entry[0].inStock=59;

      *size +=1;

       strcpy(entry[1].brand,"COORS");
       strcpy(entry[1].lastShipDate,"04/15/06");
       entry[1].cost=5.00;
       strcpy(entry[1].company,"COORS");
       entry[1].price=6.00;
       entry[1].inStock=59;

      *size +=1;

       strcpy(entry[2].brand,"LITE");
       strcpy(entry[2].lastShipDate,"04/15/06");
       entry[2].cost=5.00;
       strcpy(entry[2].company,"MILLER");
       entry[2].price=6.00;
       entry[2].inStock=59;

      *size +=1;

       strcpy(entry[3].brand,"HEINEKEN");
       strcpy(entry[3].lastShipDate,"04/15/06");
       entry[3].cost=5.00;
       strcpy(entry[3].company,"HEINEKEN");
       entry[3].price=6.00;
       entry[3].inStock=59;
      
      *size +=1;

       strcpy(entry[4].brand,"MILLER");
       strcpy(entry[4].lastShipDate,"04/15/06");
       entry[4].cost=5.00;
       strcpy(entry[4].company,"MILLER");
       entry[4].price=6.00;
       entry[4].inStock=59;

      *size +=1;      
}

char Menu()
{

      char choice;

      printf("\n\nPlease choose a selection from the options below\n");
      printf("\t\ta.  Add a New Entry\n");
      printf("\t\td.  Delete Entry\n");
      printf("\t\te.  Edit an Entry\n");
      printf("\t\tp.  Display Current Data Stored\n");
      printf("\t\ts.  Save Current Data\n");
      printf("\t\tc.  Clear the Entire Inventory\n");
      printf("\t\tl.  Load Current Sales From a File\n");
      printf("\t\tq.  Quit\n\n>>");

      scanf("%c", &choice);

      printf("\n\n");

      return choice;
}

void PrintEntrys(entry, int *size)
{
      int i;
      for(i=0; i<*size; i++)
    {
            printf("hello\n\n");
            printf("%s\n", entry[i].brand);
            printf("%s\n", entry[i].company);
            printf("%d\n", entry[i].inStock);
            printf("%s\n", entry[i].lastShipDate);
            printf("%lf\n", entry[i].cost);     // %lf because it is a double
            printf("%lf\n", entry[i].price);    // %lf because it is a double}
      }

}

--------------------------------------------------------------------

error messages:
------ Build started: Project: beer3, Configuration: Debug Win32 ------
Compiling...
beer3.c
beer3.c(19) : error C2143: syntax error : missing ')' before '&'
beer3.c(19) : error C2143: syntax error : missing '{' before '&'
beer3.c(19) : error C2059: syntax error : '&'
beer3.c(19) : error C2059: syntax error : ')'
beer3.c(21) : error C2143: syntax error : missing ')' before '&'
beer3.c(21) : error C2143: syntax error : missing '{' before '&'
beer3.c(21) : error C2059: syntax error : '&'
beer3.c(21) : error C2059: syntax error : ')'
beer3.c(33) : error C2059: syntax error : ','
beer3.c(33) : error C2143: syntax error : missing ')' before '*'
beer3.c(33) : error C2143: syntax error : missing ';' before '*'
beer3.c(33) : error C2059: syntax error : ')'
beer3.c(67) : warning C4013: 'PrintEntrys' undefined; assuming extern returning int
beer3.c(107) : error C2088: '[' : illegal for struct
beer3.c(107) : error C2198: 'strcpy' : too few arguments for call
beer3.c(108) : error C2088: '[' : illegal for struct
beer3.c(108) : error C2198: 'strcpy' : too few arguments for call
beer3.c(109) : error C2088: '[' : illegal for struct
beer3.c(110) : error C2088: '[' : illegal for struct
beer3.c(110) : error C2198: 'strcpy' : too few arguments for call
beer3.c(111) : error C2088: '[' : illegal for struct
beer3.c(112) : error C2088: '[' : illegal for struct
beer3.c(116) : error C2088: '[' : illegal for struct
beer3.c(116) : error C2198: 'strcpy' : too few arguments for call
beer3.c(117) : error C2088: '[' : illegal for struct
beer3.c(117) : error C2198: 'strcpy' : too few arguments for call
beer3.c(118) : error C2088: '[' : illegal for struct
beer3.c(119) : error C2088: '[' : illegal for struct
beer3.c(119) : error C2198: 'strcpy' : too few arguments for call
beer3.c(120) : error C2088: '[' : illegal for struct
beer3.c(121) : error C2088: '[' : illegal for struct
beer3.c(125) : error C2088: '[' : illegal for struct
beer3.c(125) : error C2198: 'strcpy' : too few arguments for call
beer3.c(126) : error C2088: '[' : illegal for struct
beer3.c(126) : error C2198: 'strcpy' : too few arguments for call
beer3.c(127) : error C2088: '[' : illegal for struct
beer3.c(128) : error C2088: '[' : illegal for struct
beer3.c(128) : error C2198: 'strcpy' : too few arguments for call
beer3.c(129) : error C2088: '[' : illegal for struct
beer3.c(130) : error C2088: '[' : illegal for struct
beer3.c(134) : error C2088: '[' : illegal for struct
beer3.c(134) : error C2198: 'strcpy' : too few arguments for call
beer3.c(135) : error C2088: '[' : illegal for struct
beer3.c(135) : error C2198: 'strcpy' : too few arguments for call
beer3.c(136) : error C2088: '[' : illegal for struct
beer3.c(137) : error C2088: '[' : illegal for struct
beer3.c(137) : error C2198: 'strcpy' : too few arguments for call
beer3.c(138) : error C2088: '[' : illegal for struct
beer3.c(139) : error C2088: '[' : illegal for struct
beer3.c(143) : error C2088: '[' : illegal for struct
beer3.c(143) : error C2198: 'strcpy' : too few arguments for call
beer3.c(144) : error C2088: '[' : illegal for struct
beer3.c(144) : error C2198: 'strcpy' : too few arguments for call
beer3.c(145) : error C2088: '[' : illegal for struct
beer3.c(146) : error C2088: '[' : illegal for struct
beer3.c(146) : error C2198: 'strcpy' : too few arguments for call
beer3.c(147) : error C2088: '[' : illegal for struct
beer3.c(148) : error C2088: '[' : illegal for struct
beer3.c(175) : error C2122: 'size' : prototype parameter in name list illegal
beer3.c(176) : error C2371: 'PrintEntrys' : redefinition; different basic types
beer3.c(181) : error C2109: subscript requires array or pointer type
beer3.c(182) : error C2109: subscript requires array or pointer type
beer3.c(183) : error C2109: subscript requires array or pointer type
beer3.c(184) : error C2109: subscript requires array or pointer type
beer3.c(185) : error C2109: subscript requires array or pointer type
beer3.c(186) : error C2109: subscript requires array or pointer type
beer3 - 65 error(s), 1 warning(s)
-----------------------------------------------------------

I really feel posting my entire coding a bad thing but not sure if you all are ok with that?

0
 
LVL 14

Accepted Solution

by:
cwwkie earned 1600 total points
ID: 16519894
> I really feel posting my entire coding a bad thing but not sure if you all are ok with that?

It would be better if you post only the differences with the previous version, But if you are not sure what to post, it does not matter much.

But you must mark the line numbers which cause a compiler error. For example this error:
     beer3.c(19) : error C2143: syntax error : missing ')' before '&'

You can mark it like this:
     void InitialBevEntry(bev_t,int &size);   /* LINE 19 */
or like this:
     void InitialBevEntry(bev_t,int &size);   /* beer3.c(19) : error C2143: syntax error : missing ')' before '&' */
The other errors on the same line are not important.

I'm going to explain the following part of your sourcecode:
+++++++++++++++++++++++++++++++++++++++++++++
void PrintEntrys(bev_t,int &size);

void InitialBevEntry(bev_t,int &size);

int main()
{
     int *size=0;
     int i=0;
     char choice;

     bev_t entry[6];

     void InitialBevEntry(entry, *size);
+++++++++++++++++++++++++++++++++++++++++++++
     // I mark my comments like this

     // prototypes:
void PrintEntrys(bev_t entries[],int size);  // you need to pass the array of bev_t, and size as a integer.

void InitialBevEntry(bev_t entries[],int * size);  // same here, but a pointer to size, because you want to return it.

int main()
{
     int size=0; // No pointer here. Just a normal int to store the size
     int i=0;
     char choice;

     bev_t entry[6];

     // Now you need to call InitialBevEntry, the prototype is already declared above main,
     // so now just call it with the correct parameters

     InitialBevEntry(entry, &size);  // the array entry, and the address of size
+++++++++++++++++++++++++++++++++++++++++++++
0
 

Author Comment

by:Redevo7
ID: 16520702
I see now the purpose of the pointer for one case and not the other. For the print we don't want to change the global value of size but instead we want to use the value of it for our i<size! In either case we don't even change the value of size within the PrintEntry function defintion. That part makes sense.


Now comes my InitialBevEntry() function defition is giving me errors.

here is the syntax with the errors given to me


void InitialBevEntry(bev_t entry, int &size)//LINE 103
{
      //for charater arryas you need to use string copy function
       strcpy(entry[0].brand,"BUDWEISER");
       strcpy(entry[0].lastShipDate,"04/15/06");
       entry[0].cost=5.00;
       strcpy(entry[0].company,"BUSCH");
       entry[0].price=6.00;
       entry[0].inStock=59;

      *size +=1;

       strcpy(entry[1].brand,"COORS");
       strcpy(entry[1].lastShipDate,"04/15/06");
       entry[1].cost=5.00;
       strcpy(entry[1].company,"COORS");
       entry[1].price=6.00;
       entry[1].inStock=59;

      *size +=1;

       strcpy(entry[2].brand,"LITE");
       strcpy(entry[2].lastShipDate,"04/15/06");
       entry[2].cost=5.00;
       strcpy(entry[2].company,"MILLER");
       entry[2].price=6.00;
       entry[2].inStock=59;

      *size +=1;

       strcpy(entry[3].brand,"HEINEKEN");
       strcpy(entry[3].lastShipDate,"04/15/06");
       entry[3].cost=5.00;
       strcpy(entry[3].company,"HEINEKEN");
       entry[3].price=6.00;
       entry[3].inStock=59;
      
      *size +=1;

       strcpy(entry[4].brand,"MILLER");
       strcpy(entry[4].lastShipDate,"04/15/06");
       entry[4].cost=5.00;
       strcpy(entry[4].company,"MILLER");
       entry[4].price=6.00;
       entry[4].inStock=59;

      *size +=1;      
}

-------------------------------------
beer3.c(103) : error C2143: syntax error : missing ')' before '&'
beer3.c(103) : error C2143: syntax error : missing '{' before '&'
beer3.c(103) : error C2059: syntax error : '&'
beer3.c(103) : error C2059: syntax error : ')'
-------------------------------------


0
 

Author Comment

by:Redevo7
ID: 16521551
I just can't get rid of those errors above....
0
 

Author Comment

by:Redevo7
ID: 16521577
Ok I got rid of the errors. Now Im trying to get my menu to work. When I enter a as my choice it prints out the right output but I have it where i loops back to the menu option screen. When it asks again to choose, I enter another option but it always gives me a output prompt as if I had chosen a again. Am I supposed to clear the choice before i write choice=menu(); again?
0
 

Author Comment

by:Redevo7
ID: 16521721
This is so weird what am I doing wrong here!!!

while(choice != 'q')
      {
            if(choice='a')
            {
                  printf("\nYou have chosen AddEntry\n\n");
                  choice="";
                  //AddEntry(entry[*size]);
                  //Menu();
            }


            else if(choice='d')
            {
                  printf("\nYou have chosen DelEntry\n\n");
                  //DeleteEntry(entry[*size]);
                  //Menu();
            }


            else if(choice='e')
            {
                  printf("\nYou have chosen EditEntry\n\n");
                  //EditEntry(entry[*size]);
                  //Menu();
            }


            else if(choice='p')
            {
                  printf("\nYou have chosen PrintEntry\n\n");
                  PrintEntrys(entry, size);
            }


            else if(choice='s')
            {
                  printf("\nYou have chosen SaveEntry\n\n");
                  //SaveEntrys(entry[*size]);
            }


            else if(choice='c')
            {
                  printf("\nYou have chosen ClearEntry\n\n");
                  //ClearEntrys(entry[*size]);
            }


            else if(choice='l')
            {
                  printf("\nYou have chosen LoadEntry\n\n");
                  //LoadEntrys(entry[*size]);
            }


            else            
            {printf("Invalid entry; please try again");}

            choice=Menu();

      }


When i execute it always gives me as if I have chosen a as my choice now!
0
 

Author Comment

by:Redevo7
ID: 16522023
Ok I got it to work sort of. Below is my int main and the menu selection portion


------------------------------
int main()
{
      int size=0;
      int i=0;
      char choice='0';

      bev_t entry[20];


      void InitialBevEntry(bev_t entry[20], int *size);

      printf("\n\nPlease choose a selection from the options below\n");
      printf("\t\ta.  Add a New Entry\n");
      printf("\t\td.  Delete Entry\n");
      printf("\t\te.  Edit an Entry\n");
      printf("\t\tp.  Display Current Data Stored\n");
      printf("\t\ts.  Save Current Data\n");
      printf("\t\tc.  Clear the Entire Inventory\n");
      printf("\t\tl.  Load Current Sales From a File\n");
      printf("\t\tq.  Quit\n\n>>");


      choice=Menu();

      while(choice != 'q')
      {
            if(choice=='a')
            {
                  printf("\nYou have chosen AddEntry\n\n");
                  //AddEntry(entry[*size]);
                  //Menu();
            }


            else if(choice=='d')
            {
                  printf("\nYou have chosen DelEntry\n\n");
                  //DeleteEntry(entry[*size]);
                  //Menu();
            }


            else if(choice=='e')
            {
                  printf("\nYou have chosen EditEntry\n\n");
                  //EditEntry(entry[*size]);
                  //Menu();
            }


            else if(choice=='p')
            {
                  printf("\nYou have chosen PrintEntry\n\n");
                  PrintEntrys(entry, size);
            }

            choice=Menu();
      }

      printf("\n\nThank you\n\n");

      
      return 0;
}


char Menu( )
{
      char choice;

      scanf("%c", &choice);

      return choice;
}

            else if(choice=='s')
            {
                  printf("\nYou have chosen SaveEntry\n\n");
                  //SaveEntrys(entry[*size]);
            }


            else if(choice=='c')
            {
                  printf("\nYou have chosen ClearEntry\n\n");
                  //ClearEntrys(entry[*size]);
            }


            else if(choice=='l')
            {
                  printf("\nYou have chosen LoadEntry\n\n");
                  //LoadEntrys(entry[*size]);
            }

            choice=Menu();
      }

      printf("\n\nThank you\n\n");

      
      return 0;
}


------------------------------------

Couple comments I wanted to add. At first I wanted to add a line that said

else
printf("error try again\n\n");

and then wanted to add another line that said

printf("please choose again");

problem was I was always getting the message error prompted.

If I just took out the else statement and just left it with the printf("please choose again.");
 it would prompt twice onto the screen! Not sure what is going on. Also before anything I had the options for the user to choose within the PrintEntry() function but it seem to give me MORE problems!

Any insight?
0
 

Author Comment

by:Redevo7
ID: 16522099
I guess I had to place printf("choose again"); within each condition...i.e:
---------------------------------------------------------------------

        else if(choice=='p')
          {
               printf("\nYou have chosen PrintEntry\n\n");
               PrintEntrys(entry, size);
               printf("choose again");
          }

----------------------------------------------------------------------
... and etc for each option


Mess after mess but its getting there. New problem.... Why isnt my function PrintEntrys Working?


Going back to the previous post about my  int mainfunction
------------------------------------------------------------------------
        else if(choice=='p')
          {
               printf("\nYou have chosen PrintEntry\n\n");
               PrintEntrys(entry, size);
          }
------------------------------------------------------------------------

and the definition for PrintEntrys( ) is:

void PrintEntrys(bev_t entry[],int size)
{
      int i;
      for(i=0; i<size; i++)
    {
            printf("hello\n\n");
            printf("%s\n", entry[i].brand);
            printf("%s\n", entry[i].company);
            printf("%d\n", entry[i].inStock);
            printf("%s\n", entry[i].lastShipDate);
            printf("%lf\n", entry[i].cost);     // %lf because it is a double
            printf("%lf\n", entry[i].price);    // %lf because it is a double}
      }

}

--------------------------------------------------------------
0
 

Author Comment

by:Redevo7
ID: 16522158
Is this portion of my forced values of entry struct even correct?


----------------------------------
void InitialBevEntry(bev_t entry[], int *size)
{
      //for charater arryas you need to use string copy function
       strcpy(entry[0].brand,"BUDWEISER");
       strcpy(entry[0].lastShipDate,"04/15/06");
       entry[0].cost=5.00;
       strcpy(entry[0].company,"BUSCH");
       entry[0].price=6.00;
       entry[0].inStock=59;

      *size +=1;

       strcpy(entry[1].brand,"COORS");
       strcpy(entry[1].lastShipDate,"04/15/06");
       entry[1].cost=5.00;
       strcpy(entry[1].company,"COORS");
       entry[1].price=6.00;
       entry[1].inStock=59;

      *size +=1;

       strcpy(entry[2].brand,"LITE");
       strcpy(entry[2].lastShipDate,"04/15/06");
       entry[2].cost=5.00;
       strcpy(entry[2].company,"MILLER");
       entry[2].price=6.00;
       entry[2].inStock=59;

      *size +=1;

       strcpy(entry[3].brand,"HEINEKEN");
       strcpy(entry[3].lastShipDate,"04/15/06");
       entry[3].cost=5.00;
       strcpy(entry[3].company,"HEINEKEN");
       entry[3].price=6.00;
       entry[3].inStock=59;
      
      *size +=1;

       strcpy(entry[4].brand,"MILLER");
       strcpy(entry[4].lastShipDate,"04/15/06");
       entry[4].cost=5.00;
       strcpy(entry[4].company,"MILLER");
       entry[4].price=6.00;
       entry[4].inStock=59;

      *size +=1;

}
-----------------------------------------------------------
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 16522646
I live in a different time zone, so I cannot respond when I'm asleep (it's now 8:40 am here). But I see you are able to solve the problems yourself too.... :-)

In your mainfunction you just put the declaration of InitialBevEntry:
     void InitialBevEntry(bev_t entry[20], int *size);
But that does not call it. See the last line of my previous comment.
0
 

Author Comment

by:Redevo7
ID: 16525838
Yeah Im starting to finish the project. Ill more then likely get stuck again tho!

Thanks again.
0
 

Author Comment

by:Redevo7
ID: 16526549
Another question... I want to use a strncmp function but I'm forgeting how to find out the length of a word.

so far what I have decided is:
  1. I print on to the screen current brand names availlable
  2. I want to ask the user for which brand to edit
  3. Then I want to get a strncmp( original brand name, typed in brand name, length to compare)
  4. How do I get the length of typed in brand name.

So far Im guessing we get a while loop that looks for the NULL operator for the "typed in brand name arrar" and then return a value correct?
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 16526641
>  3. Then I want to get a strncmp( original brand name, typed in brand name, length to compare)

There is no need to compare a part of the string if both strings must be equal. Just use strcmp instead.

But if you want the user to enter just the first few characters, for example HE if HEINEKEN is meant, you can use strlen(s) to get the length of the string the user has entered.

If you want a case insensiteve compare, you can use stricmp or strnicmp, but it's not part of the ansi c-standard.

here are different standard str* functions explained: http://cplusplus.com/ref/cstring/
0
 

Author Comment

by:Redevo7
ID: 16526845
Seems really difficult

here is my syntax so far:

-------------------------------------------------------------------------------------------
void EditEntry(bev_t entry[], int size)
{
      int i=0,
            c=0,
            x=0;
      char trash;
      char editbrand[20];
      int length_word_brand=0;
      printf("\nPlease enter which Brand you would like to edit");
      while(i<size)
      {
            printf("\n\t%s", entry[i].brand);
            i++;
      }
      
      printf("\n\t\t>>");

      scanf("%s", &editbrand);

      length_word_brand=strlen(editbrand);


      while(c<length_word_brand)
      {
      editbrand[c] = toupper(editbrand[c]);
      c++;
      }
      
      
      if(strncmp(entry[x].brand,editbrand,length_word_brand)==1)
      {
      scanf("%c", &trash);

      printf("\nEnter Brand of beer  ");
      scanf("%s", &entry[x].brand);


      printf("\nEnter Company of Brand  ");
      scanf("%s", &entry[x].company);


      printf("\nEnter number of bottles in stock  ");
      scanf("%lf", &entry[x].inStock);


      printf("\nEnter Last Date of Shipment  ");
      scanf("%s", &entry[x].lastShipDate);


      printf("\nEnter dealer cost or beer  ");
      scanf("%lf", &entry[x].cost);


      printf("\nEnter retail price of beer  ");
      scanf("%lf", &entry[x].price);
      }
}
----------------------------------------------------------------------------------------------

How do i get the correct value of x?
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 16527133
> How do i get the correct value of x?

* Loop through all brands (with a for loop, the same way as you print it), use i as index.
* If the current entry is equal, then you have found the correct entry. So remember that index. (x=i;)
0
 

Author Comment

by:Redevo7
ID: 16528844
So then I wouldn't even have to use a strcmp if I just put in a condition:

for(i=0;i<size;i++)
{
     if(editbrand==entry[i].brand)
     {.....}
}


0
 

Author Comment

by:Redevo7
ID: 16528887
hmmm something is missing here


------------------------------------------------------------
void EditEntry(bev_t entry[], int size)
{
      int i=0,
            c=0,
            x=0;
      char trash;
      char editbrand[20];
      int length_word_brand=0;

      scanf("%c", &trash);

      printf("\nPlease enter which Brand you would like to edit");
      while(i<size)
      {
            printf("\n\t%s", entry[i].brand);
            i++;
      }
      
      printf("\n\t\t>>");

      scanf("%s", &editbrand);

      length_word_brand=strlen(editbrand);

      

      while(c<length_word_brand)
      {
      editbrand[c] = toupper(editbrand[c]);
      c++;
      }
      
      
      for(i=0;i<size;i++)
      {
            scanf("%c", &trash);

            if(editbrand==entry[i].brand)
            {
                  x=i;

                  printf("\nEnter Brand of beer  ");
                  scanf("%s", &entry[x].brand);


                  printf("\nEnter Company of Brand  ");
                  scanf("%s", &entry[x].company);

      
                  printf("\nEnter number of bottles in stock  ");
                  scanf("%lf", &entry[x].inStock);


                  printf("\nEnter Last Date of Shipment  ");
                  scanf("%s", &entry[x].lastShipDate);


                  printf("\nEnter dealer cost or beer  ");
                  scanf("%lf", &entry[x].cost);


                  printf("\nEnter retail price of beer  ");
                  scanf("%lf", &entry[x].price);
            }
      }
}
-----------------------------------------------------------------------------------
0
 
LVL 14

Assisted Solution

by:cwwkie
cwwkie earned 1600 total points
ID: 16529109
> if(editbrand==entry[i].brand)

no, you must use a strcmp function to compare strings.

>    for(i=0;i<size;i++)
>    {
>          scanf("%c", &trash);

I don't think you want to execute the scanf each time the for loop is executed.

> scanf("%lf", &entry[x].cost);
during this and the other input, you don't handle newlines properly. Take a look at the following function:

  void digestline(void) {
    scanf("%*[^\n]");   /* Skip to the End of the Line */
    scanf("%*1[\n]");   /* Skip One Newline */
  }
(taken from http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/io.html)

If you change both these things, I think it will work.

btw, this is my last reply for today.
0
 

Author Comment

by:Redevo7
ID: 16529137
Ohhh ok. Thanks for the help either way. This is my last function actually. Will there be others to assist me even though you helped me already?
0
 

Author Comment

by:Redevo7
ID: 16530212
Can someone elaborate on the strncmp real quick please?

What is it returning?
0
 
LVL 14

Assisted Solution

by:cwwkie
cwwkie earned 1600 total points
ID: 16532017
http://cplusplus.com/ref/cstring/strncmp.html

strncmp is returing 0 if the strings are equal

and others are probably not watching this thread any more, you could better post a new question if you need help more quickly.
0
 

Author Comment

by:Redevo7
ID: 16537457
Hey I finished the program. Felt kind of happy I did so well on it. Thanks for the help!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question