Thread: could someone help?

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    15

    could someone help?

    ok, my program is working, and infact im quite happy, the only reasons being, its MY program and it WORKS!!!! usually not two words used in the same scentance!!!!

    just kiddin!!

    Anyway, only one slight error,

    i cant get the exit it work, when i press 4 it comes up witht the question "do you want to exit y/n?" but regardless of what i press the program exits

    any ideas

    Code:
    #include <conio.h>
    #include <stdio.h>
    
    void main(void){
    
    	int Choice;
    
    	void LPT_Check(void);
    	void COM_Check(void);
    	void EXIT(void);
    	void RJ45_Check(void);
    
    	clrscr();
    
    	printf("\nJon Butterworths Port Testing Program");
    	printf("\n");
    	printf("\n");
    	printf("\nPlease make a selection...\n");
    	printf("\n");
    
    	printf("[1] LPT Check\n");
    	printf("[2] COM Check\n");
    	printf("[3] RJ45 Check\n");
    	printf("[4] Exit\n");
    	printf("\nSelection:");
    
    	if(scanf("%d",&Choice));
    
    	switch(Choice){
    
    		case 1:LPT_Check();
    			break;
    		case 2:COM_Check();
    			break;
    		case 3:RJ45_Check();
    			break;
    		case 4:EXIT();
    			break;
    		}
    }
    
    	void LPT_Check(void){
    
    		printf("\nLPT Port Work in progress........");
    		printf("\n");
    		printf("\nPress any key to continue.....");
    		getch();
    	}
    
    	void COM_Check(void){
    
    		printf("\nCom Port Work in progress.......");
    		printf("\n");
    		printf("\nPress any key to continue.....");
    		getch();
    
    	}
    
    	void RJ45_Check(void){
    
    		printf("\nRJ45 Port Work in progress.......");
    		printf("\n");
    		printf("\nPress any key to continue......");
    		getch();
    
    	}
    
    	void EXIT(void){
    
    		char yn;
    
    		clrscr();
    		printf("\ndo you want to exit y/n?");
    
    		scanf("%c",&yn);
    
    		switch(yn)
    		{
    			case 'y' :exit(0);
    			case 'n' :main();
    		}
    		getch();
    		}

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    you can call your main() function? just doesnt seem right to me.....

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) main always returns an integer. Void is wrong.
    2) Never call 'main' recursivly.
    3) There is a newline character left in the input stream from your scanf call.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    15
    i dont understand?

    if i cant call main(), then how can i tell it not to close if i press n and to close if i press y, nd to DO NOTHING when i press anyother button other than Y and N.

    Anyone know how i can do this?

    Jon

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    	char chInput;
    
    	//infinite loop
    	for ( ; ; ) { 
    		system("cls"); //clear the screen
    		printf("Press Q to exit and any other key to exit\n");
    		chInput = getchar();
    
    		if (chInput == 'Q' || chInput == 'q')
    			return 0;
    	}
    
    	return 0;
    }
    Use loops man

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    15
    i dont understand your code

    and where would it go, and what would it replace?

    Thanx

    Jon

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    his for loop will never end, so its always checking. he then asks the user if he wanta to quit, if he types "Q" or "q" his if will return 0. When a function calls return its ends. so if the use enters anything else the if isnt true so the return is never called and the loop starts over again.

    as for the where would it go question, try it!!!! if bang your head against the keyboard enought code will work.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    15
    ok, thats cool, i understand what it does and where it goes now,

    just a couple of questions though

    Code:
    for ( ; ; ) {
    it doesent seem to like that

    and

    Code:
    if (chInput == 'Q' || chInput == 'q')
    are those funny looking I things actually I or somehting else, im sure ive seen them on the keyboard but cant find it on an american keyboard layout!

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    are those funny looking I things actually I or somehting else, im sure ive seen them on the keyboard but cant find it on an american keyboard layout!
    its a '|' (shift \), it means or.

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    15
    still doesent work

    no idea why though

  11. #11
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Are you sure that you want to have void functions? I'd return the status of the operation or some sort of flag once you actually write these functions.

    Try this....

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int LPT_Check(void);
    int COM_Check(void);
    int RJ45_Check(void);
    
    int main(void)
    {
      const char MENU[] = "\nPlease make a selection...\n\n"
    	              "[1] LPT Check\n"
    	              "[2] COM Check\n"
    	              "[3] RJ45 Check\n"
    	              "[4] Exit\n"
    	              "\nSelection: ";
      char Choice;
      int flag;  /* use this to check operation
    	        of port access */
    	              
      printf("\nJon Butterworths Port Testing Program\n\n");                  	
    
      for(;;)
      {
        printf("%s", MENU);
        scanf("%c%*c", &Choice);
    		
        if(Choice == '4') break;
    		
        switch(Choice)
        {
           case '1': flag = LPT_Check();
                     /* test operation */
                     break;
           case '2': flag = COM_Check();
                     /* test operation */
                     break;
           case '3': flag = RJ45_Check();
                     /* test operation */
                     break;
        } 
      }
    	
      printf("\n\nFinished");
    
      return EXIT_SUCCESS;
    }	
    
    int LPT_Check(void)
    {
    	/* int test; */
    	printf("\nLPT Port Work in progress........");
    	printf("\n");
    	printf("\nPress any key to continue.....");
    	getch();
    	
    	return 1 /* test */;
    }
    
    int COM_Check(void)
    {
        /* int test */
    	printf("\nCom Port Work in progress.......");
    	printf("\n");
    	printf("\nPress any key to continue.....");
    	getch();
    	
    	return 1 /* test */;
    }
    
    int RJ45_Check(void)
    {
    	/* int test; */
    	printf("\nRJ45 Port Work in progress.......");
    	printf("\n");
    	printf("\nPress any key to continue......");
    	getch();
    	
    	return 1 /* test */;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    15
    thanx mate, thats just what i was trying to do

    thanx again

    Jno

Popular pages Recent additions subscribe to a feed