Thread: printing array content - spot the error

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 05:44 PM. Reason: duh...

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, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02: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