Thread: C (Not ++) Console Application

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    7

    C (Not ++) Console Application

    I think i found an alternative to scanf/fscanf and that is scanf_s.
    So I have my inputs working, I dont know if they are saving for later usage however.

    [1]- I am currently stuck trying to calculate the base*exp for case 3, I cannot tie in my methods to make it work. I'd like some elaboration in this area of switches.

    [2]- I also would like to know how to 'clear' the previous menu, as the current code will keep repeating the menu when returned to main, which is messy; how can i clear/prevent the menu from repeating itself over and over again?



    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
    	
        int selection = 0;
        int base = 1;
        int exponent = 1;
    
        printf("\nPower Menu:\n");
        printf("  1. Change Base\n");
        printf("  2. Change Exponent\n");
        printf("  3. Display base raised to exponent\n");
        printf("  4. Exit Program\n"); 
        printf("Option:");
    
        scanf_s("%d", &selection, 4);
    
        switch(selection)
        {
            case 1:
                 printf("Enter Base :");
    	     printf("Base = %d\n", base, scanf_s("%d", &base, stdin)); 
                 return main(); 
            case 2:
                 printf("Enter Exponent :");
    	     printf("Exponent = %d\n", exponent, scanf_s("%d", &exponent, stdin)); 
                 return main();
            case 3:
                 printf("The Sum is\n");
                 return main();
            case 4:
                 printf("Exiting Program\n");
                 return 0;
            default:
                 printf("Invalid Choice\n");
    			 return main();
        }
        
        return main();
    }
    Last edited by MittWaffen; 10-09-2010 at 02:09 PM. Reason: Update

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    There is no standard C method for this. You're probably looking for ncurses.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well if you want to avoid using the scanf function you could try the getchar function? Like...

    Code:
    #include <stdio.h>
    
    int main()
    {
         int ch;
         
         printf("\n Menu:\n\n");
         printf(" 1. Base\n");
         printf(" 2. Exponent\n");
         printf(" 3. B Raised To Exp\n");
         printf(" 4. Exit \n");
         printf("\n Selection?\n\n");
         
         ch = getchar();
         
         switch( ch )
         {
            case 1: 
                 /* ... */
                 break;
         }
    Ofcourse you might have to include some error checking there!

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    switch statements, thank you! ssharish!

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Hmm...guess I misread you request. Sorry!

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MittWaffen View Post
    -Selectable via keyboard key (%d) press or if simple enough using arrow keys (If it isnt going to complicate my understanding)
    If your compiler supports it (ex. VC++) you can use the getch() function (#include <conio.h> for that).
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    I had issues with the getch() working it out on my own, and with textbook assistance so i started with switch statements.

    I have updated my code, and questions within the first post.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Try this...

    Code:
    #include<stdio.h>
    
    int main()
    {
        int selection;
    
        printf("Power Menu:\n");
        printf("1. Change base\n");
        printf("2. Change exponent\n");
        printf("3. Display base raised to exponent\n");
        printf("4. Exit Program\n"); 
    
        scanf("%d",&selection);
        
        switch(selection)
        {
            case 1:
                 printf("Enter Base\n");
                 break;
            case 2:
                 printf("Enter Exponent\n");
                 break;
            case 3:
                 printf("The sum is..\n");
                 break;
            case 4:
                 printf("Exiting Program");
                 return 0;
            default:
                 printf("You entered an invalid choice\n");
        }
        
        return 0;
    }
    Seems to be working fine for me

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    What complier are you using?
    I had it entered perfectly correct, and it still gives me default in the switch statement.
    This was on VS 2010 Ultimate

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    i use dev-c++. Did you try my code?

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    Corrected, I retyped it into notepad, then entered it on a clean source file. Weird..
    VS 2010 sucks so far, stupid issues that shouldn't even occur happen. Might have to download 2008 to be safe from here on out.

    Regardless, I'm not trying to tie getch() instead of scanf; then try to make calculations based on this.

    would this be the proper alternative...?
    I cannot make this method work.

    ---------
    int selection;
    to (stays the same)
    int selection;
    ----------
    scanf("%d",&selection)
    to
    selection=getch();
    ---------



    Code:
    #include<stdio.h>
    
    int main()
    {
        int selection;
    
        printf("Power Menu:\n");
        printf("1. Change base\n");
        printf("2. Change exponent\n");
        printf("3. Display base raised to exponent\n");
        printf("4. Exit Program\n"); 
        printf("Option?");
    
        scanf("%d",&selection);
        
        switch(selection)
        {
            case 1:
                 printf("Enter Base\n");
                 break;
            case 2:
                 printf("Enter Exponent\n");
                 break;
            case 3:
                 printf("The sum is..\n");
                 break;
            case 4:
                 printf("Exiting Program");
                 return 0;
            default:
                 printf("You entered an invalid choice\n");
        }
        
        return 0;
    }
    Last edited by MittWaffen; 10-08-2010 at 05:28 PM.

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >I have also tried (int selection either way the issue still occurs unless char is used.
    Thats would mean different when it comes to switch statement. the case '1': means really case 49: As it will converted to 49.

    This would eventually jump to the default part. Because the selection would contain the values between 1-4, but not 49.

    Can you try put a printf statement just after scanf to check what value does the selection varibale holds before executing switch statemnt. For the sake of debugging?

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    Hey ssharish, I have used a printf right after scanf to no avail, only after I enter a decimal value does it print together with the case printf that was selected.

    However I just made a new source file, and it seems to have corrected itself magically.

    Now I am trying to get a proper alternative to scanf working for the input method; I am trying the getch() method, however it seems to be causing issues. I will also attempt the getnum() method to see if it ties into my code better than the previous.

    But, i'm not entirely sure what I am doing with these currently.

  14. #14
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Never come across such a behavior before with the any code, being working after copying into a new file. Though it might be down to the file like .o. Should have deleted the object files and try recompiling them again. Would have been interesting to find the behavior.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    Bump, updated OP.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. console application...
    By k1ll3r in forum C++ Programming
    Replies: 4
    Last Post: 05-23-2006, 10:41 AM
  2. 'No symbols Loaded'. Simple console application
    By flipflop82 in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2004, 09:10 AM
  3. making a stealthy win32 console application?
    By killdragon in forum C++ Programming
    Replies: 3
    Last Post: 09-08-2004, 02:50 PM
  4. Move cursor in console application
    By alfa in forum C# Programming
    Replies: 2
    Last Post: 02-09-2003, 02:58 PM