Connecting Tech Pros Worldwide Forums | Help | Site Map

Struct problem

Newbie
 
Join Date: Feb 2007
Posts: 3
#1: Feb 19 '07
Hi,

I have a record register where I manage records. My problem is that I dont know how to add records to my register. This is the struct I have created that works just fine, so does modify and view functions. I need a function for adding up tp 20 records and need to check that the user doesnt enter more than the maximum allowed amount of characters as you can see in the struct. At the end you can see my function that isnt correct.
Need help asap as I will turn this in to my university teacher.

Struct:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5. #define record_total 20 /* Max records */
  6.  
  7. void menu(void);
  8. void add(void);
  9. void modify(void);
  10. void view(void);
  11. void record_id(void);
  12.  
  13. int
  14.    menu_select, /* Saves menuchoices */
  15.    record_count = 0, /*Counts records */
  16.    record_select; /* Saves records*/
  17.  
  18. /* Struct for all records */
  19. typedef struct
  20. {
  21.    char title[51], artist[51], type[3];
  22.    double price;
  23.    int songs;
  24. }
  25. record_struct;
  26. record_struct record[record_total]; 
  27.  
  28. My try:
  29.  
  30. void add(void)
  31. {
  32.  
  33.    int input_integer;
  34.    char input_string[51];
  35.    double input_double;
  36.  
  37.    printf("Menyval: Titel\n");   
  38.  
  39.    if(record_count <= record_total)
  40.    {
  41.  
  42.      /*printf("Menyval: Titel\n\n");
  43.      gets(record[record_count].title);*/
  44.  
  45.      printf("\n Mata in titel: ", record[record_count].title);
  46.      gets(record[record_count].title);
  47.      if(strlen(input_string) > 0 && strlen(input_string) <= 50)
  48.      {
  49.       strcpy(record[record_count].title, input_string);
  50.      }   
  51.      printf("\n Mata in artist: ", record[record_count].type);
  52.      gets(input_string);
  53.      if(strlen(input_string) > 0 && strlen(input_string) <= 50)
  54.      {
  55.       strcpy(record[record_count].artist, input_string);
  56.      }   
  57.      printf("\n Mata in typ: ", record[record_count].type);
  58.      gets(input_string);
  59.      if(strlen(input_string) > 0 && strlen(input_string) <= 50)
  60.      {
  61.       strcpy(record[record_count].type, input_string);
  62.       }   
  63.       printf("\n Mata in antal låtar:", record[record_count].songs);
  64.       scanf("%d", &input_integer);
  65.       if(input_integer > 0)
  66.       {
  67.        record[record_count].songs = input_integer;
  68.       }   
  69.       printf("\n Mata in pris: ", record[record_count].price);
  70.       scanf("%lf", &input_double);
  71.       if(input_double > 0)
  72.       {
  73.          record[record_count].price = input_double;
  74.       }   
  75.       record_count++;
  76.    }   
  77.    return;

Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Feb 19 '07

re: Struct problem


Is this record register another struct? Or are you simply trying to get 20 or less record objects in your main program?
Newbie
 
Join Date: Feb 2007
Posts: 3
#3: Feb 20 '07

re: Struct problem


Quote:

Originally Posted by Ganon11

Is this record register another struct? Or are you simply trying to get 20 or less record objects in your main program?

I just want to add 20 entries to my main. The add function will ask the user to enter info about the records. View presents records and modify modifies a selected record. I simply need to add 20 entries to my struct.
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#4: Feb 20 '07

re: Struct problem


Well, with a cursory glance everything looks like it should be working. Can you explain, if possible, how it's not working? For example, is it getting some of the input, but not all? Is it skipping the input?
Newbie
 
Join Date: Feb 2007
Posts: 3
#5: Feb 21 '07

re: Struct problem


Quote:

Originally Posted by Ganon11

Well, with a cursory glance everything looks like it should be working. Can you explain, if possible, how it's not working? For example, is it getting some of the input, but not all? Is it skipping the input?

I am not supposed to use cursors for this assignment. Just need to add entries to the struct in main.

Problem is if I remove this line:
/*printf("Menyval: Titel\n\n");
gets(record[record_count].title);*/

the program skips and wants me to enter artist. If I uncheck it works but its not a correct solution. Also want add entries up to 20 and not overwrite previous ones.
Reply


Similar C / C++ bytes