printing array content - spot the error

This is a discussion on printing array content - spot the error within the C Programming forums, part of the General Programming Boards category; Hi all, I am new to programming and new to the board! the program should get student names and marks, ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    printing array content - spot the error

    Hi all, I am new to programming and new to the board!

    the program should get student names and marks, list them, search for a mark, delete an entry, help and exit.

    my problem is during the list function, I get some random extended ascii characters and numbers on the last line, and I cannot for the life of me work out where they came from. If you can spot the error, I will send you a crisp 5 monopoly banknote...

    i think the program is maybe printing the values of the arrays subscripts, but I am not sure, and if it is, I don't know why...

    Please load the prog with some data and you will see what I mean

    (ps i know the code is ugly, I havent been doing it for very long!)

    Code:
    //chris skelton
    //150109
    //menu driven program
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <conio.h>
    #include <string.h>
    
    //functions
         char menu();
         void add();
         void search();
         void exit(); 
         void processMenu();
         void list();
         void help(); 
         void delet();   
    //end functions
    
    //global vars
         char name[20][15];
         int col=0, index=0;
         int marks[20];
         char choice;
         char clearName[20][15];//blank array for delet() function
    //end global vars
         
         
    int main()
    {//begin main
    
         //populate array marks[] with dummy data 999
         for(index=0; index<=19; index++)
         {//begin for
              marks[index]=999;
              }//end for
         
         menu();
         
             
    }//end main
    
    
    
    char menu()
    {//begin menu
    
         system("cls");   
    
         printf("\n\n\t*****************************\n");
         printf("\t*                           *\n");
         printf("\t*        M E N U            *\n");
         printf("\t*===========================*\n");
         printf("\t*                           *\n");
         printf("\t*  Add Student.......A      *\n");
         printf("\t*  Search............S      *\n");
         printf("\t*  List All..........L      *\n");
         printf("\t*  Help..............H      *\n");
         printf("\t*  Exit..............E      *\n");
         printf("\t*  Delete............D      *\n");
         printf("\t*                           *\n");
         printf("\t*  Please enter a choice:   *\n");
         printf("\t*                           *\n");
         printf("\t*****************************\n\n\t");
         
         choice=getch();
         //scanf("%c", &choice);
         choice=toupper(choice);
         processMenu();
         
         //return choice;
              
    }//end menu
    
    //----------------------------------------------------------------------------//
    
    void processMenu()
    {//begin processMenu
    
         switch(choice)
              {//begin switch
              
              case 'A': add();
              break;
              
              case 'S': search();
              break;
              
              case 'E': exit();
              break;
              
              case 'L': list();
              break;
              
              case 'H': help();
              break;
              
              case 'D': delet();
              break;
              
              default: getchar();
                       printf("\n\n\tThat is not a valid choice, press enter to try again");
                       getchar();
                       menu();
                        
              }//end switch
    
    }//end processMenu
    
    
    //----------------------------------------------------------------------------//
    
    void add()
    {//begin add
    
         int flag=0;
         index=0;
         do
         {//begin do
         if(marks[index]<999)
         {//begin if
              index++;
              flag=1;       
              }//end if
                   else
                   {//begin else
                        
                        flag=0;
                        col=0;
                        printf("\n\n\tEnter student name: ");
                        scanf("%s", &name[index][col]);
                        printf("\n\n\tEnter student mark: ");
                        scanf("%d", &marks[index]);
              
              
                        }//end else
              
              }//end do
              while(flag==1);
              menu();
         
         
    
    }//end add
    
    
    //----------------------------------------------------------------------------//
    
    void search()
    {//begin search
    
         char temp[15];
         int found;
         
         printf("\n\n\tPlease enter the name you wish to search for: ");
         scanf("%s", &temp);
         
              for(index=0; index<19; index++)
              {//begin for
                   temp[0]=toupper(temp[0]);
                   if(strcmp(temp, name[index])==0)
                   {//begin if
                        found=1;
                        break;
                        }//end if
                             else
                             {//begin else
                                  found=0;
                                  }//end else
                   }//end for
                   
         
                   
         if(found==1)
         {//begin if
              printf("\n\n\tStudent\t\tMark");
              printf("\n\t-------\t\t----\n\t");
              
              for(col=0; col<14; col++)
              {//begin for
                   printf("%c", name[index][col]);
                   }//end for
                   printf("\t%d", marks[index]);
                   }//end if
                        else
                        {//begin else
                             printf("\n\n\tStudent not found!");
                             }//end else
                             
         getchar();
         printf("\n\n\tHit enter to return to main menu.");
         getchar();
         menu();
        
    
    }//end search
    
    
    
    //----------------------------------------------------------------------------//
    
    void exit()
    {//begin exit
    
         printf("\n\n\tExiting Program");
         printf("\n\n\tGoodbye");
         //getchar();
         printf("\n\n\tHit return to exit");
         getchar();
         
    
    }//end exit
    
    //----------------------------------------------------------------------------//
    
    void list()
    {//begin list
         index=0;
         col=0;
    
         
         printf("\n\n\tStudent\t\tMark");
         printf("\n\t-------\t\t----\n");
    
         
    
         while(index<=19)//outer loop
         {//begin while
              
              while(marks[index]==999)
                   {//begin while
                        index++;
                             if(index>19)
                                  {break;}
                        }//end while
                   
                   
                   printf("\n\t");
         
                   for(col=0; col<=14; col++)//inner loop
                   {//begin for
                        printf("%c", name[index][col]);
                        if(col==0)
                             {//begin if
                                  name[index][col]=toupper(name[index][col]);//makes the first letter of any name capital
                                  }//end if
                        }//end for
                        printf("\t%d", marks[index]);
                        
                   index++;
         }//end while
         
         
         //getchar();
         printf("\n\n\tHit enter to return to menu.");
         getchar();
         menu();
         
    
    }//end list
    
    //----------------------------------------------------------------------------//
    
    void help()
    {//begin help
    
         printf("\n\n\tWelcome to Menu help");
         printf("\n\n\tPlease press the letter on your keyboard");
         printf("\n\twhich corresponds to the menu choice");
         printf("\n\tyou would like");
         printf("\n\n\tFor example, press 'a' to add a student");
         //getchar();
         printf("\n\n\tPress enter to return to main menu");
         getchar();
         menu();
    
    }//end help
    
    
    //----------------------------------------------------------------------------//
    
    void delet()
    {//begin delet
    
         char temp[15];
         char answer;
         int found;
         
         printf("\n\n\tPlease enter the name you wish to delete: ");
         scanf("%s", &temp);
         
              for(index=0; index<19; index++)
              {//begin for               
                   temp[0]=toupper(temp[0]);
                   if(strcmp(temp, name[index])==0)
                   {//begin if
                        found=1;
                        break;
                        }//end if
                             else
                             {//begin else
                                  found=0;
                                  }//end else
                   }//end for
                   
         
                   
         if(found==1)
         {//begin if
              printf("\n\n\tStudent\t\tMark");
              printf("\n\t-------\t\t----\n\t");
              
              for(col=0; col<14; col++)
              {//begin for
                   printf("%c", name[index][col]);
                   }//end for
                   printf("\t%d", marks[index]);
                   printf("\n\n\tARE YOU SURE YOU WANT TO DELETE THIS RECORD? Y/N");
                   //scanf("%c", &answer);
                   answer=getch();
                   answer=toupper(answer);
                        if(answer=='Y')
                        {//begin if
                             for(col=0; col<15; col++)
                                  {//begin for
                                  name[index][col]=clearName[index][col];
                                  marks[index]=999;
                                  }//end if
                             }//end if
                                  else
                                  {//begin else
                                       printf("\n\n\tRecord not deleted");
                                       }//end else
                                  
                             
                   }//end if
                        else
                        {//begin else
                             printf("\n\n\tStudent record not found!");
                             }//end else
                             
         
         getchar();                    
         printf("\n\n\tHit enter to return to menu.");
         getchar();
         menu();
    
    }//end delet
    Last edited by nosnowking; 02-04-2009 at 04:44 PM. Reason: duh...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is this a competation? What do I get if I can spot it? How many are there?

    Seriously, before you can expect anyone to have a serious look at the code, could you perhaps indent your code to match the flow of the code, rather than like some random upside down stair-case?

    Also, why is "index", "choice" and a global variable.

    main should return 0.

    Why does "menu" have a return value, when it returns nothing?

    Why are you using recursion to make a loop from processMenu and the actual process functions back to menu? Normally, this is done by using a loop in for example the menu function.

    Why is the function to delete a student called "delet" with the last 'e' missing? One of Richie Kernighan's biggest regrets in the Unix operating system was that he called the function to create a file "creat" - but you don't have a 16-bit compiler that stores 3 characters in 8 bits, right? [Although I admit that I would probably prefer another name entirely, as C++ has reserved the name "delete" as a memory deallocation operator].

    Code:
    		name[index][col]=clearName[index][col];
    Why not just set it to zero? The above way is quite a complicated way to achieve that.

    Your function prototypes should have (void) not () to indicate that you are not us

    You are using the name "exit" for your own function - there is a standard function exit(int status) that it overlays. Not a good idea. Call it "exitProgram" or "quit" or some such.

    Code:
        printf("\n\n\tPlease enter the name you wish to search for: ");
        scanf("%s", &temp);
    That is incorrect - you should not use a & here since it's an array. Likewise in the delete function. Also, entering a name of more than 14 letters will overwrite some other data...

    Code:
    	    if(index>19)
    	    {break;}
    Yeuch! Either do not have braces, or put them on their own line:
    Code:
    	    if(index>19)
    	    {
    		break;
    	    }
    (Or better yet, incorporate your condition into the while-condition itself).

    Code:
    	for(col=0; col<=14; col++)//inner loop
    	{//begin for
    	    printf("%c", name[index][col]);
    	    if(col==0)
    	    {//begin if
    		name[index][col]=toupper(name[index][col]);//makes the first letter of any name capital
    	    }//end if
    	}//end for
    Why do you FIRST print the name, then change it to upper-case?

    Your list funciton doesn't do what you expect if there is NOTHING in the student list.

    Now I'm bored with finding more fautls, but I'm sure someone else can find some more.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Good job matsp. That's about $200 US worth of consulting for free.

    I'll review after the original poster makes the fixes you pointed out.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    please be gentle...

    I do appreciate the in depth look, although I did ask the answer to a specific problem...

    will make the changes you suggest and edit the post.

    Please go easy though, I've only been doing this a few weeks, I was previously a chef, and my college classmates think that 2+2=purple, so I'm having to learn on my own...

    thanks so far...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I did (sort of) answer your actual question: You do not deal well with the "empty" list case, but that also affects non-empty listing - when there's nothing else that needs listing, you still print one more item.

    Edit: And by the way, don't edit the original post. Post your new code - and if you want to point out particular important bits, use colours.

    And no, 2+2 can't possibly make purple, red or blue, yes. Maybe also green. Depends on how many bits per colour it is... [Unless it's a palletized colouring system, of course]

    --
    Mats
    Last edited by matsp; 02-04-2009 at 04:50 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    ok

    I see....

    cheers, will have to rethink my approach.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

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