help with saving strings

This is a discussion on help with saving strings within the C Programming forums, part of the General Programming Boards category; I am triing to write a programm, where i would save names of teaching classes and the number of available ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    help with saving strings

    I am triing to write a programm, where i would save names of teaching classes and the number of available hours. The program that i wrote does not want to save the names and i do not know why... could someboody please give me some tips what is wrong, plus i was thinking taht i would make the name_of_class into a *name_of_class but i do not know where to put char *name_of_class=(char*)malloc(20*sizeof(char)).

    Code:
    #include <stdio.h>#include <string.h>
    
    
    #define length_of_name 20
    #define number 20
    
    
    typedef struct{
           char name_of_class[length_of_name];
           int max_students;
           }nov_tip;
    nov_tip classroom[number];
    
    
    void show_classes(){
         system("COLOR 0F"); 
         printf("\t\t\ ALL CLASSES:\n\n");
         int i,empty;
         empty=0;
         for(i=number;i>=0;i--){
                  if (strcmp(classroom[i].name_of_class,"")==0) empty=i;               
                  }         
         for(i=0;i<=empty;i++){
                  printf("%d.: ",i+1);
                  puts(classroom[i].name_of_class);printf("\t"); 
                  printf("\t%d",classroom[i].max_students);               
                  } 
         system("PAUSE");               
         }
    
    
    void enter_class(){
         system("COLOR 0C"); 
         printf("\t\t\tENTER NEW CLASS: \n\n");
        
         
         int i,empty;
         empty=0;
    
    
         for(i=0; i<number; i++){
                 if (strcmp(classroom[i].name_of_class,"")!=0) printf("%d: %s\n",i+1,classroom[i].name_of_class);
                  }
         for(i=number;i>=0;i--){
                  if (strcmp(classroom[i].name_of_class,"")==0) empty=i;               
                  }         
                  
         printf("New class name: "); 
         gets(classroom[i].name_of_class);
         printf("People that can go to class: "); scanf("%d",&classroom[i].max_students);
         
         system("PAUSE");
         }
    
    
    void meni(){
           int select;
           do{ 
                system("CLS");   
                system("COLOR 0A"); 
                printf("\t\t ___MENU___\n\n");
                printf("1. Eneter new class\n");
                printf("2. Show all classes\n");
                scanf("%d",&select);
                switch (select){
                      case 1: system("CLS");
                              enter_class();
                              break;
                      case 2: system("CLS");
                              show_classes();
                              break;
                       }
                       
                fflush(stdin);
                
           }while (select!=0);
         }
    
    
    int main(){
          
        meni();
    
    
        system("PAUSE");
        }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    There a few problems with your code. First, your include files must be on their own lines:
    Code:
    #include <stdio.h>
    #include <string.h>
    Second: You should stop using the system() calls, they are considered bad practice and they are operating system specific. If you must use this function then you need to include the stdlib.h include file where this function is prototyped.

    Third: Never use gets() this function is very unsafe. Always use a function where you can specify the size of your C-string. There is fgets() of if your C-string does not contain spaces you can use scanf(), as long as you specify the length of the C-string scanf("%20s", YourCString);.

    Fourth: The fflush() function is not designed to work with input streams. The C standard only defines this function to work with output strings.

    Now to part of your problem: strcmp() works with the entire C-string not a single character. If you want to test for an empty C-string you should use strlen(), to test the length of the string.

    Jim

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, what Jim has already advised... Plus...

    Lines 20 - 22 and 44 - 46 ... this is not going to work as you expect. First you need strlen() == 0 not strcmp() == 0 for this. Second there is no guarantee the strings in your struct are initialized in any given state, unless you intialize them manually. A much more reliable practice is to kepp a "high water mark", a variable tracking the last entry you made.

    Line 20, 25, 42, 45, 50... stacking multiple functions on a single line is an extremely bad idea. It makes your code less readable and tends to hide the structure of your program making it harder to find and fix problems.


    The reason it's not saving classnames is most likely your loop on lines 44 - 46 starts one past the end of your array, memory you don't own, and is probably finding an empty string there so it is reading your class names into an invalid location. Valid array indexes begin at 0 and go to N-1 where N is the size of the array. When you fix my first suggestion you will likely fix that too...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file saving: trouble with strings and strncpy()
    By psychopath in forum C++ Programming
    Replies: 10
    Last Post: 09-17-2005, 10:26 PM
  2. Saving...
    By Cilius in forum C++ Programming
    Replies: 1
    Last Post: 05-05-2005, 11:28 PM
  3. Saving
    By ZachariahLee in forum C++ Programming
    Replies: 4
    Last Post: 02-06-2003, 07:05 AM
  4. Saving
    By PJYelton in forum Linux Programming
    Replies: 3
    Last Post: 12-03-2002, 06:22 PM
  5. saving strings to a table
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-14-2002, 10:47 AM

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