Thread: New to programming: home work

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    3

    Exclamation New to programming: home work

    I am trying to figure out how to add isdigit to my code to verify that the information entered is in numerical form. I have tried and tried but just cant get it.

    this is what i have so far:

    Code:
    #include <stdio.h>//header
    #include <ctype.h>//isdigit header
    main()
    {//start
    int iResponse = 0;//town selection
    float fCost;//calculation operator
    fCost = 0;
    printf ("\n\tThank you for using the Kudler tax calculation utility.\n");
    printf ("\n\tPlease select your location to recieve your taxable total.\n");
    Printf ("\n	1\tDel Mar\n");
    Printf ("	2\tEncinitas\n");
    Printf ("	3\tLa Jolla\n");
    printf("\n	Please select your location (1-3): ");
    scanf("%d", &iResponse);
    switch (iResponse)						
    {
    
    	case 1:
    		printf("\n	Welcome Del Mar Associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("\n%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");
    		printf("	Your total tax is $%.2f\n", fCost * .0725);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.0725));//in-line calculation
    		break;
    		getchar();
    		
    	case 2:
    		printf("\n	Welcome Encinitas associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");
    		printf("	Your total tax is $%.2f\n", fCost * .075);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.075));//in-line calculation
    		break;
    		getchar();
    
    	case 3:
    		printf("\n	Welcome La Jolla associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");		
    		printf("	Your total tax is $%.2f\n", fCost * .0775);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.0775));//in-line calculation
    		break;
    		getchar();
    		}// end calc						
    		getchar();//keeps application viewable on screen
    	}// end selection
    I dont want anyone to do it for me. I just cant figure out how to add the

    if \ else statements
    would i place the if (isdigit) before the case statements or within them?

    what about the else statements would i have just one? or one per case statement?

    if anyone can give me a direction to go i would be most aprecieative.
    thank you

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    scanf() will read in a number, and a number only; if there isn't a number in the input buffer, it will return failure, and you should clear the buffer and try again.
    Code:
    #include <stdio.h>
    
    int main(void) {
        int x, c;
    
        for(;;) {
            printf("Enter a number: ");
            if(scanf("%i", &x) == 1) {  /* scanf returns the number of items (%x) read successfully */
                break;  /* end the for(;;) loop */
            }
    
            puts("Invalid number, please try again");
            
            /* clear the input buffer */
            while((c = getchar()) != '\n' && c != EOF) {}
        }
    
        printf("You entered %i\n", x);
    
        return 0;
    }
    There are several FAQs on the subject, including:


    [edit] Your getchar()s after the break statments are useless, since the break causes the switch statement to end and the getchar()s will never be executed.

    You forgot a closing curly brace (}) to end main().

    Main should be declared as int main(): FAQ > What's the difference between... > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])

    if \ else statements
    would i place the if (isdigit) before the case statements or within them?

    what about the else statements would i have just one? or one per case statement?
    If you're going to be using isdigit(), you probably don't want scanf() statements. You might want to read a whole line of text from the keyboard (FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)) and convert it as described for the last option of FAQ > How do I... (Level 1) > How do I get a number from the user (C). else statements follow if statements, and there can only be one per else, so you'd have to put them there.
    [/edit]

    [edit=2]
    Code:
    getchar();//keeps application viewable on screen
    May I recommend yet another FAQ: FAQ > How do I... (Level 1) > Stop my Windows Console from disappearing everytime I run my program?

    Code:
    printf ("\n\tPlease select your location to recieve your taxable total.\n");
    Printf ("\n	1\tDel Mar\n");
    Printf ("	2\tEncinitas\n");
    Printf ("	3\tLa Jolla\n");
    printf("\n	Please select your location (1-3): ");
    C is a case-sensitive language. 'P' isn't the same as 'p'. You need to make those Printfs printfs for the code to link.
    [/edit]
    Last edited by dwks; 09-07-2006 at 09:25 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    Code:
    printf ("\n\tPlease select your location to recieve your taxable total.\n");
    Printf ("\n	1\tDel Mar\n");
    Printf ("	2\tEncinitas\n");
    Printf ("	3\tLa Jolla\n");
    printf("\n	Please select your location (1-3): ");
    for(;;){
    	if (scanf("%d", &iResponse)==0);
    		break;
    		}
    		        puts("Invalid number, please try again");
            
            /* clear the input buffer */
            while((iResponse = getchar()) != '\n' && iResponse != EOF) {}
    	
    switch (iResponse)
    //the code continues below this but pasted only a small portion for space reduction
    This is what i placed but no matter the entery i get the error message
    "invalid number...."

    how would i make the c prog get the error message only when non numeric characters are entered?
    would i use the else statement?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    Strike the last post, i reread what you posted and corrected my issues

    thank you so much

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by chaos_defined
    Strike the last post, i reread what you posted and corrected my issues

    thank you so much
    So you saw the misplaced semi-colon at the end of the if statement's first line?

    Those can be a real nightmare! Ditto while statements with them similarly misplaced.

    In general, you want to verify the user's input, immediately after you get it, and before you enter any switch statement which acts upon that input.

    Did you notice the large amount of repetition in your code in the switch statement?

    If you use some variables to represent the tax rate, you could remove a good deal of that repeated code. Just abstract it a bit - works wonders.

    Good luck with your program.

    Adak

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in accessing root home folder....
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 02-13-2008, 05:50 AM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. C++ software for home work
    By rgk44 in forum C++ Programming
    Replies: 12
    Last Post: 02-15-2006, 04:00 AM
  5. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM