• C

Help With Fine Tuning Some Code

I am working on a program for a corespondence course and I can not get it work properly.  It needs to take input from a user and write that inpu to an array of structs via a function.  Then I need a function to printf what's in the array of structs.  I can not seem to get it to work right.  What am I missing here?

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

#define MAX_TITLE 255      
#define MAX_TYPE 255    
#define MAX_ACTOR 255    
#define MAX_VIDEOS 8192      

typedef struct videos
{
      char title [ MAX_TITLE ];
      char type  [ MAX_TYPE ];
      char actor [ MAX_ACTOR ];
};
      
void find(void);
void check_out(void);
void return_videos(void);      
void find_videos(void);
void add_videos(struct videos *new_videos);
void print_inventory(struct videos MAX_VIDEOS[]);
void menu_header(void);
      
struct videos new_videos[MAX_VALUE];

int main(void)
{
      int menu_val;                        

      while ((menu_val = getchar()) != 0)  
      {
      printf( "SCHLOCKLUSTER VIDEO  -  Main Menu: \n\n");
      printf( "DESCRIPTION/FUNCTION:                        ENTER NUMBER: \n");
      printf( "Find video(s) (by title, type, or actors) -       1 \n");
      printf( "Check out video(s)                        -       2 \n");
      printf( "Return video(s)                           -       3 \n");
      printf( "Find all videos out to a customer         -       4 \n");
      printf( "Add new video(s)                          -       5 \n");
      printf( "Print inventory list (sorted by...)       -       6 \n");
      printf( "Reprint the menu options                  -       7 \n\n");

      getchar();
      menu_val -= 48;
          switch (menu_val)
        {
            case 1:
            find();
            break;

            case 2:
            check_out();
            break;

            case 3:
            return_videos();
            break;
              
            case 4:
            find_videos();
            break;

            case 5:
            add_videos(new_videos);
            break;

            case 6:
            print_inventory();
            break;

            default:
            printf("Your selection did not match one of the menu options.\n Please enter a valid option. \n");
            break;
            }       
}      
      return(0);

}

void find(void)
{
printf("This option not available yet.  It is still in development process. \n");
}

void check_out(void)
{
printf("This option not available yet.  It is still in development process. \n");
}

void return_videos(void)
{
printf("This option not available yet.  It is still in development process. \n");
}

void find_videos(void)
{
printf("This option not available yet.  It is still in development process. \n");
}

void add_videos(struct videos new_videos)
{
int i;
fflush(stdin);
printf("\nEnter the title of the video?  ");
gets(new_videos[i].title);

for (i=0; i<255; i++) {
strcpy("title", new_videos[i].title);
}
      
printf("\nWhat genre is the video?  ");
gets(new_videos[i].type);
      
for (i=0; i<255; i++) {
strcpy("type", new_videos[i].type);
}
      
printf("\nEnter upto three actors seperated by spaces:  ");
gets(new_videos[i].actor);
      
for (i=0; i<255; i++) {
strcpy("actor", new_videos[i].actor);
}
      
}

void print_inventory(struct videos MAX_VIDEOS[])
{
int i;
for(i = 0; i < MAX_VIDEOS; i++)
{
printf("Title of movie: %s\n", MAX_VIDEOS[i].title);
printf("Type of move: %s\n", MAX_VIDEOS[i].type);
printf("Actors in movie: %s\n", MAX_VIDEOS[i].actor);
}
}

Any help would be greatly appreciated.

Thanks,

maplah
maplahAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sreenathkCommented:
Hi Maplah,
   There are some doubts in your code.
1. When you are declaring the functions in the beginning, for function void add_video(struct videos *new_video);
The parameter passed is a global variable and this may create some problem in resolving. So you just mention void add_video(struct video *);

2. For the function declaration voice print_inventory(struct videos MAX_VIDEOS[]);
there is a parameter present and when you call the function in main() you did not pass any parameter. More over MAX_VIDEOS is a constant and will be replaced by the value in parsing. So better declaring it as void print_inventory();
So that the global variable new_videos you can use for printing inventory.

3. In function definition of add_videos(), you have declared a variable int i, which is not initialised. Depending on the OS you are using it may got initialized but it is not good to use a variable without initializing. Remeber that you are using the varable i before for loop.
4. Your add_videos function is not proper as it writes 255 strings "title" into into the structure member title. the function strcpy you are using incorrectly. it is in a for loop. That mean you are getting one title and filling it in all the variables.

5. First correct the above corrections. Revert if this is not the solution you are expecting. One more suggestion to you is increase the points for this question as it is complex and that attracts many people around.

Sreenath
sreenath.reddy@philips.com
0
Ready4DisCommented:
//This should work now...

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

#define MAX_TITLE 255      
#define MAX_TYPE 255      
#define MAX_ACTOR 255    
#define Max_Value 8192
int Video_Current=0; //Initialize current video to 0

typedef struct videos
{
char title [ MAX_TITLE ];
char type  [ MAX_TYPE ];
char actor [ MAX_ACTOR ];
};

void find(void);
void check_out(void);
void return_videos(void);
void find_videos(void);
void add_videos(void);
void print_inventory(void);
void menu_header(void);

struct videos new_videos[Max_Value];

int main(void)
{
int menu_val;

while ((menu_val = getchar()) != 0)  
{
printf( "SCHLOCKLUSTER VIDEO  -  Main Menu: \n\n");
printf( "DESCRIPTION/FUNCTION:                        ENTER NUMBER: \n");
printf( "Find video(s) (by title, type, or actors) -       1 \n");
printf( "Check out video(s)                        -       2 \n");
printf( "Return video(s)                           -       3 \n");
printf( "Find all videos out to a customer         -       4 \n");
printf( "Add new video(s)                          -       5 \n");
printf( "Print inventory list (sorted by...)       -       6 \n");
printf( "Reprint the menu options                  -       7 \n\n");

menu_val = (getchar() - 48);
switch (menu_val)
        {
            case 1:
            find();
            break;

            case 2:
            check_out();
            break;

            case 3:
            return_videos();
            break;
         
            case 4:
            find_videos();
            break;

            case 5:
            add_videos();
            break;

            case 6:
            print_inventory();
            break;

            default:
            printf("Your selection did not match one of the menu options.\n Please enter a valid option. \n");
            break;
            }
}
return(0);

}

