Like Tree1Likes
  • 1 Post By laserlight

C calculating program some help please!

This is a discussion on C calculating program some help please! within the C Programming forums, part of the General Programming Boards category; I have made a simple calculating program. And I want some help on this. Well, I just want to improve ...

  1. #1
    Registered User
    Join Date
    Nov 2013
    Location
    Silicon Valley, CA
    Posts
    7

    C calculating program some help please!

    I have made a simple calculating program. And I want some help on this. Well, I just want to improve this program.
    I want a password protection and the timer.
    And little advice could help.
    Thank you.

    Here's the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <conio.h>
    #include <string.h>
    
    
    void load_menu(void);
    void sum(void);
    void sum3(void);
    void product(void);
    void product3(void);
    void division(void);
    void difference(void);
    void difference3(void);
    void square_root(void);
    void percentage(void);
    void FileCopying(void);
    void CharacterCounting(void);
    void Guess(void);
    void rest(void);
    
    
    
    int main(int argc, char** argv)
    {
    int ch;
    
    
    
    printf("****WELCOME TO MY CALCULATING PROGRAM(includes some random stuff)!!****");
    printf("\n\nPress ENTER to continue.");
    
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;
    printf("\nLOADING.........");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;
    system("cls");
    load_menu();
    
    return 0;
    }
    
    void load_menu(void)
    {
    int choice;
    
    do
    {
    printf("\t*******Menu*******\n\n");
    printf("\t1. Sum\n");
    printf("\t2. Sum of Three Integers\n");
    printf("\t3. Product\n");
    printf("\t4. Product of Three Integers\n");
    printf("\t5. Division\n");
    printf("\t6. Difference\n");
    printf("\t7. Difference of Three Integers\n");
    printf("\t8. Square Root of the Number\n");
    printf("\t9. Percentage\n");
    printf("\t10. File Copying\n");
    printf("\t11. Character Counting\n");
    printf("\t12. Rest\n");
    printf("\t13. Exit(Bad choice)\n");
    printf("\t\n\nChoose one you want to try.\n");
    scanf_s("%d", &choice);
    
    switch (choice)
    {
    case 1: sum();
    break;
    case 2: sum3();
    break;
    case 3: product();
    break;
    case 4: product3();
    break;
    case 5: division();
    break;
    case 6: difference();
    break;
    case 7: difference3();
    break;
    case 8: square_root();
    break;
    case 9: percentage();
    break;
    case 10: FileCopying();
    break;
    case 11: CharacterCounting();
    break;
    case 12: rest();
    break;
    case 13: printf(":( WE hope you come back soon! Press ENTER to quit. \n");
    system("PAUSE");
    exit(0);
    
    default: printf("Invalid choice: Please enter the number that is IN the menu! \n");
    system("PAUSE");
    system("cls"); 
    break;
    }
    
    } while (choice != 13);
    
    }
    
    void sum(void)
    {
    int num1, num2;
    int ch;
    
    printf("Enter number 1: ");
    scanf_s("%d", &num1);
    printf("Enter number 2: ");
    scanf_s("%d", &num2);
    
    printf("\nThe sum of the numbers was: %d", num1 + num2);
    
    
    while ((ch = getchar()) != '\n' && ch != EOF);
    
    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;
    
    system("cls");
    return;
    }
    
    void sum3(void)
    {
    int num1, num2, num3;
    int ch;
    
    printf("Enter number 1:");
    scanf_s("%d", &num1);
    printf("Enter number 2:");
    scanf_s("%d", &num2);
    printf("Enter number 3:");
    scanf_s("%d", &num3);
    
    printf("\nThe sum of the numbers was: %d", num1 + num2 + num3);
    
    
    while ((ch = getchar()) != '\n' && ch != EOF);
    
    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;
    
    system("cls");
    return;
    
    }
    
    void product(void)
    {
    int num1, num2;
    int ch;
    
    printf("Enter number 1: ");
    scanf_s("%d", &num1);
    printf("Enter number 2: ");
    scanf_s("%d", &num2);
    
    printf("\nThe product of the numbers was: %d", num1 * num2);
    
    while ((ch = getchar()) != '\n' && ch != EOF);
    
    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;
    
    system("cls");
    return;
    }
    
    void product3(void)
    {
    int num1, num2, num3;
    int ch;
    
    printf("Enter number 1: ");
    scanf_s("%d", &num1);
    printf("Enter number 2: ");
    scanf_s("%d", &num2);
    printf("Enter number 3: ");
    scanf_s("%d", &num3);
    
    printf("\nThe product of the numbers was: %d", num1 * num2 * num3);
    
    while ((ch = getchar()) != '\n' && ch != EOF)
    
    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;
    
    system("cls");
    return;
    
    }
    
    void division(void)
    {
    float num1, num2; // fix this part 
    int ch;
    
    printf("Enter a number 1: ");
    scanf_s("%f", &num1);
    printf("Enter a number 2: ");
    scanf_s("%f", &num2);
    
    printf("\nThe quotient of the numbers was: %f", num1 / num2); 
    
    while ((ch = getchar()) != '\n' && ch != EOF)
    
    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;
    
    system("cls");
    return;
    
    }
    
    void difference(void)
    {
    int num1, num2;
    int ch;
    
    printf("Enter a number 1: ");
    scanf_s("%d", &num1);
    printf("Enter a number 2: ");
    scanf_s("%d", &num2);
    
    printf("\nThe difference of the numbers was: %d", num1 - num2);
    
    while ((ch = getchar()) != '\n' && ch != EOF);
    
    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;
    
    system("cls");
    return;
    }
    
    void difference3(void)
    {
    int num1, num2, num3;
    int ch;
    
    printf("Enter number 1: ");
    scanf_s("%d", &num1);
    printf("Enter number 2: ");
    scanf_s("%d", &num2);
    printf("Enter number 3: ");
    scanf_s("%d", &num3);
    
    printf("\nThe product of the numbers was: %d", num1 - num2 - num3);
    
    while ((ch = getchar()) != '\n' && ch != EOF)
    
    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;
    
    system("cls");
    return;
    
    }
    
    void square_root(void)
    {
    float num;
    int ch;
    
    printf("Enter a number that you want to take a square root: ");
    scanf_s("%f", &num);
    
    printf("\nThe square root of the number was: %f", sqrt(num));
    
    while ((ch = getchar()) != '\n' && ch != EOF)
    
    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;
    
    system("cls");
    return;
    
    
    }
    
    void percentage(void)
    {
    int inp;
    float c, ch;
    
    printf("Type the percentage:");
    scanf_s("%f", &ch);
    printf("\nThe number that will take the percentage: ");
    scanf_s("%f", &c);
    
    printf("\nThe percent of the number is: %f", ((0.01)*ch)*c);
    
    while ((inp = getchar()) != '\n' && inp != EOF)
    
    printf("\n\nPress ENTER to continue.");
    while ((inp = getchar()) != '\n' && inp != EOF)
    ;
    
    system("PAUSE");
    system("cls");
    return;
    
    }
    void FileCopying(void)
    {
    int c, ch;
    
    
    printf("Type the character you want to copy. \n");
    printf("On Windows, ctrl+z for EOF and press ENTER and it goes back. \n");
    
    c = getchar();
    while (c != EOF) {
    putchar(c);
    c = getchar();
    }
    
    while ((ch = getchar()) != '\n' && ch != EOF);
    
    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;
    
    system("PAUSE");
    system("cls");
    return; 
    }
    
    void CharacterCounting(void)
    {
    long nc;
    int ch;
    
    printf("This is the character counting part.\n");
    printf("Press CTRL + Z for EOF signal. After you're done typing some characters.\n");
    
    nc = 0;
    while (getchar() != EOF)
    ++nc;
    printf("%ld\n", nc);
    
    while ((ch = getchar()) != '\n' && ch != EOF);
    
    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;
    
    
    
    
    system("PAUSE");
    system("cls");
    return;
    
    }
    
    
    
    void rest(void)
    {
    int ch;
    
    printf("Sleepy sleepy... zZZzZzZz\n");
    printf("You now feel awake again!\n");
    
    
    while ((ch = getchar()) != '\n' && ch != EOF);
    
    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;
    
    system("cls");
    return;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Here are some suggestions to improve this program:
    • Indent the code properly.
    • Notice that in many places you repeat the same code. Write functions to reduce this repetition.
    • Check the return value of scanf_s.

    Incidentally, instead of:
    Code:
    printf("\t*******Menu*******\n\n");
    printf("\t1. Sum\n");
    printf("\t2. Sum of Three Integers\n");
    printf("\t3. Product\n");
    printf("\t4. Product of Three Integers\n");
    printf("\t5. Division\n");
    printf("\t6. Difference\n");
    printf("\t7. Difference of Three Integers\n");
    printf("\t8. Square Root of the Number\n");
    printf("\t9. Percentage\n");
    printf("\t10. File Copying\n");
    printf("\t11. Character Counting\n");
    printf("\t12. Rest\n");
    printf("\t13. Exit(Bad choice)\n");
    printf("\t\n\nChoose one you want to try.\n");
    You can make use of the automatic concatenation of adjacent string literals:
    Code:
    printf("\t*******Menu*******\n\n"
           "\t1. Sum\n"
           "\t2. Sum of Three Integers\n"
           "\t3. Product\n"
           "\t4. Product of Three Integers\n"
           "\t5. Division\n"
           "\t6. Difference\n"
           "\t7. Difference of Three Integers\n"
           "\t8. Square Root of the Number\n"
           "\t9. Percentage\n"
           "\t10. File Copying\n"
           "\t11. Character Counting\n"
           "\t12. Rest\n"
           "\t13. Exit(Bad choice)\n"
           "\t\n\nChoose one you want to try.\n");
    James Moon likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2013
    Location
    Silicon Valley, CA
    Posts
    7
    Thank you, that was helpful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A program calculating sequence
    By HolmesChang in forum C Programming
    Replies: 6
    Last Post: 09-09-2013, 12:30 AM
  2. Grades calculating program help please!
    By hotshotennis in forum C Programming
    Replies: 93
    Last Post: 04-24-2012, 12:08 PM
  3. C Program Help - Calculating Federal Tax
    By MetDude21 in forum C Programming
    Replies: 10
    Last Post: 02-18-2011, 11:51 AM
  4. Replies: 2
    Last Post: 11-10-2003, 08:12 PM
  5. Replies: 8
    Last Post: 02-23-2002, 08:50 PM

Tags for this Thread


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