Thread: GCD LCM with menu

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Svitavy, Czech Republic
    Posts
    37

    GCD LCM with menu

    Hello, I have problems with creating program like this:
    Input five pairs of nunbers and input menu:
    MENU
    -----
    1. GCD
    2. LCM
    0. CLOSE
    Input option:
    1
    Input number of selected pair of numbers:
    1

    print result and print menu again.
    It suppose on input there isn't different number and everything should be realized by
    custom separated functions.

    I created this, but I don't know how to finish it, please could you help me?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    unsigned int nsd(unsigned int m, unsigned int n) {
    	if (n==0) return m;
    	else return nsd(n,m%n);
    }
    
    
    int lcm(int a,int b) {
    int n;
    for(n=1;;n++) {
    	if(n%a == 0 && n%b == 0)
    	return n;
    }
    }
    
    
    
    
    
    
    int menu() {
    	int i;
    	printf("%s\n%s\n%s\n%s\n%s\n%s\n", "MENU", "-----", "1. Nejvetsi spolecny delitel", 
    "2. Nejmensi spolecny nasobek", "0. KONEC", "Zadej operaci:");
    	i = getchar();
    	if (i==1) {
    		unsigned int vysledek_nsd, m, n;
    			printf("Zadej m a n");
    			m = getchar();
    			n = getchar();	
    		vysledek_nsd = nsd(m, n);
    		printf("%u", vysledek_nsd);
    	}
    	if (i==2) {
    		//nsn();
    	}
    	if (i==0) return 0;
    	if (i!=0 && i!=1 && i!=2) {
    		printf("%s\n", "Chybne zadani");
    		return -1;
    	}
    	return 0;
    }
    
    
    int main(void) {
    
    
    /*int i, j, pole[2][5];
    puts("Zadej dvojice cisel: ");
    for(int x=0; x<5; x++) {
    	scanf("%d%d", &i, &j);
    	for(int c = 0; c<2; c++) {
    		for(int k = 0; k<5; k++) {
    			pole[i][j] = c;
    		}
    	}
    }
    for(int c = 0; c<2; c++) {
    	for(int d = 0; d<5; d++) {
    		pole[c][c]++;
    		printf("Nejake cislo: %d a druhe cislo %d\n", i, j);
    	}
    }
    */
    int vysledek = menu();
    if(vysledek == -1) return -1;
    
    
    return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, maestrom!

    If you have a menu function, then EVERYTHING will branch out (or start to branch out), from the menu().

    menu() will have:

    a BIG do while loop, which is called just ONCE, from main(). All the other functions will be called from here, and returned here when they're done.

    Code:
    void menu(void) {
    
       int choice=0;
    
       do {
    
          print the choices for the user to choose from:
    
          1) GCD, 
    
          2) etc.
    
          3) etc.
    
          etc....
    
          5) Exit
    
    
          select (choice) {
    
             case 1:
                //code here for choice 1 - calling other functions.
    
                break;
             case 2:
                //code here for choice 2  -   ditto
                break;
             case 3: 
                //code here for choice 3  -     ditto 
                break;
             case 5:       //just an exit
                break;
             default:
                print Invalid choice, please re-enter.
          }     
    
       } while(choice != 5);
    }
    See if that makes sense to you.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Location
    Svitavy, Czech Republic
    Posts
    37
    But functions must be out from main, please help me more sophisticated, this solution looks good, but I am not so good in C yet.

    Thank you for your help, friend.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm going to add a few things to it, but really, it's up to you, at this point, to work on it.

    You can't learn to read without actually reading, and you can't learn to code in C, without a good deal of practice.

    Code:
    #include <stdio.h>
    
    void menu(void);
    
    int main(void) {
    
       menu();
       return 0;
    }
    
    void menu(void) {
     
       int choice=4,num1,num2,lcmultiple;
     
       do {
     
          print the choices for the user to choose from:
     
          1) GCD,
     
          2) LCM
     
          3) etc.
     
          etc....
     
          0) Exit
     
          printf("Enter your choice [1-3 or 0 to Exit]: ");
          scanf("%d",&choice);
    
          select (choice) {
             case 0:  
                printf("\n\nGoodbye\n\n");
                break; 
             case 1:
                printf("Enter the first number to find the LCM of:  ");
                scanf("%d", &num1);
                printf("And now enter the second number: ");
                scanf("%d", &num2);
                lcmultiple=lcm(num1,num2);
                printf("The LCM for %d and %d is %d\n",num1,num2,lcmultiple);
                break;
             case 2:
                //code here for choice 2  -   ditto
                break;
             case 3:
                //code here for choice 3  -     ditto
                break;
             default:
                print Invalid choice, please re-enter.
          }    
     
       } while(choice != 0);
    
    }
    That's a good start, for sure! Run through the C tutorial by clicking on the C Tutorial tab at the top of this forum - it has a LOT of really good info in there!

  5. #5
    Registered User
    Join Date
    Oct 2012
    Location
    Svitavy, Czech Republic
    Posts
    37
    Thank you, but my greatest problem is implementation of input and choose of those pairs of numbers, please could you write here, how could I do that I don't understand how to use with it in the functions and menu.

    Thank you very much.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by maestorm View Post
    Thank you, but my greatest problem is implementation of input and choose of those pairs of numbers, please could you write here, how could I do that I don't understand how to use with it in the functions and menu.

    Thank you very much.
    Input is included in the code I posted above, for two numbers. If you don't understand it, run through the C tutorial which is linked at the tab with that name, at the top of this forum.

    Now it's your turn to get to work - you need to practice, not have someone else code for you the rest of the way.

    Time to get busy, my friend!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu in C++
    By JOCAAN in forum C++ Programming
    Replies: 6
    Last Post: 02-14-2011, 07:30 PM
  2. c menu
    By sumdude in forum C Programming
    Replies: 11
    Last Post: 10-08-2008, 05:38 AM
  3. menu help
    By joe44 in forum C Programming
    Replies: 4
    Last Post: 05-05-2004, 10:04 AM
  4. Menu
    By Bluesun159 in forum Windows Programming
    Replies: 2
    Last Post: 01-22-2004, 07:14 PM
  5. How to modify a menu into a menu Folder(contains submenus) ??
    By L.O.K. in forum Windows Programming
    Replies: 3
    Last Post: 01-09-2003, 02:26 PM

Tags for this Thread