void find(void)
{
printf("This option not available yet.  It is still in development process. \n");
}

void check_out(void)
{
printf("This option not available yet.  It is still in development process. \n");
}

void return_videos(void)
{
printf("This option not available yet.  It is still in development process. \n");
}

void find_videos(void)
{
printf("This option not available yet.  It is still in development process. \n");
}

void add_videos(struct videos new_videos)
{
fflush(stdin);
printf("\nEnter the title of the video?  ");
gets(new_videos[Video_Current].title);

printf("\nWhat genre is the video?  ");
gets(new_videos[Video_Current].type);

printf("\nEnter upto three actors seperated by spaces:  ");
gets(new_videos[Video_Current].actor);

Video_Current++; // Video_Current = Video_Current + 1;
//Move to next video...
}

void print_inventory(void)
{
int i;
for(i = 0; i < Video_Current; i++) //Loop to last inputed video
{
printf("Title of movie: %s\n", new_videos[i].title);
printf("Type of move: %s\n", new_videos[i].type);
printf("Actors in movie: %s\n", new_videos[i].actor);
}
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maplahAuthor Commented:
Ready4Dis,

I get the following errors when I try to compile:

(72) : error C2001: newline in constant
(73) : error C2146: syntax error : missing ')' before identifier 'option'
(73) : error C2017: illegal escape sequence
(73) : error C2001: newline in constant
(102) : warning C4027: function declared without formal parameter list
(105) : error C2088: '[' : illegal for struct
(105) : error C2198: 'gets' : too few actual parameters
c(108) : error C2088: '[' : illegal for struct
(108) : error C2198: 'gets' : too few actual parameters
c(111) : error C2088: '[' : illegal for struct
(111) : error C2198: 'gets' : too few actual parameters
Error executing cl.exe.

What's mean?

Thanks,

maplah
0
Identify and Prevent Potential Cyber-threats

Become the white hat who helps safeguard our interconnected world. Transform your career future by earning your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

maplahAuthor Commented:
Ready4Dis,

I was able to figure out the first for relating to line (73) - and I might be able to figure out the rest but lines:

105 through 111 are beyond me...

I see you added a counter and changed the name of the structs array - why did you do what you did?

Thanks,

maplah
0
Ready4DisCommented:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_TITLE 255
#define MAX_TYPE 255
#define MAX_ACTOR 255
#define Max_Value 8192
int Video_Current=0; //Initialize current video to 0

struct videos
{
      char title [ MAX_TITLE ];
      char type  [ MAX_TYPE ];
      char actor [ MAX_ACTOR ];
};

void find(void);
void check_out(void);
void return_videos(void);
void find_videos(void);
void add_videos(void);
void print_inventory(void);
void menu_header(void);

videos new_videos[Max_Value];

int main(void)
{
      int menu_val;

      while ((menu_val = getchar()) != '0')
      {
            printf( "SCHLOCKLUSTER VIDEO  -  Main Menu: \n\n");
            printf( "DESCRIPTION/FUNCTION:                        ENTER NUMBER: \n");
            printf( "Find video(s) (by title, type, or actors) -       1 \n");
            printf( "Check out video(s)                        -       2 \n");
            printf( "Return video(s)                           -       3 \n");
            printf( "Find all videos out to a customer         -       4 \n");
            printf( "Add new video(s)                          -       5 \n");
            printf( "Print inventory list (sorted by...)       -       6 \n");
            printf( "Reprint the menu options                  -       7 \n\n");
            getchar();
            switch (menu_val)
            {
                  case '1':
                  find();
                  break;

                  case '2':
                  check_out();
                  break;

                  case '3':
                  return_videos();
                  break;

                  case '4':
                  find_videos();
                  break;

                  case '5':
                  add_videos();
                  break;

                  case '6':
                  print_inventory();
                  break;

                  case '7':
                  //Do nothing, just reprint menu
                  break;

                  default:
                  printf("Your selection did not match one of the menu options.\n Please enter a valid option. \n");
                  break;
            }
      }
      return(0);
}

void find(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

void check_out(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

void return_videos(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

void find_videos(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

void add_videos(void)
{
      fflush(stdin);
      printf("\nEnter the title of the video?  ");
      scanf("%s",new_videos[Video_Current].title);
//      gets(new_videos[Video_Current].title);
      printf("\nWhat genre is the video?  ");
      gets(new_videos[Video_Current].type);
      printf("\nEnter upto three actors seperated by spaces:  ");
      gets(new_videos[Video_Current].actor);
      Video_Current++; // Video_Current = Video_Current + 1;
      //Move to next video...
}

void print_inventory(void)
{
      int i;
      for(i = 0; i < Video_Current; i++) //Loop to last inputed video
      {
            printf("Title of movie: %s\n", new_videos[i].title);
            printf("Type of move: %s\n", new_videos[i].type);
            printf("Actors in movie: %s\n", new_videos[i].actor);
      }
}
0
Ready4DisCommented:
The first code I wrote was just written in wordpad.  Not tested or anything.  This code should be a little bit better to work with.
I added the counter due to the fact that you had no way of telling which video should be next to input, and no way of telling how many to print out.  Now it is stored in a global variable.  I didn't really change anything, except now I use '0' instead of minusing 48.  It just means character '0', character '1'... and so on.  I added a case 7 so you wouldn't get the error message when you hit it.  I didn't do much work on the menu system or anything like that, so it's basically how you had it set up... which was having problems before I got to it.  So if you still need more help than that, I think the points should go up another 25... since the next step for me would basically be re-writing the program.  

Ps. What compiler are you using?  I am just curious, so maybe I can actually use the same compiler to make sure it is compatible before I send it :)
0
maplahAuthor Commented:
Adjusted points to 100
0
maplahAuthor Commented:
Ready4Dis,

I am using Micro$oft Visual C++ to compile and the last code produced 18 errors - I see what you mean about the counter - how else would I know what was written to the array?  My major problem it passing the variables from the functions/structs to the array - that is blowing my mind - do what you must but please add comments so I can learn from you expertise - I have over a week and a half into this one point and I am tired of banging my head off of the keyboard out of frustration - I am desperate and will reward your time accordingly - all I ask is that you sprinkle comments throughout so I can grow from this experience and learn something - I have read over 8 different books - but to not avail - I am the type of person that needs to be shown first - to learn the concepts - I did not have any trouble picking up the less complicated aspects of C - but now I can not seem to get this to work!

I am in your hands - please be gentle...


Thanks,

maplah
0
Ready4DisCommented:
Heh, ok... I will start tonight, and completely go through your code and comment everything that I change and why I had to change it.  I am going out right now, and have work soon, so I will probably have it done tomorrow.  Don't reject my answer till I am done with this program.  I wrote that second version in Borland C/C++.  It ran fine, outside of the Array being to large since I used a 16-bit language as apposed to a 32-bit one.  Anyways, I will start on it first thing after work.

Bill
0
maplahAuthor Commented:
Bill,

I appreciate your effort - the use of remarks will benefit my learning experience.

Thanks again,

maplah
0
maplahAuthor Commented:
Adjusted points to 200
0
maplahAuthor Commented:
Bill,

Haven't heard from you in a while - thought I would jack up the points to peak your interest.

Please - sprinkle mega remarks in the code so I can learn from what you have done.

Thanks,

maplah
0
ozoCommented:
Where are you now, and what help do you need from this point?
0
Ready4DisCommented:
Just to let you know, I am still working on it, just got really tied up lately.  Sorry.  Ok, here's the deal.  I tried to compile my code, and it compiled first time with no problems!  Did you make sure to make the project a Win32 Console App?  Make sure it's a console app, and try this!
0
Ready4DisCommented:
//First thing, Tab program :)

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

#define MAX_TITLE 255
#define MAX_TYPE 255
#define MAX_ACTOR 255
#define Max_Value 8192

struct videos
{
      char title [ MAX_TITLE ];
      char type  [ MAX_TYPE ];
      char actor [ MAX_ACTOR ];
};


/*
      new_videos (video list array) is global.
      counter is global.
      No need to pass anything to any functions now!
*/


//Declare all functions of program!
void find(void);
void check_out(void);
void return_videos(void);
void find_videos(void);
void add_videos(void);
void print_inventory(void);
void menu_header(void);


//Added this function to display menu!
void Show_Menu(void);

//Define Global Variables!
videos new_videos[Max_Value]; //No more than 8,192 videos!
int Video_Current=0; //Initialize current video to 0

int main(void)
{
      int menu_val;
      Show_Menu(); //Display Menu first thing!
      while ((menu_val = getche()) != '0') //Get first choice
      //Use getche because it doesn't require enter to be hit!
      {
            switch (menu_val)
            {
                  case '1':
                  find();
                  break;

                  case '2':
                  check_out();
                  break;
      
                  case '3':
                  return_videos();
                  break;
      
                  case '4':
                  find_videos();
                  break;

                  case '5':
                  add_videos();
                  break;

                  case '6':
                  print_inventory();
                  break;

                  case '7':
                  //Do nothing, just reprint menu
                  break;

                  default:
                  printf("Your selection did not match one of the menu options.\n Please enter a valid option. \n");
                  break;
            }
            //Show menu right before you loop again!
            //No reason to show it before you know what you are doing.
            Show_Menu();
      }
      return(0);
}

void find(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

void check_out(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

void return_videos(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

void find_videos(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

void add_videos(void)
{
      fflush(stdin);
      printf("\nEnter the title of the video?  ");
      gets(new_videos[Video_Current].title);
      printf("\nWhat genre is the video?  ");
      gets(new_videos[Video_Current].type);
      printf("\nEnter upto th5ree actors seperated by spaces:  ");
      gets(new_videos[Video_Current].actor);
      Video_Current++; // Video_Current = Video_Current + 1;
      //Move to next video...
}

void print_inventory(void)
{
      int i;
      //Only loops till hits last video inputed.
      printf("\n\n\n"); //Just to space it out a second!
      for(i = 0; i < Video_Current; i++) //Loop to last inputed video
      {
            printf("Title of movie: %s\n", new_videos[i].title);
            printf("Type of move: %s\n", new_videos[i].type);
            printf("Actors in movie: %s\n", new_videos[i].actor);
      }
      //Wait for key to be hit!
      getch();
}

void Show_Menu(void)
{
      printf( "SCHLOCKLUSTER VIDEO  -  Main Menu: \n\n");
      printf( "DESCRIPTION/FUNCTION:                        ENTER NUMBER: \n");
      printf( "Find video(s) (by title, type, or actors) -       1 \n");
      printf( "Check out video(s)                        -       2 \n");
      printf( "Return video(s)                           -       3 \n");
      printf( "Find all videos out to a customer         -       4 \n");
      printf( "Add new video(s)                          -       5 \n");
      printf( "Print inventory list (sorted by...)       -       6 \n");
      printf( "Reprint the menu options                  -       7 \n\n");
}
0
Ready4DisCommented:
If you need more help.. or have any questions, I try to get here as often as possible!
0
maplahAuthor Commented:
Bill,

Here is what I worked on today - it has just been some changes - I can get it to compile w/o errors but the menu prints and then throws and error values #7 - then prints the menu again...

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

// Was just erring on the side of caution have reduced the sizes.
#define MAX_TITLE 160      /*set maximum for titles */
#define MAX_TYPE 80      /*set maximum for types */
#define MAX_ACTOR 160      /*set maximum for actors */
#define MAX_VIDEOS 510      /*set maximum for videos */

typedef struct video
{
      char title [ MAX_TITLE ];
      char type  [ MAX_TYPE ];
      char actor [ MAX_ACTOR ];
}Video;

void find(void);
void check_out(void);
void return_videos(void);
void find_videos(void);
void add_videos( allVideo );
void print_inventory();
void menu_header(void);

struct video allVideo [ MAX_VIDEOS ];

int main(void)
{
      int menu_val; /* input from user for menu */
      
      while ((menu_val = getchar()) != 0)  
      {
            printf( "SCHLOCKLUSTER VIDEO  -  Main Menu: \n\n");
            printf( "DESCRIPTION/FUNCTION:                        ENTER NUMBER: \n");
            printf( "Find video(s) (by title, type, or actors) -       1 \n");
            printf( "Check out video(s)                        -       2 \n");
            printf( "Return video(s)                           -       3 \n");
            printf( "Find all videos out to a customer         -       4 \n");
            printf( "Add new video(s)                          -       5 \n");
            printf( "Print inventory list (sorted by...)       -       6 \n");
            printf( "Reprint the menu options                  -       7 \n\n");
            
            getchar();
            menu_val -= 48;
            switch (menu_val)
        {
        case 1:
            find();
            break;
                  
        case 2:
                  check_out();
                  break;
                  
        case 3:
                  return_videos();
                  break;
                  
        case 4:
            find_videos();
            break;
                  
        case 5:
                  add_videos(allVideo);
                  break;
                  
        case 6:
                  print_inventory();
                  break;
                  
            default:
                  printf("Your selection did not match one of the menu options.\n Please enter a valid option. \n");
                  break;
            }
            menu_header();
      }
      return(0);
      
}

/* This is the first function in the video store menu.
it is the menus Find video(s) (by title, type, or actors) option */
void find(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

/* This is the second function in the video store menu.
it is the Check out video(s) option */
void check_out(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

/* This is the third function in the video store menu.
it is the Return video(s) option */
void return_videos(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

/* This is the fourth function in the video store menu.
it is the Find all videos out to a customer option */
void find_videos(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

void add_videos(struct video *allVideo)
{
      int i;
      
      fflush(stdin);
      printf("\nEnter the title of the video?  ");
      gets(allVideo[i].title);
      
      for (i=0; i<255; i++) {
            strcpy("title", allVideo[i].title);
      }
      
      printf("\nWhat genre is the video?  ");
      gets(allVideo[i].type);
      
      for (i=0; i<255; i++) {
            strcpy("type", allVideo[i].type);
      }
      
      printf("\nEnter upto three actors seperated by spaces:  ");
      gets(allVideo[i].actor);
      
      for (i=0; i<255; i++) {
            strcpy("actor", allVideo[i].actor);
      }
      
}

/* This is the sixth function in the video store menu.
it is the Print inventory list (sorted by...) option */

void print_inventory(struct video *allVideo)
{
      char i;
      
      for(i = 0; i <= allVideo; i++)
      {
            printf("Title of movie: %s\n", allVideo[i].title);
            printf("Type of move: %s\n", allVideo[i].type);
            printf("Actors in movie: %s\n", allVideo[i].actor);
      }
}

/* This function is for the menu - it splats it back to the screen */
void menu_header(void)
{
      printf( "\nSCHLOCKLUSTER VIDEO  -  Main Menu: \n\n");
      printf( "DESCRIPTION/FUNCTION:                        ENTER NUMBER: \n");
      printf( "Find video(s) (by title, type, or actors) -       1 \n");
      printf( "Check out video(s)                        -       2 \n");
      printf( "Return video(s)                           -       3 \n");
      printf( "Find all videos out to a customer         -       4 \n");
      printf( "Add new video(s)                          -       5 \n");
      printf( "Print inventory list (sorted by...)       -       6 \n");
      printf( "Reprint the menu options                  -       7 \n\n");
}

Where am I going wrong?

Thanks,

maplah
0
maplahAuthor Commented:
Bill,

I have compiled the code - Using M$ Visual C++ - Win32 Release - I get the following errors:

error C2061: syntax error : identifier 'new_videos'
error C2059: syntax error : ';'
error C2059: syntax error : '['
error C2065: 'new_videos' : undeclared identifier
error C2109: subscript requires array or pointer type
error C2224: left of '.title' must have struct/union type
error C2198: 'gets' : too few actual parameters
error C2109: subscript requires array or pointer type
error C2224: left of '.type' must have struct/union type
error C2198: 'gets' : too few actual parameters
error C2109: subscript requires array or pointer type
error C2224: left of '.actor' must have struct/union type
error C2198: 'gets' : too few actual parameters
error C2109: subscript requires array or pointer type
error C2224: left of '.title' must have struct/union type
error C2109: subscript requires array or pointer type
error C2224: left of '.type' must have struct/union type
error C2109: subscript requires array or pointer type
error C2224: left of '.actor' must have struct/union type

I have checked all of the options/settings and it appears to be set up for 32 bit operation.  I am clueless at this point...

Thanks,

maplah
0
Ready4DisCommented:
Where my code says,
struct videos
{
char title [ MAX_TITLE ];
char type  [ MAX_TYPE ];
char actor [ MAX_ACTOR ];
};

change it too...

typedef struct videos
{
char title [ MAX_TITLE ];
char type  [ MAX_TYPE ];
char actor [ MAX_ACTOR ];
};


and change...
videos new_videos[Max_Value]; //No more than 8,192 videos!

too...

struct videos new_videos[Max_Value]; //No more than 8,192 videos!

that.

I am using visual C++ version 6.0  My version supports typeing stuff the other way, I guess yours doesn't.  I have seen problems similar to this before.  I will repost the code how it SHOULD work :).  Here.
0
Ready4DisCommented:
//Try this!

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

#define MAX_TITLE 255
#define MAX_TYPE 255
#define MAX_ACTOR 255
#define Max_Value 8192

typedef struct videos
{
char title [ MAX_TITLE ];
char type  [ MAX_TYPE ];
char actor [ MAX_ACTOR ];
};


/*
new_videos (video list array) is global.
counter is global.
No need to pass anything to any functions now!
*/


//Declare all functions of program!
void find(void);
void check_out(void);
void return_videos(void);
void find_videos(void);
void add_videos(void);
void print_inventory(void);
void menu_header(void);


//Added this function to display menu!
void Show_Menu(void);

//Define Global Variables!
struct videos new_videos[Max_Value]; //No more than 8,192 videos!
int Video_Current=0; //Initialize current video to 0

int main(void)
{
int menu_val;
Show_Menu(); //Display Menu first thing!
while ((menu_val = getche()) != '0') //Get first choice
//Use getche because it doesn't require enter to be hit!
{
switch (menu_val)
{
case '1':
find();
break;

case '2':
check_out();
break;

case '3':
return_videos();
break;

case '4':
find_videos();
break;

case '5':
add_videos();
break;

case '6':
print_inventory();
break;

case '7':
//Do nothing, just reprint menu
break;

default:
printf("Your selection did not match one of the menu options.\n Please enter a valid option. \n");
break;
}
//Show menu right before you loop again!
//No reason to show it before you know what you are doing.
Show_Menu();
}
return(0);
}

void find(void)
{
printf("This option not available yet.  It is still in development process. \n");
}

void check_out(void)
{
printf("This option not available yet.  It is still in development process. \n");
}

void return_videos(void)
{
printf("This option not available yet.  It is still in development process. \n");
}

void find_videos(void)
{
printf("This option not available yet.  It is still in development process. \n");
}

void add_videos(void)
{
fflush(stdin);
printf("\nEnter the title of the video?  ");
gets(new_videos[Video_Current].title);
printf("\nWhat genre is the video?  ");
gets(new_videos[Video_Current].type);
printf("\nEnter upto th5ree actors seperated by spaces:  ");
gets(new_videos[Video_Current].actor);
Video_Current++; // Video_Current = Video_Current + 1;
//Move to next video...
}

void print_inventory(void)
{
int i;
//Only loops till hits last video inputed.
printf("\n\n\n"); //Just to space it out a second!
for(i = 0; i < Video_Current; i++) //Loop to last inputed video
{
printf("Title of movie: %s\n", new_videos[i].title);
printf("Type of move: %s\n", new_videos[i].type);
printf("Actors in movie: %s\n", new_videos[i].actor);
}
//Wait for key to be hit!
getch();
}

void Show_Menu(void)
{
printf( "SCHLOCKLUSTER VIDEO  -  Main Menu: \n\n");
printf( "DESCRIPTION/FUNCTION:                        ENTER NUMBER: \n");
printf( "Find video(s) (by title, type, or actors) -       1 \n");
printf( "Check out video(s)                        -       2 \n");
printf( "Return video(s)                           -       3 \n");
printf( "Find all videos out to a customer         -       4 \n");
printf( "Add new video(s)                          -       5 \n");
printf( "Print inventory list (sorted by...)       -       6 \n");
printf( "Reprint the menu options                  -       7 \n\n");
}
0
maplahAuthor Commented:
Bill,

It works!  

However, I have a question - when I hit CNTL Z to break out of the program - it just loops back into the menu again - I thought the break in the switch statement would kick the program out of the loop?

Thanks,

map
0
maplahAuthor Commented:
Bill,

Here's another bug - I have noticed:

When you enter the number from the menu - it prints it again then asks for the name of the movie

SCHLOCKLUSTER VIDEO                       -  Main Menu:

DESCRIPTION/FUNCTION:                        ENTER NUMBER (Do not hit enter):
Find video(s) (by title, type, or actors) -       1
Check out video(s)                        -       2
Return video(s)                           -       3
Find all videos out to a customer         -       4
Add new video(s)                          -       5
Print inventory list (sorted by...)       -       6
Reprint the menu options                  -       7

5
Enter the title of the video?  5

What is causing that?

Thanks,

map
0
maplahAuthor Commented:
Bill,

How hard would it be to get the video check outs, the return videos, and finding all videos checked out by a customer?

I would double the points to 400 if you could help me figure that out...

Thanks,

maplah
0
Ready4DisCommented:
Oh boy.  That bug that you pointed out!  I couldn't figure out why it wasn't clearing the buffer, and couldn't figure out why it was reading the character twice.  It's one of those problems, and I can't get it fixed!  As for Video Check Outs, Return Videos, and finding videos... it depends on how you want this information stored.  Do you want to store customers in a database, and have each movie barcoded... or just have the movies in a database, and when a person chooses one, it puts it to a file, or some type of numbering system.  Let me know how you want it done, and I will let you know how easily it can be done.

Ps.  I was thinking about just writing my own input function which would do away with the whole extra key thing, and would make searching a little simpler in the long run.  Other than that, I couldn't get it to work correctly!
0
maplahAuthor Commented:
Ready4Dis,

I fixed it by changing this:

int main(void)
{
int menu_val;
Show_Menu(); /*Display Menu first thing! - allow customer input */
while ((menu_val = getchar()) != '0')  
{
getchar();
switch (menu_val)
{
            
case '1':
find();
break;
                  
case '2':
check_out();
break;
                  
case '3':
return_videos();
break;
                  
case '4':
find_videos();
break;
                  
case '5':
add_videos();
break;
                  
case '6':
print_inventory();
break;
                  
case '7':
Show_Menu();
break;
                  
default:
printf("Your selection did not match one of the menu options.\n Please enter a valid option. \n");
break;
}
            
}


The features I would like to add are:

Check out video(s) - the user selects a/some video(s), inputs the customer who is checking
them out, and the video structs in the array are updated to indicate the new (check out) status of
the videos.

Return video(s) - as above, but the videos are now checked in

Find all videos out to a customer - print (to screen) a list of videos checked out to a given
customer

Is this doable?  If so, let me know and I will double the points to 400 for your trouble.

Thanks,

maplah
0
Ready4DisCommented:
I was just wondering how you wanted to store the customers though.  There should probably be a Main file for users and asign each one of them a bar-code number.  When you get there number, it should return their name and info and put save what they are taking out/returning into their number, not as their name.  This is how they ussually work, and it also simplifies having to search through characters.  Have a standard/set length of numbers.  Say, 15.  You shouldn't go over this amount of combos.  It should also work as a random number thing.  So every new person is given a random number that hasn't been used, or just give the numbers out in order.  Let me know if this way sounds good, or if you have a better way of doing it.
0
maplahAuthor Commented:
Bill,

That sounds good - I just wanted to be able to enter:

Jones, Fred
Matrix, Foxfire

for the check out and then be able to enter Jones, Fred Matrix, Foxfire to check them back in, be able to enter Jones, Fred and find out what movies he has checked out that's what I had in mind.  I am not too sure how to go about it.  Your method souds good enough to me.  Frankly, I don't really have a clue to go about it.  I could not get past writting the info to an array and then being able to bring just one customers info out of that array. I can see that your logic of assigning a customer an account number/bar code identificaton could be used as a trigger to find that customer in the array and makes a lot of sense.  Yeah, it makes a lot of sense - would or could that be used to search for movies too?

Thanks,

maplah
0
Ready4DisCommented:
It could.  You could assign each movie a barcode and put it into an file.  If the movie is in, it is marked as in, if not, it is marked out.  If someone asks for a movie by title, you can search by title and then return the barcode to check if it is in.  It is really kinda simple, and I can help you with it.  Just write back, and I will try to get you started on writing some of the functions. Ok.
0
maplahAuthor Commented:
Yes, please help.

Your idea sounds really good to me.

maplah
0
Ready4DisCommented:
Sorry, been really busy lately.  I will get started on it with you very soon.  First thing, what kind of file input/output do you want to use?  cin/cout, fscanf/fprintf, fwrite/fread... it's up to you.  Or even putc/getc.  Either way would work fine, but if you are going to be using structures I would have to recommend either cin/cout or fwrite/fread.  Either one can do structures pretty easily.  Even fscanf/fprintf would work fine.  Oh well, talk to you soon.

First lets make a customer structure...
Let me know what else you may need.

Notice all Character arrays/strings are one over the amount.  A computer knows that a string terminates because the last letter is a NULL or character code 0.  So, to get that last char in you need to allocate enough memory!

typedef struct Customer
{
   unsigned long ID;
   char First_Name[11]; //No more than 10 letters!
   char Last_Name[11];  //Save First/Last names seperate.  Helps for searching by name.
   char Address[41]; //Shouldn't be more than 40 characters.
   char City[16]; //15 chars...
   char State[3]; //Initials... if need state!
   int Zip_Code; //Zip code!
   char Phone_Number[11]; // 10 = Area code + phone #
   char Credit_Number[13]; //How many digits on a credit card :)
   char Expiration_Month;
   char Expiration_Day;
   unsigned int Expiration_Year; // Large enough for 20?? to 654??
//Just incase your program is around in the 655th century, after that it's going to have problems... :)
   char Many_Out; //No more than 127 at a time :)
   unsigned long Videos_Out[128]; //ID of up to 127 videos
//Just store the video ID, don't have to store all the info more than once!
};

Next lets make a video structure.

typedef struct Video
{
   unsigned long ID;
   char Title[31]; //No more than first 30 chars of title :)
   char Type[6]; //Enough for PG-13
   char Many_In;
   Char Total;
};

Let me know if you can think of anything else to add.. Maybe a description in the video.  I put a Many_In variable so you can save it once, and it will register how many you have and how many are in.  It could be useful.  Oh well, talk to you soon.  I will try to check as often as possible, but I gotto school full time, and work full time, + girlfriend, plus I do computer setups and tutoring on the side.  So, my days are ussually busy enough :)

Bill
0
Ready4DisCommented:
After this, we will have to start inputing videos and create a large database full of them for lookup.  If you want, it can put each video in order, and automatically jump to the next number, and if you pull out an old number, you can just replace it when you put a new one in.  That way when you are looking for a certain number, you can jump to it very quickly if they are in order.  You don't have to search!  Wouldn't that be simple :)  Oh well, talk to you soon.
0
maplahAuthor Commented:
Bill,

You have gone above and beyond,  I was thinking first and last name, Id number and videos they are checking out.  That would do it just fine as far as cin/cout or fwrite/fread goes either way is fine.

I am not really picky about it - it just needs to work.

and do this:

Check out video(s) - the user selects a/some video(s), inputs the customer who is checking
them out, and the video structs in the array are updated to indicate the new (check out) status of
the videos.

Return video(s) - as above, but the videos are now checked in

Find all videos out to a customer - print (to screen) a list of videos checked out to a given            customer

That's all.

Thanks,

maplah
0
Ready4DisCommented:
//Returns 0 on success, 1 if invalid video, 2 if invalid customer.

char Check_Out_Video(unsigned int C_ID, unsigned int V_ID)
//Customer ID & Video ID
{
   FILE *vid, *cus
   int ctr;
   Video V;
   Customer C;

   vid = fopen("videos.dat","rb");
   while (!feof(vid))
   {
      fread(V,1,sizeof(Video),vid);
      if (V.ID == V_ID) //If found then exit!
         break;
   }
   fclose(vid);
   if (V.ID != V_ID) //If not found
      return 1;

   cus = fopen("cus.dat","rb");
   while (!feof(cus))
   {
      fread(C,1,sizeof(Customer),vid);
      if (C.ID == C_ID) //If found then exit!
         break;
   }
   fclose(cus);
   if (V.ID != V_ID) //If not found
      return 2;
   //Else it will process the rest.


   //Functions not made yet...
   Video_Rented(V_ID); //Video rented, to subtract how many are in!
   Customer_Movie(V_ID); //Put that movie ID into the customer file!
}

//May have to add a return date on that in the customer struct!



New Customer Structure


typedef struct Customer
{
   unsigned long ID;
   char First_Name[11]; //No more than 10 letters!
   char Last_Name[11];  //Save First/Last names seperate.  Helps for searching by name.
   char Address[41]; //Shouldn't be more than 40 characters.
   char City[16]; //15 chars...
   char State[3]; //Initials... if need state!
   int Zip_Code; //Zip code!
   char Phone_Number[11]; // 10 = Area code + phone #
   char Credit_Number[13]; //How many digits on a credit card :)
   char Expiration_Month;
   char Expiration_Day;
   unsigned int Expiration_Year; // Large enough for 20?? to 654??
//Just incase your program is around in the 655th century, after that it's going to have problems... :)
   char Many_Out; //No more than 127 at a time :)
   unsigned long Videos_Out[128]; //ID of up to 127 videos
//Just store the video ID, don't have to store all the info more than once!
   //To check dates!
   char Month[128];
   char Day[128];  
   unsigned int Year[128];
};
0
maplahAuthor Commented:
Bill,

You are like a C machine - dude - I don't need that much info - just a customer name - id # and videos they are checking out - then to be able to check them back in and then find all videos checked out - 3 seperate functions.  

You don't need to make them that complicated.

Thanks,

maplah
0
maplahAuthor Commented:
Bill,

I was thinking something along the lines of:

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

#define MAX_TITLE 80
#define MAX_TYPE 80
#define MAX_ACTOR 160
#define Max_Value 510
#define CUST_NAME 80
#define CUST_VIDEO 80
#define Max_Cust 510

typedef struct videos
{
      char title [ MAX_TITLE ];
      char type  [ MAX_TYPE ];
      char actor [ MAX_ACTOR ];
};

typedef struct checkout
{
      char name [ CUST_NAME ];
      char video  [ CUST_VIDEO ];
};

/*Declare all functions of program! */
void find(void);
void check_out(void);
void return_videos(void);
void find_videos(void);
void add_videos(void);
void print_inventory(void);
void menu_header(void);


/*This function displays the menu! */
void Show_Menu(void);

/*Define Global Variables! */
struct videos new_videos[Max_Value];
struct checkout new_checkout[Max_Cust];

int Video_Current=0; /*Initialize current video to 0 */
int Video_Checkout=0; /*Initialize checkout video to 0 */

int main(void)
{
      int menu_val;
      Show_Menu(); /*Display Menu first thing! - allow customer input */
      while ((menu_val = getchar()) != '0')  
            /* Was advised to use getche because it doesn't require enter to be hit! */
      {
            getchar();
            switch (menu_val)
            {
            
            case '1':
                  find();
                  break;
                  
            case '2':
                  check_out();
                  break;
                  
            case '3':
                  return_videos();
                  break;
                  
            case '4':
                  find_videos();
                  break;
                  
            case '5':
                  add_videos();
                  break;
                  
            case '6':
                  print_inventory();
                  break;
                  
            case '7':
                  Show_Menu();
                  break;
                  
            case '8':
                  exit(1);
                  break;
                  
            default:
                  printf("Your selection did not match one of the menu options.\n I do not under stand %d \nPlease enter a valid option. \n", menu_val);
                  break;
            }
            
      }
      
      return(0);
}
/* This option will allow you to find videos by title, type or actors */
void find(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

/* This option lets you check vidoes out to t a customer */
void check_out(void)
{
      fflush(stdin);
      printf("\nEnter customer name (Last name, First name):  ");
      gets(new_checkout[Video_Checkout].name);
      printf("\nEnter video name: ");
      gets(new_checkout[Video_Checkout].video);
      Video_Checkout++; /* Video_Checkout = Video_Checkout + 1; */
      /* Move on to next customer checkout... */
      Show_Menu(); /*Display Menu to allow for customer input */
}

/* This option allows you to check the videos back in */
void return_videos(void)
{
      int i;
      char cust_name;
      /* Only loops till hits last video inputed. */
      printf("\n\n"); /* Just to add some space between entries */
      
      printf("\nEnter customer ID Number: ");
      gets(cust_name);
      
            for(i = 0; i < Video_Checkout; i++) /* Loop until last video that was entered */
      {
            if (cust_name = new_checkout[Video_Checkout].id)'
                  printf("Title of movie: %s\n", new_checkout[i].name);
                  printf("Type of move: %s\n", new_checkout[i].video);
            else
                  printf("The customer ID Number entered was not found!/n Please try again.");
      }
      Show_Menu(); /*Display Menu to allow for customer input */
}

/* This option allows you to search for a video by entering a fragment of a title or actor
      name, and see a list of videos whose title or actor list contain the fragment */
void find_videos(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}

/* This option allows you to add videos to the array */
void add_videos(void)
{
      fflush(stdin);
      printf("\nEnter the title of the video?  ");
      gets(new_videos[Video_Current].title);
      printf("\nWhat genre is the video?  ");
      gets(new_videos[Video_Current].type);
      printf("\nEnter upto three actors seperated by spaces:  ");
      gets(new_videos[Video_Current].actor);
      Video_Current++; /* Video_Current = Video_Current + 1; */
      /* Move on to next video... */
      Show_Menu(); /*Display Menu to allow for customer input */
}

/* This option allows you to print all of the titles in the inventory */
void print_inventory(void)
{
      int i;
      /* Only loops till hits last video inputed. */
      printf("\n\n\n"); /* Just to space it out a second! */
      for(i = 0; i < Video_Current; i++) /* Loop until last video that was entered */
      {
            printf("Title of movie: %s\n", new_videos[i].title);
            printf("Type of move: %s\n", new_videos[i].type);
            printf("Actors in movie: %s\n\n", new_videos[i].actor);
      }
      Show_Menu(); /*Display Menu to allow for customer input */
}

void Show_Menu(void)
{
      printf( "\nSCHLOCKLUSTER VIDEO                       -  MAIN MENU: \n\n");
      printf( "DESCRIPTION/FUNCTION:                        ENTER NUMBER (Then hit enter): \n");
      printf( "Find video(s) (by title, type, or actors) -       1 \n");
      printf( "Check out video(s)                        -       2 \n");
      printf( "Return video(s)                           -       3 \n");
      printf( "Find all videos out to a customer         -       4 \n");
      printf( "Add new video(s)                          -       5 \n");
      printf( "Print inventory list (sorted by...)       -       6 \n");
      printf( "Print Main Menu again                     -       7 \n");
      printf( "To Exit Program Enter 8                   -       8 \n\n");       
}


I am having a little trouble getting it to compile.

Thanks,

maplah
0
Ready4DisCommented:
Heh, trust me... to do those simple features, you have to have a huge structure.  I have been so busy lately, I just don't have a lot of time to help you.  I am trying, but school + work have been getting the better of me... plus working on my car lately.  Well, I will try to get back to you with some simple code as soon as possible.  Sorry it is taking so long!  :)
0
maplahAuthor Commented:
great ...

Thanks - believe me I understand completely...

maplah
0
maplahAuthor Commented:
Bill,

I got the checkin - check out and print videos checked out to work - now I need help with File I/O - I need to read/write the data to a file (either text or binary) and add search (function #1)

For this part, give the user the choice of selecting on title or actor. If the user chooses to select on title, ask the user for the   title or piece of a title to search for. For example, if the user enters "wind" as a title fragment, you would list videos with
titles such as:

Gone With the Wind
The Black, Winding Road
Swindled by Circumstance

Similarly, if the user chooses to select on actor, allow them to enter a part of an actor’s name and return all matches for that part. Make the search case-insensitive.

I will bump the points up to 600 if you can help me get this working...

maplah

The function that writes to the struct array:

typedef struct videos_s
{
      char title [ MAX_TITLE ];
      char type  [ MAX_TYPE ];
      char actor [ MAX_ACTOR ];
} video_t;

/* This option allows you to add videos to the array */
void add_videos(void)
{
      fflush(stdin);
      printf("\nEnter the title of the video?  ");
      gets(new_videos[Video_Current].title);
      printf("\nWhat genre is the video?  ");
      gets(new_videos[Video_Current].type);
      printf("\nEnter upto three actors seperated by spaces:  ");
      gets(new_videos[Video_Current].actor);
      Video_Current++; /* Video_Current = Video_Current + 1; */
      /* Move on to next video... */
      Show_Menu(); /* Display menu to allow for customer input */
}

This is the function for the find (search) function:

/* This option will allow you to search for a video by entering a fragment of a video or title,
type or actors */
void find(void)
{
      printf("This option not available yet.  It is still in development process. \n");
}
0
maplahAuthor Commented:
Thanks!

Bill
0
Ready4DisCommented:
//Some simple file manipulation...

#include <stdio.h>

typedef struct Video
{
   unsigned long ID;
   char Title[31]; //No more than first 30 chars of title :)
   char Type[6]; //Enough for PG-13
   char Many_In;
   Char Total;
};

unsigned long Next_ID(void)
{
  FILE *in;
  unsigned long ID=0;
  Video Temp;
  in = fopen("video.dat","rb");
  if (in==NULL) //No file!
  {
    return 0;
    //To save, add one to last found... so it starts at 1!
    //That way later 0 can be used as not found :)
  }
  //rb = Read Binary
  while (!(feof(in)) //While not end of file
  {
    fread(Temp,1,sizeof(Video),in);
    ID = Temp.ID;  //Sets ID to the last ID
  }
  return ID;
}

void Save_Video(Video vid)
{
  FILE *out;
  out = fopen("video.dat","ab");
  //ab = append binary
  fwrite(vid,1,sizeof(Video),out);
  fclose(out); //close file.
}

char Find_Video_ID(unsigned long ID, struct Video &Temp)
{
  Video Temp;
  FILE *in;
  in = fopen("video.dat","rb");
  //rb = Read Binary
  while (!(feof(in) && Temp.ID!=ID)
  {
    fread(Temp,1,sizeof(Video),in);
    ID = Temp.ID;  //Sets ID to the last ID
  }
  if (feof(in))  //If file ends before found, then not found!
    return 0;
  else
    return 1; //Else found!
}
0
maplahAuthor Commented:
Bill,

Thanks!

I appreciate it...

Have a good one...

maplah
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.