Help with Struct

This is a discussion on Help with Struct within the C++ Programming forums, part of the General Programming Boards category; Hi ppl... I need to make a program with struct that can add, modify, delete and search information in inside ...

  1. #1
    Registered User Nagash's Avatar
    Join Date
    Sep 2008
    Location
    Brazil - SC, Florianopolis
    Posts
    3

    Help with Struct

    Hi ppl... I need to make a program with struct that can add, modify, delete and search information in inside tha strut. My problem is keeping the information in a vector. Im posting the program half made... so dont mind the unused voids... I managed to show in screen at least one of the added information, but I cant get it to show the rest in void writeMovie. To make this I used Bloodshed DEV++
    And sorry for the bad english (Im Brazilian)... plz help

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    #include <ctype.h>
    #include <iostream>
    #include <fstream>
    
    typedef char string[51];
    
    typedef struct {
            string title, director, code, genre, year, available;
            }MOVIE;
    
    char inbuf;
    
    void addMovie (MOVIE list[500]);
    void readMovie (MOVIE list[500], int count);
    void writeMovie(MOVIE list[500], int count);
    void searchMovie(MOVIE list[500], int count);
    
    int counting(int count);
    
    //---------------------------------------------------------------
    int main(){
        MOVIE list[500];
        int count=0;
        
        readMovie(list, count);
        writeMovie(list, count);  
        
        getch();
    }
    //---------------------------------------------------------------
    void addMovie (MOVIE list[500]){
         
            fflush(stdin);
            puts("Movie Title:");
            gets((*list).title);
            puts("Movie's Director:");
            gets((*list).director);
            puts("Year of Making:");
            gets((*list).year);
            puts("Movie Code:");
            gets((*list).code);
            puts("Numerber of copies Available:");
            gets((*list).available);        
            puts("Genre:(R)Romance, (F)Fiction, (A)Action, (C)Comedy:");
            gets((*list).genre);
            
    }
    //---------------------------------------------------------------
    
    void readMovie(MOVIE list[500], int count){
         int i;
         char option;
         
         for(i=count; ; i++){
            system("CLS");         
            addMovie(&list[i]);
            counting(count);
            puts ("Do you wish to add another movie?(Y)/(N)");
            scanf("%c",&option);
            if (option != 'Y' && option != 'y')
               break;
         }
    }
    //---------------------------------------------------------------    
        
    int counting(int count){ 
        
        count=count+1;
        return count;
    }    
    //--------------------------------------------------------------- 
    
    void searchMovie(MOVIE list[500], int count){
       string search1, search2;
       int i;  
       
       puts("Type in Movie's Name");
       gets (search1);
       for (i=0; i=count; i++)
          if (search1 == list[i].title){
             puts("Type the Movie's Genre (R)Romance, (F)Fiction, (A)Action, (C)Comedy");
             scanf ("%c", search2);       
             if (search2 == list[i].genre){
                printf("%s\t%c", list[i].director, list[i].code);
             }    
          puts ("Movie does not exist in the list");                
       }     
    }     
    //---------------------------------------------------------------          
        
    void writeMovie(MOVIE list[500], int count){
         int i;
         puts("Complete Movie List:");
         for(i=0; i=count; i++);
            printf("%s\t%s\t%s\t%s\t%s\t%s\t", list[i].title, list[i].director,
            list[i].code, list[i].genre, list[i].year, list[i].available);
                  
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Nagash View Post
    Code:
    #include<stdio.h>
    #include<conio.h> // this is a non-standard header
    #include<string.h>
    #include<stdlib.h>
    #include <ctype.h>
    #include <iostream>
    #include <fstream>
    
    typedef char string[51];
    // This is a very dubious line... I recommend against using the name string, but it is
    // not technically illegal. Its more like... unethical.
    
    typedef struct {
            string title, director, code, genre, year, available;
            }MOVIE;
    
    char inbuf;
    
    void addMovie (MOVIE list[500]);
    void readMovie (MOVIE list[500], int count);
    void writeMovie(MOVIE list[500], int count);
    void searchMovie(MOVIE list[500], int count);
    
    int counting(int count);
    
    //---------------------------------------------------------------
    int main(){
        MOVIE list[500];
        int count=0;
        
        readMovie(list, count);
        writeMovie(list, count);  
        
        getch(); // getchar() is also available.
    }
    //---------------------------------------------------------------
    void addMovie (MOVIE list[500]){
         
            fflush(stdin);
            puts("Movie Title:");
            gets((*list).title);
            puts("Movie's Director:");
            gets((*list).director);
            puts("Year of Making:");
            gets((*list).year);
            puts("Movie Code:");
            gets((*list).code);
            puts("Numerber of copies Available:");
            gets((*list).available);        
            puts("Genre:(R)Romance, (F)Fiction, (A)Action, (C)Comedy:");
            gets((*list).genre);
            
    }
    //---------------------------------------------------------------
    
    void readMovie(MOVIE list[500], int count){
         int i;
         char option;
         
         for(i=count; ; i++){ // Is this what you meant? I don't think so. This loops is VERY dubious and can seg fault.
            system("CLS");         
            addMovie(&list[i]); // This line is going to buffer overflow 100&#37; of the time!!! Fix the above loop and this will be fine.
            counting(count);
            puts ("Do you wish to add another movie?(Y)/(N)");
            scanf("%c",&option); // fgetc() works too ya know... or getchar()
            if (option != 'Y' && option != 'y')
               break;
         }
    }
    //---------------------------------------------------------------    
        
    int counting(int count){ 
        
        count=count+1;
        return count;
    }    
    //--------------------------------------------------------------- 
    
    void searchMovie(MOVIE list[500], int count){
       string search1, search2;
       int i;  
       
       puts("Type in Movie's Name");
       gets (search1);
       for (i=0; i=count; i++)
          if (search1 == list[i].title){
             puts("Type the Movie's Genre (R)Romance, (F)Fiction, (A)Action, (C)Comedy");
             scanf ("%c", search2);       
             if (search2 == list[i].genre){
                printf("%s\t%c", list[i].director, list[i].code);
             }    
          puts ("Movie does not exist in the list");                
       }     
    }     
    //---------------------------------------------------------------          
        
    void writeMovie(MOVIE list[500], int count){
         int i;
         puts("Complete Movie List:");
         for(i=0; i=count; i++); // Again with your for loops... Plus look at the constraint... i = count??? I don't think you even meant to type it that way. i < count, perhaps.
            printf("%s\t%s\t%s\t%s\t%s\t%s\t", list[i].title, list[i].director,
            list[i].code, list[i].genre, list[i].year, list[i].available);
                  
    }
    Ok... So take a look at those issues first... You should really not use gets() so librally... or even at all for that matter.
    Last edited by master5001; 09-16-2008 at 01:23 PM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    In addition to all the points made by Master5001, I can tell you one reason it won't work. You're not passing the var "count" into the functions as a pointer, so when you increment it in your function counting(), you're only changing the value within the scope of that function. It has no effect in the rest of the program.
    Last edited by rmetcalf; 09-16-2008 at 02:03 PM.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Oh yeah... I also forgot to throw in the "This is a C board, post your question C++ on the board" point that initially even got me looking at his code.

    This is a C board, post your C++ question on the C++ board.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Oh yes, moved to C++ programming forum.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21