here we go again please

This is a discussion on here we go again please within the C Programming forums, part of the General Programming Boards category; I have done everything that has been sugested (and ya i am a terible speller that is why spell check ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    18

    here we go again please

    I have done everything that has been sugested (and ya i am a terible speller that is why spell check was invented) I am not able to get my dec c ++ to do the extra configurations I am working on it I would sure like to see what you all are seeing and than learn to understand it,

    here is the latest problem my screen gose blank as soon as I input the hours per my if else statment (40 or 41+) i have gone through the debuger and it is not telling me anything so if someone could show me the output on the added compil factors for the dev c++ I might find a way to understand or if someone has a sugestion ... I DONT WANT THE ANSWER .. I want help.

    Code:
    #include <stdio.h>
    int main(void)
    
    {
        char name[30];
        int pay = 0;        /* selected from case group by alph corispondant # */
        int hours = 0;      /* user input with if elas statments */
        int hlhrs = 0;      /* user input in conjunction with the if eals statments */
        int ot = 0;         /* mathmatical calculation with in the if eals than statments */
        int othol = 0;	/* mathmatical calculation with in the if eals than statments */
        int check = 0;      /* total amount of pay for the week */
    
        printf("What is your Name?\n> ");
        scanf("%s", name);
        
        printf("Good Day to you %s.");
        printf("Please chose one of the following choses so we know the amount of your hourly pay.\n");
        printf("1 : $10.00 an hour.\n");
        printf("2 : $20.00 an hour.\n");
        printf("3 : $30.00 an hour.\n");
        printf("4 : $50.00 an hour.\n> ");
        scanf("%d", &pay);
        
        switch (pay) {
            case 1: pay = 10; break;
            case 2: pay = 20; break;
            case 3: pay = 30; break;
            case 4: pay = 50; break;
            default: pay = 10; break;
        }                           
    		/* Section 2.0 = input of hours worked */
        printf("So How many Hours did you work?\n> ");
        scanf("%d", &hours);
    
        if (hours <= 40) {
            check = pay * hours;
            delay (30)
            printf("Your pay this week will be $%d", check);
        }
        else if (hours > 40) {
            printf("How many Holiday and Sunday Hours did you have? IF any!\n> ");
            scanf("%d", &hlhrs);
    
            printf("How many Overtime hours did you work? If any!\n> ");
            scanf("%d", &ot);
        }
          
        check = ((hlhrs * 2) + (ot * 1.5) + (hours - (hlhrs + ot)));
        printf("Your pay this week will be $%d", check);
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    18

    a little more info

    I am looking back at some of the error code that was threded before and yes I know I have some unused ints .. do I need to do that before I can get it to work right and yes i know my math equation is not functioning corectly I will fix that later tonight finaly, again i am not asking ?s to have this done by others I want to get my dev configured corectly so it will tell me what is wrong.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    FAQ > How do I... (Level 1) > Flush the input buffer
    FAQ > How do I... (Level 1) > How do I get a number from the user (C)

    And try to avoid nonstandard stuff like delay added for no good reason.

    [edit]And, badly beaten by AD (damn these pokey fingers!):
    FAQ > How do I... (Level 1) > Stop my Windows Console from disappearing everytime I run my program?
    FAQ > How do I... (Level 1) > How do I get my program to wait for a keypress?

    [edit=?]Overall, perhaps the FAQ may be of interest.
    Last edited by Dave_Sinkula; 01-02-2006 at 08:37 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    the reason the screen goes blank is an age-old problem that nearly all newbes have -- you didn't put anything at the end of main() to prevent it from returning to the operating system. put getchar() at the end so that main() will wait for keystroke before exiting the program.

  5. #5

    Join Date
    Jan 2006
    Posts
    28
    I reccommend replacing all your
    Code:
    scanf
    s with
    Code:
    fget
    s.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    22
    So How many Hours did you work?
    > 41
    How many Holiday and Sunday Hours did you have? IF any!
    > 10
    How many Overtime hours did you work? If any!
    > 5
    Your pay this week will be $53
    joe@ubuntu:~$
    Seems to work here.

    One thing I noticed quickly is that you will output the line "Your pay this week will be $x" twice if the user has worked < 40 hrs.

    You should move this:
    Code:
    check = ((hlhrs * 2) + (ot * 1.5) + (hours - (hlhrs + ot)));
    printf("Your pay this week will be $%d", check);
    into here:

    Code:
    else if (hours > 40) {
                    printf("How many Holiday and Sunday Hours did you have? IF any!\n> ");
                    scanf("%d", &hlhrs);
                    printf("How many Overtime hours did you work? If any!\n> ");
                    scanf("%d", &ot);
    
                    check = ((hlhrs * 2) + (ot * 1.5) + (hours - (hlhrs + ot)));
                    printf("Your pay this week will be $%d", check);
    
            }
    The following scanf() call fails to do any kind of bounds checking
    Code:
      char name[30];
      ..
     scanf("%s", name);

    What happens when the user enters a name > 30 characters?

    It's also worth noting that you don't check the return values from your scanf().

    As stated already, you should probably look into using fgets() for reading input from stdin.
    Last edited by slcjoey; 01-02-2006 at 08:50 PM.

Popular pages Recent additions subscribe to a feed

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