Thread: Command Prompt keeps relaunching itself. Unable to stop it.

  1. #1
    Registered User Eehan's Avatar
    Join Date
    Dec 2009
    Posts
    1

    Command Prompt keeps relaunching itself. Unable to stop it.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    int foodsel;
    struct info
    {
           char name[35];
           char loc[99];
           float avgcostpermeal;
           int foodtype;       
           struct info *next;
    }*newnode, *prev, *tmp,*list;
    
    
    main() 
    {
           createlist();    
           menu();   
    }
    
    menu()
    {
          int choose;
          printf("\nHit 1 to Add a New Restaurant!");
          printf("\nHit 2 to View All Entries!");
          printf("\nHit 3 to Search!");
          printf("\nHit 4 to Exit!");
          printf("\n\n\n\nYour Selection:");
          scanf("%d",&choose);fflush(stdin);
          system("cls");
          do
          {
          switch(choose)
          {
              case 1 : {
                          insert();
                          break;
                       } 
              case 2 : {
                          viewall();
                          break;
                       }
              case 3 : {
                          find();
                          break;
                       } 
               case 4 :{
                          byebye();
                          break;
                       }               
              default: { 
                        
                        printf("\n\nInvalid input! Hit Any Key to Return to The Main Menu!"); 
    
                        getch();
                        system("cls");
                        break;
                        }          
          }
          }while(choose!=4);
                        
    }
    
    createlist()
    {
           	list=NULL;
    }
    
    
    insert()
    {
            char resname[35];
         char resloc[99];
         float resprice;
         int position;
    
          
         newnode=(struct info*)malloc (sizeof(struct info));
         typeoffood();
         printf("\nName of Restaurant:");fflush(stdin);
         scanf("%[^\n]",resname);
         system("cls");
         printf("\nLocation of Restaurant:");fflush(stdin);
         scanf("%[^\n]",resloc);
         system("cls");
         printf("\nAverage Cost per Meal At The Restaurant:");fflush(stdin);
         scanf("%f",&resprice);
         system("cls");     
                                        
     strcpy(newnode->name,resname);
         newnode->next=NULL;
     strcpy(newnode->loc,resloc);
         newnode->next=NULL;
    newnode->avgcostpermeal=resprice;
         newnode->next=NULL;
    newnode->foodtype=foodsel;
         newnode->next=NULL;
         
         
         if(list==NULL)
         list=newnode;
         else if(resprice < list->avgcostpermeal)
         {
              newnode->next=list;
              
              list=newnode;
              
         }
              
         else
         {
             
                  
                  
             
              tmp=list;position=0;
              while(tmp!=NULL && position==0)
              {
                  if(resprice >= tmp->avgcostpermeal)
                  {
                      prev=tmp;
                      tmp=tmp->next;
                  }
                  else
                  {
                      position=1;              
                  }
              } 
               newnode->next=prev->next;
               prev->next=newnode;
               position=0;
          
    }
    system("cls");
          menu();      
    }
      
    display()
    {
         system("cls");
         if (list == NULL)
         printf("\nSorry, No Data Entry Was Found\n\n");
         else
         {
             tmp=list;
             while(tmp!=NULL)
             {
                                   printf("%s\n",tmp->name);
                                   printf("%.2f\n",tmp->avgcostpermeal);
                                   printf("%s\n",tmp->loc);
                                   printf("%d\n\n",tmp->foodtype);
                                   tmp=tmp->next;
             }
             
         }
    }
    
    typeoffood()
    {
              int foodchoice;
              
              printf("\nVariants of Food Served:");
              printf("\nHit 1 for Malay Culture Food!");
              printf("\nHit 2 for Chinese Culture Food!");
              printf("\nHit 3 for Indian Culture Food!");
              printf("\nHit 4 for Western Culture Food!");
              printf("\nHit 5 for Japanese Culture Food!");
              printf("\nHit 6 for Italian Culture Food!");
              printf("\n\n\n\nYour Selection:");
              scanf("%d",&foodchoice);fflush(stdin);
              switch(foodchoice)
              {
              case 1 : {
                          printf("\nMalay Culture Food.");
                          printf("%d",foodchoice);
                          system("cls");
                          foodsel=1;
                          
                          break;
                          
                       } 
              case 2 : {
                          printf("\nChinese Culture Food.");
                          printf("%d",foodchoice);
                          system("cls");
                          foodsel=2;
                         
                          break;
                         
                       }
              case 3 : {
                          printf("\nIndian Culture Food.");
                          printf("%d",foodchoice);
                          system("cls");
                          foodsel=3;
                          
                          break;
                          
                       } 
               case 4 :{
                          printf("\nWestern Culture Food.");
                          printf("%d",foodchoice);
                          system("cls");
                          foodsel=4;
                         
                          break;
                            
                       }
               case 5 :{
                          printf("\nJapanese Culture Food.");
                          printf("%d",foodchoice);
                          system("cls");
                          foodsel=5;
                       
                          break;
                            
                       } 
               case 6 :{
                          printf("\nItalian Culture Food.");
                          printf("%d",foodchoice);
                          system("cls");
                          foodsel=6;
                           
                          break;
                       }                                            
                        
              } 
                             
    }
    find()
    {
          char name[35];
            printf("Restaurant Name?");
            gets(name);fflush(stdin);
            tmp=list;
            while(tmp!= NULL)
            {
                if(strcmp(name, tmp->name)== 0) 
                {
                     printf("\nThese Are The Restaurants Available In The Database:");
                     printf("\n\nName: %s",tmp->name);
                     printf("\nAddress: %s",tmp->loc);
                     printf("\nAverage Cost Per Meal: %.2f",tmp->avgcostpermeal);
                    
                     if(tmp->foodtype==1)
                     {
                         printf("\nMalay Culture Food\n");
                                          
                     }
                     else if(tmp->foodtype==2)
                     {
                         printf("\nChinese Culture Food\n");
                                           
                     }
                     else if(tmp->foodtype==3)
                    {
                         printf("\nIndian Culture Food\n");
                                           
                     }
                     else if(tmp->foodtype==4)
                    {
                         printf("\nWestern Culture Food\n");
                                           
                     }
                     else if(tmp->foodtype==5)
                    {
                         printf("\nJapanese Culture Food\n");
                                           
                     }
                     else 
                     {
                         printf("\nItalian Culture Food\n");
                                          
                     }
                      break;  
                                  
                }
                else
                {
                 printf("Invalid Input! Please Try Again.");
                }
                tmp=tmp->next;
            }
            getch();
          
    }
    
    viewall()
    {
    
        int select;
        printf("\nHit 1 to view by least cost per meal");
        printf("\nHit 2 to view by food type");
        scanf("%d",&select);
        if (select==1)
        {
             tmp=list;
             while(tmp != NULL)
             {
                 printf("\n restaurant name: %s \n",tmp->name);fflush(stdin);
                 printf("\n addresss: %s \n average cost per meal: %.2f ",tmp->loc,tmp->avgcostpermeal);
                 
                 if(tmp->foodtype==1)
                     {
                         printf("\nMalay Culture Food\n");
                                          
                     }
                     else if(tmp->foodtype==2)
                     {
                         printf("\nChinese Culture Food\n");
                                           
                     }
                     else if(tmp->foodtype==3)
                    {
                         printf("\nIndian Culture Food\n");
                                           
                     }
                     else if(tmp->foodtype==4)
                    {
                         printf("\nWestern Culture Food\n");
                                           
                     }
                     else if(tmp->foodtype==5)
                    {
                         printf("\nJapanese Culture Food\n");
                                           
                     }
                     else 
                     {
                         printf("\nItalian Culture Food\n");
                                          
                     }
                 tmp=tmp->next;         
             }            
        }
        else
        {
            select=0;
            printf("\nselect what type of food u want to display");
            printf("\nHit 1 for display Malay Culture Food");
            printf("\nHit 2 for display Chinese Culture Food");
            printf("\nHit 3 for display Indian Culture Food");
            printf("\nHit 4 for display Western Culture Food");
            printf("\nHit 1 for display Japanese Culture Food");
            printf("\nHit 1 for display Italian Culture Food");
            scanf("%d",&select);
            if (select==1)
            {
                 tmp=list;
                 while(tmp != NULL)
                 {if(tmp->foodtype==1)
                         {
                     printf("\n restaurant name: %s \n",tmp->name);fflush(stdin);
                     printf("\n addresss: %s \n average cost per meal: %.2f ",tmp->loc,tmp->avgcostpermeal);
                     
                     
                             printf("\nMalay Culture Food\n");
                         }
                         tmp=tmp->next;
                 }                              
            }
            else if (select==2)
            {
                 tmp=list;
                 while(tmp != NULL)
                 { if(tmp->foodtype==2)
                     {
                     printf("\n restaurant name: %s \n",tmp->name);fflush(stdin);
                     printf("\n addresss: %s \n average cost per meal: %.2f ",tmp->loc,tmp->avgcostpermeal);
           
                    
                         printf("\nChinese Culture Food\n");
                                           
                     }
                     tmp=tmp->next;
                 } 
           }
           else if (select==3)
            {
                 tmp=list;
                 while(tmp != NULL)
                 {if(tmp->foodtype==3)
                         {
                     printf("\n restaurant name: %s \n",tmp->name);fflush(stdin);
                     printf("\n addresss: %s \n average cost per meal: %.2f ",tmp->loc,tmp->avgcostpermeal);
                     
                     
                             printf("\nIndian Culture Food\n");
                         }
                         tmp=tmp->next;
                 }                              
            }
            else if (select==4)
            {
                 tmp=list;
                 while(tmp != NULL)
                 {if(tmp->foodtype==4)
                         {
                     printf("\n restaurant name: %s \n",tmp->name);fflush(stdin);
                     printf("\n addresss: %s \n average cost per meal: %.2f ",tmp->loc,tmp->avgcostpermeal);
                     
                     
                             printf("\nWestern Culture Food\n");
                         }
                         tmp=tmp->next; 
                 }                             
            }
            else if (select==5)
            {
                 tmp=list;
                 while(tmp != NULL)
                 {
                     if(tmp->foodtype==5)
                    {printf("\n restaurant name: %s \n",tmp->name);fflush(stdin);
                     printf("\n addresss: %s \n average cost per meal: %.2f ",tmp->loc,tmp->avgcostpermeal);
                     
                         printf("\nJapanese Culture Food\n");
                                           
                     }
                     tmp=tmp->next; 
                 }
            }
            else
            {
                 tmp=list;
                 while(tmp != NULL)
                 {
                     if(tmp->foodtype==5)
                         {printf("\n restaurant name: %s \n",tmp->name);fflush(stdin);
                     printf("\n addresss: %s \n average cost per meal: %.2f ",tmp->loc,tmp->avgcostpermeal);
                     
                     
                             printf("\nItalian Culture Food\n");
                         }
                         tmp=tmp->next;  
                         }                            
            }
                     
                         
                         
        }    
               
    }
    
    byebye()
    {
    exit(1);        
    }
    Here is my code. When i launch, command prompt [cmd.exe] keeps opening and closing non-stop. Can anybody tell me whats wrong?

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Do not use fflush(stdin)
    Code:
    scanf("%[^\n]s",resname);
    You are missing it at other places as well.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    I don't know about your command prompt opening and closing, but on an invalid entry in your main menu, you never ask for another entry, so keep using the same one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf %s or %d integer input space char stop question...
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 04-14-2009, 10:44 AM
  2. Error stop Http Listener
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-04-2008, 02:14 AM
  3. Unable to stop at the end of linked list
    By megablue in forum C Programming
    Replies: 14
    Last Post: 07-16-2004, 11:34 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. Telling other applications to stop
    By nickname_changed in forum Windows Programming
    Replies: 11
    Last Post: 09-25-2003, 12:47 AM