help me to decode this code

This is a discussion on help me to decode this code within the C Programming forums, part of the General Programming Boards category; The code is: Code: #include <stdio.h> void connect(); void disconnect(); void print(); void query(); int countries[100][100]; int main () { ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    help me to decode this code

    The code is:

    Code:
    #include <stdio.h>
    
    void connect();
    void disconnect();
    void print();
    void query();
    
    int countries[100][100];
    
    int main ()
    {
      char choice;
    
      printf("***MAIN MENU***\n");
      printf("(C) Connect two countries\n");
      printf("(D) Disconnect two countries\n");
      printf("(P) Print all connections between countries\n");
      printf("(Q) Query all connecting countries\n");
      printf("(X) Exit the program\n");
      printf("---\n");
      printf("Please enter the first letter of your choice, then hit Return\n");
      scanf("%c", &choice);
    
    
      switch (choice){
      case 'C':
        connect();;
        break;
      case 'D':
        disconnect();
        break;
      case 'P':
        print();
        break;
      case 'Q':
        query();
        break;
      case 'X':
        return 0;
    
      default:
        fprintf(stderr, "%c is an invalid choice\n", choice);
        return 1;
      }
    
    return 0;
    }
    
    void connect(){
      int c1, c2;
      printf("Please enter the two numbers of the countries you wish to connect.\n");
      scanf("%d %d", &c1, &c2);
    
      countries[c1-1][c2-1] = 1;
      countries[c2-1][c1-1] = 1;
    
      main();
    }
    The problem is that after I enter C and go to the connect procedure. After the connect procedure finishes I want to go back to main again. The first time the program runs it allows me to enter a choice, but however after I enter the choice C and go to the connect procedure and want to return back to main it won't allow me to enter a choice!

    It just goes through as I didn't enter a choice. How can I fix this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should NEVER, EVER call "main" within your C code [1].

    You need to put a loop in your main function to repeat the steps of asking what the user wants to do and select the correct function.

    [1] Unless you are actually writing the C runtime part that runs before main() - but this is a very specialized thing that most C programmers never ever do in their entire life - in the 15-20 years I've worked in C, and bearing in mind that I have done quite a bit of "low level" & "OS level" programming, I have written code that calls main maybe 3-4 times. I'm only writing this to avoid one of the list pedants from pointing this out. If this makes no sense to you, just ignore it.

    --
    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
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    then when should the for loop ends??

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I didn't say "for loop", I said "loop", meaning the generic loop construct - this can be done with for, while, do - while or even goto. As to which you choose is up to you - I have my idea of what is best, but I'll leave it to you to decide what type of loop makes most sense [after you've posted something with a working loop, I will reveal what I prefer...].

    As to when the loop ends, how about when the user gives "x" as the answer?

    --
    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.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so I changed my main to this:

    Code:
    int main ()
    {
      char choice;
    
      while (choice != 'X'){
      printf("***MAIN MENU***\n");
      printf("(C) Connect two countries\n");
      printf("(D) Disconnect two countries\n");
      printf("(P) Print all connections between countries\n");
      printf("(Q) Query all connecting countries\n");
      printf("(X) Exit the program\n");
      printf("---\n");
      printf("Please enter the first letter of your choice, then hit Return\n");
    
      scanf("&#37;c", &choice);
    
      switch (choice){
      case 'C':
        connect();
        break;
      case 'D':
        disconnect();
        break;
      case 'P':
        print();
        break;
      case 'Q':
        query();
        break;
      case 'X':
        return 0;
    
      default:
        fprintf(stderr, "%c is an invalid choice\n", choice);
        return 1;
      }
    
      }
    
    return 0;
    }
    however it still doesn't work. After the method connect is finished. It print's out MAIN MENU and all that stuff however it doesn't prompts me to enter a choice? Why is this?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Exactly what does it do? And by the way, perhaps you'd want to indent your code properly when you add the while-loop?

    Edit: And I would priobably use "do- while(choice != 'X')", since that means that you don't have to initialize choice until after you've read in from the user.

    --
    Mats
    Last edited by matsp; 02-08-2008 at 08:08 AM.
    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.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I can't use any do statement because I haven't learnt this. I only have to use while loop... can you explain to me why it doesn't prompts for user input the second time it goes to the loop?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You got lucky it worked the first time -- how do you know that choice doesn't equal 'X' when you start?

    Of course it prompts for user input the second time through the loop. Do you mean it then says something like "

    is an invalid choice" right away?

    Edit to add: And of course, once you hit an invalid choice, you are done for, since there's a return statement in that switch case.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yes it print's out an invalid choice right away, after showing all the MAIN MENU and options crap. It says that my choice is empty, a blank space

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by -EquinoX- View Post
    yes it print's out an invalid choice right away, after showing all the MAIN MENU and options crap. It says that my choice is empty, a blank space
    Almost: it says that your choice is <carriage return> -- because you hit carriage return the last time through the menu. You need to scanf using "\n%c", so that whitespace that's left over will get ignored.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    thanks that works I didn't realize about the new line. Now is just how to set the while loop correctly without having to use the do statement

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yeah I fixed this problem already. By the way a simple question, what is the code in c that will allows me to print 01 say that if the input is only 1? but it prints 11 or 15 if the input is 11 or 15??

    I tried "%2d" but this doesn't work

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by -EquinoX- View Post
    yeah I fixed this problem already. By the way a simple question, what is the code in c that will allows me to print 01 say that if the input is only 1? but it prints 11 or 15 if the input is 11 or 15??

    I tried "%2d" but this doesn't work
    Well, it works, because %2d means "put this number in a field of size 2". If you want "put this number in a field of size 2 and put zeros in front of it", then you need to use that conversion specifier, which is %02d.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,438
    Quote Originally Posted by -EquinoX- View Post
    so I changed my main to this:
    Please, try to indent properly. I completely missed the while loop when scanning the code the first time and thought you might have misplaced a bracket that should have been at global level, but it turns out there was another bracket a little further down with a return, so I thought, what the heck? Is there one too many?
    So I looked at the code again and noticed a {, and then I noticed the while.

    Summary:
    Always indent every block. If you need assistance, check out http://cpwiki.sf.net/User:Elyisa/Indentation
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 04:39 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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