Like Tree1Likes
  • 1 Post By Adak

Help in returning to main

This is a discussion on Help in returning to main within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> void search (char[]); void calc(int ); void Musolla(); void GPCL(); void AcademicAffairs(); void ExamHall(); void OSCENT(); void DeanOffice(); ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    29

    Help in returning to main

    Code:
    #include<stdio.h>
    void search (char[]);
    void calc(int );
    void Musolla();
    void GPCL();
    void AcademicAffairs();
    void ExamHall();
    void OSCENT();
    void DeanOffice();
    int main()
    {
        char location[20];
        char ans;
        do
        {
          printf("Enter your location: ");
          gets(location);
        
          search(location);
        
          printf("\nDo you want to continue?(Type 'y' for yes, or 'n' for no)\n");
          printf("=>");
          scanf(" %c", &ans);
          fflush(stdin);
             
       } while(ans == 'y');
      
      system("pause");
      return 0;
    }
    
    void search (char location[])
    {
         char Places[6][20] = {"Academic Affairs", "GPCL", "Musolla", "Dean's Office", "Exam Hall", "OSCENT"};
          int i, colour1;
          
          for(i=0;i<10;)
             {
                  if(strcmp(location, Places[i]) == 0)
                  {
                       colour1= i;
                       break;
                  }
                              
                  else
                  i++;
                  if(i==10)
                  {
                       colour1 = -1;
                       printf("Invalid location: %s\n",location);
                  }
             }
           if(colour1 != -1)
             calc(colour1);
    }
    
    void calc(int colour1)
    {
         printf("\nChoose your destination\n1.Academic Affairs 2.GPCL 3.Musolla 4.Dean's Office 5.Exam Hall 6.OSCENT\n");
         printf("=>");
         int destin;
         scanf("%d",&destin);
         
         switch(destin)
         {
            case 1 : AcademicAffairs();
                             break;        
            case 2 : GPCL();
                             break;
            case 3 : Musolla();
                             break;
            case 4 : DeanOffice();
                             break;
            case 5 : ExamHall();
                             break;
            case 6 : OSCENT();
                             break;                   
         }
    }
    
    void Musolla()
    {
         char path,ans;
         printf("Choose your path- a.Shortest b.Covered c.Handicapped\n ");
         scanf("%s", &path);
         switch (path)
         {
           case 'a':
            printf("amir\n");break;
           case 'b':
            printf("amir2\n");break;
           case 'c':
            printf("amir3\n");break;
         }
          
    }
    
    void GPCL()
    {
         printf("ass\n");
    }     
    
    void AcademicAffairs()
    {
         printf("baik\n");
    }    
         
    void DeanOffice()
    {
         printf("giler\n");
    }     
         
    void ExamHall()
    {
         printf("air\n");
    }
    
    void OSCENT()
    {
         printf("ball\n");
    }
    the problm is when i entered the musolla function and chose on the option,the program wont return to main and ask the user wether they want to continue/not?
    when entering diff function,the program able to go back to main..
    can someone detect the problem with my coding??

  2. #2
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Not sure if this already solves your problem, but in line 85:

    Code:
    scanf("%s", &path);
    you're trying to read a string (= char array) into a single char variable. Format specifier should be "%c" here.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    i changed to %c,bt then i cant choose the path.it automatically exit the function and enter the main program
    i need the user to 1st choose the path then exit the function

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    hope someone can help me with this problem..any1??

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Change line 85 in your program, from this:
    Code:
    scanf("%c", &path);
    to this:
    Code:
    scanf(" %c", &path);
    That one extra space causes scanf() to ignore the trailing newline char from the last input (and fflush(stdin) doesn't usually work, btw - that is for OUTPUT streams, not INPUT streams).
    Last edited by Adak; 11-22-2012 at 10:31 AM.
    stahta01 likes this.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    im sorry,i tried bt it still didnt work..

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    You mean that yet you can not input?
    Can you please post the updated code ?

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    Code:
    #include<stdio.h>
    void search (char[]);
    void calc(int );
    void Musolla();
    void GPCL();
    void AcademicAffairs();
    void ExamHall();
    void OSCENT();
    void DeanOffice();
    int main()
    {
        char location[20];
        char ans;
        do
        {
          printf("Enter your location: ");
          gets(location);
        
          search(location);
        
          printf("\nDo you want to continue?(Type 'y' for yes, or 'n' for no)\n");
          printf("=>");
          scanf(" %c", &ans);
          fflush(stdin);
             
       } while(ans == 'y');
      
      system("pause");
      return 0;
    }
    
    void search (char location[])
    {
         char Places[6][20] = {"Academic Affairs", "GPCL", "Musolla", "Dean's Office", "Exam Hall", "OSCENT"};
          int i, colour1;
          
          for(i=0;i<10;)
             {
                  if(strcmp(location, Places[i]) == 0)
                  {
                       colour1= i;
                       break;
                  }
                              
                  else
                  i++;
                  if(i==10)
                  {
                       colour1 = -1;
                       printf("Invalid location: %s\n",location);
                  }
             }
           if(colour1 != -1)
             calc(colour1);
    }
    
    void calc(int colour1)
    {
         printf("\nChoose your destination\n1.Academic Affairs 2.GPCL 3.Musolla 4.Dean's Office 5.Exam Hall 6.OSCENT\n");
         printf("=>");
         int destin;
         scanf("%d",&destin);
         
         switch(destin)
         {
            case 1 : AcademicAffairs();
                             break;        
            case 2 : GPCL();
                             break;
            case 3 : Musolla();
                             break;
            case 4 : DeanOffice();
                             break;
            case 5 : ExamHall();
                             break;
            case 6 : OSCENT();
                             break;                   
         }
    }
    
    void Musolla()
    {
         char path,ans;
         printf("Choose your path- a.Shortest b.Covered c.Handicapped\n ");
         scanf("%c",&path);
         switch(path)
         {
           case'a':
            printf("amir\n");break;
           case'b':
            printf("amir2\n");break;
           case 'c':
            printf("amir3\n");break;
      }
    }
    
    void GPCL()
    {
         printf("ass\n");
    }     
    
    void AcademicAffairs()
    {
         printf("baik\n");
    }    
         
    void DeanOffice()
    {
         printf("giler\n");
    }     
         
    void ExamHall()
    {
         printf("air\n");
    }
    
    void OSCENT()
    {
         printf("ball\n");
    }
    it still cant return back to main to ask user wether to continue/not

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,494
    Please re-read this post!

    Tim S.

    Quote Originally Posted by Adak View Post
    Change line 85 in your program, from this:
    Code:
    scanf("%c", &path);
    to this:
    Code:
    scanf(" %c", &path);
    That one extra space causes scanf() to ignore the trailing newline char from the last input (and fflush(stdin) doesn't usually work, btw - that is for OUTPUT streams, not INPUT streams).
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    See post #5 again . You did not leave a space before %c .

    EDIT : Sorry Tim , I did not see your post

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    thx Adak and std10093..sorry,i ws so stressed out n cant see it clearly
    Last edited by amoeba532; 11-22-2012 at 11:30 AM. Reason: already solved the problem

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    You have to change from
    Code:
    scanf("%s",..);
    to
    Code:
    scanf(" %c",..);
    You have to leave a space before %c!!!!
    For more please refer to post #5

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    done.it work perfectly..thx for everythg

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're very welcome, Amoeba532!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning found from main
    By thames in forum C Programming
    Replies: 10
    Last Post: 11-16-2012, 03:34 PM
  2. Returning to main.
    By Deltahawk12 in forum C++ Programming
    Replies: 11
    Last Post: 09-10-2005, 09:11 PM
  3. Returning to main menu
    By Crash1322 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2003, 04:27 PM
  4. Returning arrays from function to main
    By musayume in forum C Programming
    Replies: 3
    Last Post: 05-12-2003, 04:33 AM
  5. Returning a Tree node back to void main()
    By gazza in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2002, 02:48 AM

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