Thread: Help exiting program

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    33

    Help exiting program

    Hey everyone! So, I have the following program. I've got it running perfectly. I realize it could be programmed cleaner or more concisely, but this is for a class and we're required to have some things in our program to ensure we're learning.

    In this program you choose between being full time or part time. The program then performs calculations based on the user input. It pumps out a chunk of text that reads like:

    Employee type
    (1) Part time
    (2) Full time
    Please make selection: -1
    Invalid choice, make a selection again: 2
    Enter the weekly sales:12750
    Commission rate is 9%
    Total commission on sales is $1147.50
    Net income is $1447.50.

    My one problem is after each of these outputs is generated, I want to ask the user if they want to type 1 to continue, or exit. If they want to continue, it will once again prompt them if they are full time or part time employees.

    I've done some research and I can't find a clear way to do it. I know I need to make a variable and perform a logical comparison test (==) to error check the user input against the number required to exit. I just can't seem to figure out where to put it.

    Thanks!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int main(void)
    {
    
    
    int employeeType, continueProgram;
    double weeklySales, totalCommission, netIncome;
    
    
    printf("Employee type \n (1)Part Time \n (2)Full Time \n");
    printf("Please make a selection. \n");
    scanf("%d", &employeeType);
            while(employeeType>2 || employeeType<1)
            {
                    printf("Invalid choice.  Please make a selection again. \n");
                    scanf("%d", &employeeType);
            }
            if(employeeType==1)
            {
                    printf("Enter the weekly sales. \n");
                    scanf("%lf",&weeklySales);
                    if(weeklySales<=10000)
                    {
                            printf("The commission rate is 7% \n");
                            totalCommission=(weeklySales*7)/100;
                            printf("Total commission on sales is $%.2f\n", totalCommission);
                            netIncome=200+(weeklySales*7)/100;
                            printf("Net income is $%.2f \n", netIncome);
                    }
                    if(weeklySales>10000 && weeklySales<=20000)
                    {
                            printf("The commission rate is 10% \n");
                            totalCommission=(weeklySales*10)/100;
                            printf("Total commission on sales $%.2f\n", totalCommission);
                            netIncome=200+(weeklySales*10)/100;
                            printf("Net income is $%.2f \n", netIncome);
                    }
            }
                    if(weeklySales>20000 && weeklySales<=30000)
                    {
                            printf("The commission rate is 15% \n");
                            totalCommission=(weeklySales*15)/100;
                            printf("Total commission on sales is $%.2f \n", totalCommission);
                            netIncome=200+(weeklySales*15)/100;
                            printf("Net income is $%.2f \n", netIncome);
                    }
                    if(weeklySales>30000 && weeklySales<=50000)
                    {
                            printf("The commission rate is 17% \n");
                            totalCommission=(weeklySales*17)/100;
                            printf("Total commission on sales is $%.2f \n", totalCommission);
                            netIncome=200+(weeklySales*17)/100;
                            printf("Net income is $%.2f \n", netIncome);
                    }
                    if(weeklySales>50000)
                    {
                            printf("The commission rate is 20% \n");
                            totalCommission=(weeklySales*20)/100;
                            printf("Total commission on sales is $%.2f \n", totalCommission);
                            netIncome=200+(weeklySales*20)/100;
                            printf("Net income is $%.2f \n", netIncome);
                    }
    else
            {
                    printf("Enter the weekly sales. \n");
                    scanf("%lf", &weeklySales);
                    if(weeklySales<=10000)
                    {
                            printf("The commission rate is 5% \n");
                            totalCommission=(weeklySales*5)/100;
                            printf("Total commission on sales is $%.2f \n", totalCommission);
                            netIncome=300+(weeklySales*5)/100;
                            printf("Net income is $%.2f \n", netIncome);
                    }
                    if(weeklySales>10000 && weeklySales<=20000)
                    {
                            printf("The commission is 9% \n");
                            totalCommission=(weeklySales*9)/100;
                            printf("Total commission on sales is $%.2f \n", totalCommission);
                            netIncome=300+(weeklySales*9)/100;
                            printf("Net income is $%.2f \n", netIncome);
                    }
                    if(weeklySales>20000 && weeklySales<=30000)
                    {
                            printf("The commission is 12% \n");
                            totalCommission=(weeklySales*12)/100;
                            printf("Total commission on sales is $%.2f \n", totalCommission);
                            netIncome=300+(weeklySales*12)/100;
                            printf("Net income is $%.2f \n", netIncome);
                    }
                    if(weeklySales>30000 && weeklySales<=50000)
                    {
                            printf("The commission is 15% \n");
                            totalCommission=(weeklySales*15)/100;
                            printf("Total commission on sales is $%.2f \n", totalCommission);
                            netIncome=300+(weeklySales*15)/100;
                            printf("Net income is $%.2f \n", netIncome);
                    }
                    if(weeklySales>50000)
                    {
                            printf("The commission rate is 20% \n");
                            totalCommission=(weeklySales*20)/100;
                            printf("Total commission on sales is $%.2f \n", totalCommission);
                            netIncome=300+(weeklySales*20)/100;
                            printf("Net income is $%.2f \n", netIncome);
                    }
            }
    
    
    
    
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You can put all that inside a while loop, like this:
    Code:
    int answer = 1;
    
    while (answer == 1)
    {
        /* Your code goes here */
    
        printf("Input 1 to continue and anything else to exit: ");
        if (scanf("%d", &answer) != 1) /* This if exists in case the user gives something other than a number */
            answer = 0;
    }
    By the way, are you sure your program is running perfectly? I'm asking because your indentation tells a different story...
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    33
    What is considered proper indentation? I'm using Putty, and it's automatically creating the indentation and suggesting bracket placement for me. Also, the formatting of my program could have been a little messed up copying it to this forum post. But yes, my program compiles, runs, and produces valid I/O. So basically, put the while loop above my first while loop...orrrrr?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Are you sure that closing bracket at line #41 should be there?

    Quote Originally Posted by Joshua Green View Post
    So basically, put the while loop above my first while loop...orrrrr?
    There's no other way I can think of, really. I mean besides the nearly identical "do...while" loop.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Joshua Green View Post
    Hey everyone! So, I have the following program. I've got it running perfectly. I realize it could be programmed cleaner or more concisely, but this is for a class and we're required to have some things in our program to ensure we're learning.
    Well, some hints on how this could be a lot cleaner and more concise without getting rid of what you are supposed to be learning:

    1. You've near-duplicated the same block of code ten times. That's a big red flag that there's probably a better way. Think of how you could condense them - what if, rather than replicate all your printing and calculations ten times, you used one more variable - which would take values of 7, 10, 15, etc. - and setting that variable was the only thing you did in your if statements.

    2. I don't know if you've covered functions, but this whole project screams for having some good separation into functions. For example, a function to return the commission rate given a sales figure and employee type would clean up main() a ton and improve readability.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Registered User
    Join Date
    Aug 2015
    Posts
    33
    Quote Originally Posted by Cat View Post
    Well, some hints on how this could be a lot cleaner and more concise without getting rid of what you are supposed to be learning:

    1. You've near-duplicated the same block of code ten times. That's a big red flag that there's probably a better way. Think of how you could condense them - what if, rather than replicate all your printing and calculations ten times, you used one more variable - which would take values of 7, 10, 15, etc. - and setting that variable was the only thing you did in your if statements.

    2. I don't know if you've covered functions, but this whole project screams for having some good separation into functions. For example, a function to return the commission rate given a sales figure and employee type would clean up main() a ton and improve readability.
    I'm totally aware of functions and how this project could benefit from them, however, we're limited by a set of criteria to meet on a grading rubric. Our subsequent programs will incorporate other elements such as functions, but for now, this is the format I have to work with. It feels silly having to work like this, believe me.

  7. #7
    Registered User
    Join Date
    Aug 2015
    Posts
    33
    Quote Originally Posted by GReaper View Post
    Are you sure that closing bracket at line #41 should be there?
    And thank you for that, I just found that error and corrected it. Believe it or not, my program still compiled and produced valid I/O even with that bracket.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Joshua Green
    And thank you for that, I just found that error and corrected it. Believe it or not, my program still compiled and produced valid I/O even with that bracket.
    It is not surprising since your braces match up in the end, but as you know merely compiling and having some valid I/O does not mean that your code is correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help exiting from a program.
    By Grimloc in forum C Programming
    Replies: 4
    Last Post: 04-09-2010, 12:21 AM
  2. Exiting the program.
    By Taka in forum C Programming
    Replies: 6
    Last Post: 10-14-2007, 09:24 AM
  3. exiting program
    By ypramesh in forum C Programming
    Replies: 2
    Last Post: 04-01-2006, 03:27 PM
  4. Exiting a program
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-14-2004, 08:51 PM
  5. exiting program
    By spike232 in forum C# Programming
    Replies: 4
    Last Post: 05-25-2002, 08:18 PM