weirdness

This is a discussion on weirdness within the C Programming forums, part of the General Programming Boards category; When I use acouple scanf()s or structure arrays and fill them with information then try to view that information it ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    2

    Question weirdness

    When I use acouple scanf()s or structure arrays and fill them with information then try to view that information it gets all messed up. Sometimes over written or new things get added and i can't figure out why??? Can someone please help? An example is this program I just started writing. Im only half done with it but in just testing it im confused why this happens.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <conio.h>
    #include <string.h>
    
    void view(int file);
    void add(int file);
    void del(int file);
    void edit(int file);
    
    struct employees
    {
           char fname[10];
           char lname[10];
           char dob[10];
           char city[2];
    } user[10];
    
    
    int main()
    {
        int choice, file;
        char temp[5], done;
    
    do {    
     printf("\t\tView=1\tAdd=2\tDel=3\tEdit4\tExit=5\n\n:");
     choice=atoi(gets(temp));
     
     if(choice==5)
     exit(0);
     
     printf("File#:");
     file=atoi(gets(temp));
     printf("\n");
     
     if(choice==1)
     view(file);
     else if(choice==2)
     add(file);
     else if(choice==3)
     del(file);
     else if(choice==4)
     edit(file);
     else {
     printf("Error!");
     exit(0); }  
     
     printf("Are you finished? (y or n):");
     done=getche(); 
     printf("\n");
    }
    while(toupper(done)=='N');  
        return 0;
    }
    
    void view(int file)
    {
     printf("%s %s\n%s\n%s\n", user[file].fname, user[file].lname, user[file].dob, user[file].city);   
     printf("\n");        
    }
    
    void add(int file)
    {
      printf("First Name:");
      gets(user[file].fname);   
      printf("Last Name:");
      gets(user[file].lname);
      printf("DOB:");
      gets(user[file].dob);
      printf("City:");
      gets(user[file].city);     
      printf("User Entered!\n"); 
    }
    
    void del(int file)
    { 
         strcpy(user[file].fname, "          ");
         strcpy(user[file].lname, "          ");
         strcpy(user[file].dob, "          ");
         strcpy(user[file].city, "  ");
         
         printf("File deleted!\n");     
    }
    
    void edit(int file)
    { }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Well it seems to me that your arrays are rather small, and the problems are compounded by using the worlds most dangerous function (gets)
    It's mentioned in the FAQ.

    Example - char city[2]; can only store 1 character, the other valid position in this array is for a \0 to terminate the string.
    Anything else you type in is bad news.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    2
    awhh! i cant believe i forgot about the null byte at the end. ill give that a shot thank u so much and ill check out that FAQ thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass-by-value weirdness
    By sillyfofilly in forum C Programming
    Replies: 8
    Last Post: 02-16-2009, 08:08 AM
  2. rand() weirdness
    By Mostly Harmless in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2006, 11:41 PM
  3. open() file creation weirdness
    By itsme86 in forum C Programming
    Replies: 10
    Last Post: 09-27-2004, 05:22 PM
  4. allegro weirdness, <fstream> errors
    By ichijoji in forum Game Programming
    Replies: 2
    Last Post: 02-16-2003, 08:46 PM
  5. while loop weirdness (does not accept compound statements?)
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-28-2002, 11:21 PM

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