What's new

Closed Simple Calculator using C Language

Status
Not open for further replies.

DarkResurrection

Eternal Poster
Established
Joined
May 28, 2016
Posts
819
Solutions
1
Reaction
142
Points
296
Good Day sa inyo ma'am/sir!

Baka pwede niyo po ako matulungan about dito sa pag gawa ng simple calculator po. Bali ito po yung codes na nilagay ko.
I am using Microsoft Visual Studio 2008 since po yun daw po ang gagamitin namin sa pag gawa ng Simple Calculator using C language po.

C:
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
double num1, num2, result;
char option;

do{
printf("\nWelcome to SimpCal!\n");
printf("What do you want to do?\n");
printf("Press 1 for Addition\n");
printf("Press 2 for Subtraction\n");
printf("Press 3 for Multiplication\n");
printf("Press 4 for Division\n");
scanf("%d", &n);
if(n>4)
{
    printf("\n!!Please press only:\n 1 for Addition!\n 2 for Subtraction!\n 3 for Multipliation! and:\n 4 for Division!!\n");
    goto loop;
}
printf("Enter your desired number: ");
scanf("%lf", &num1);
printf("Now, enter your second number: ");
scanf("%lf", &num2);
switch(n)
{
case 1:
    printf("\nThe sum of %lf and %lf is %lf.\n", num1, num2, result, result=num1+num2);
    break;
case 2: result=num1-num2;
    printf("\nThe difference of %lf and %lf is %lf.\n",num1,num2, result);
    break;
case 3: result=num1*num2;
    printf("\nThe product of %lf and %lf is %lf.\n", num1,num2, result);
    break;
case 4: result=num1/num2;
    printf("\nThe quotient of %lf and %lf is %lf. \n", num1,num2, result);
    break;
}
loop:printf("\nDo you want to try again? Y/N: \n\n");
option=getch();
}while(option=='y');
    getch();
    return 0;
}

Ito na po yung ipapatulong ko po sana, sabi po kasi sa amin nung ipinacheck namin, nag run naman po, ngayun nung mag e enter na po sa "Enter your desired number" nag enter po yung teacher namin ng puro letters, so nag error po bali ganito po yung ginawa nya, see picture po.
code.PNG


Ngayon po gusto ko po sana magpatulong kung anung tamang code po ba dapat ilalagay ko para kapag once na nag enter po sya ulit ng letters imbes na number, eh ang magapakita po is "This is not a valid number" po.
Sana may makatulong po. Huhuhuhu... Newbie lang po kasi ako sa C programming at yun po ang tina topic po namin ngayun semester po.

*note: refer na lang po kayu sa code na nilagay ko then paki revise na lang po kung may error man po or may maling format po. salamat po!
 

Attachments

Same lang din ng condition na to, palitan mo lang ng condition na kung valid number ba siya proceed else start again.
if(n>4) { printf("\n!!Please press only:\n 1 for Addition!\n 2 for Subtraction!\n 3 for Multipliation! and:\n 4 for Division!!\n"); goto loop; }

Ngayon ang tanong pano malalaman kung numbers ang input, well search mo tamang syntax, para ma enhance google skills mo.
 
Maganda yung idea mo (at ng teacher mo) na mag-validate ng input. In saying that, input validation is a huge topic. In your case, eto yung mga validation sections:

- Choosing an operation (addition, subtraction, division & multiplication) represented by an int.
- Continue prompt (Y/y/N/n)
- Input number 1
- Input number 2

Pero I think you are on the right track.

Anyway, eto ang sample code para sa: a) Operation validation, and; b) Continue prompt.
I-pattern mo nalang dyan yung validation for input numbers 1 & 2.

Mapapansin mo na I didn't use scanf, fgets or the like. They are deprecated, so best avoided. Try to compile it with a modern compiler like GCC.

C:
#include <stdio.h>
#include <stdlib.h>

int get_operation();
char do_continue();

int main(void) {
    int myoperation = get_operation();
    switch (myoperation) {
        case 1:
            printf("\nGoing to perform ADDITION...\n");
            break;
        case 2:
            printf("\nGoing to perform SUBTRACTION...\n");
            break;
        case 3:
            printf("\nGoing to perform MULTIPLICATION...\n");
            break;
        case 4:
            printf("\nGoing to perform DIVISION...\n");
            break;
    }

    switch (do_continue()) {
        case 'Y':
        case 'y':
            printf("\nOK, continuing...\n");
            break;
        case 'N':
        case 'n':
            printf("\nThank you for using SimpleCalc!\n\n");
            break;
    }
    return 0;
}

int get_operation() {
    size_t nbyte=1;
    int op=0;
    char *my_string = malloc(nbyte+1);
    puts(" ");
    while (op != 1 && op != 2 && op != 3 && op != 4) {
        printf("What do you want to do?\n");
        printf("--------------------------------------\n");
        printf("Press 1 for Addition\n");
        printf("Press 2 for Subtraction\n");
        printf("Press 3 for Multiplication\n");
        printf("Press 4 for Division\n");
        printf("Your choice: ");
        getline(&my_string, &nbyte, stdin);
        sscanf(my_string, "%d", &op);
        if (op != 1 && op != 2 && op != 3 && op != 4) {
            printf("\nINVALID INPUT. Please re-enter.\n");
        }
    }
    free(my_string);
    return op;
}

char do_continue() {
    size_t nbyte=1;
    char answer=' ';
    char *my_string = malloc(nbyte+1);
    puts(" ");
    while (answer != 'Y' && answer != 'y' && answer != 'N' && answer != 'n') {
        printf("Continue <y/n>: ");
        getline(&my_string, &nbyte, stdin);
        sscanf(my_string, "%s", &answer);
        if (answer != 'Y' && answer != 'y' && answer != 'N' && answer != 'n') {
            printf("\nINVALID INPUT. Please re-enter.\n");
        }
    }
   free(my_string);    
   return answer;
}
 
Last edited:
Maganda yung idea mo (at ng teacher mo) na mag-validate ng input. In saying that, input validation is a huge topic. In your case, eto yung mga validation sections:

- Choosing an operation (addition, subtraction, division & multiplication) represented by an int.
- Continue prompt (Y/y/N/n)
- Input number 1
- Input number 2

Pero I think you are on the right track.

Anyway, eto ang sample code para sa: a) Operation validation, and; b) Continue prompt.
I-pattern mo nalang dyan yung validation for input numbers 1 & 2.

Mapapansin mo na I didn't use scanf, fgets or the like. They are deprecated, so best avoided. Try to compile it with a modern compiler like GCC.

C:
#include <stdio.h>
#include <stdlib.h>

int get_operation();
char do_continue();

int main(void) {
    int myoperation = get_operation();
    switch (myoperation) {
        case 1:
            printf("\nGoing to perform ADDITION...\n");
            break;
        case 2:
            printf("\nGoing to perform SUBTRACTION...\n");
            break;
        case 3:
            printf("\nGoing to perform MULTIPLICATION...\n");
            break;
        case 4:
            printf("\nGoing to perform DIVISION...\n");
            break;
    }

    switch (do_continue()) {
        case 'Y':
        case 'y':
            printf("\nOK, continuing...\n");
            break;
        case 'N':
        case 'n':
            printf("\nThank you for using SimpleCalc!\n\n");
            break;
    }
    return 0;
}

int get_operation() {
    size_t nbyte=1;
    int op=0;
    char *my_string = malloc(nbyte+1);
    puts(" ");
    while (op != 1 && op != 2 && op != 3 && op != 4) {
        printf("What do you want to do?\n");
        printf("--------------------------------------\n");
        printf("Press 1 for Addition\n");
        printf("Press 2 for Subtraction\n");
        printf("Press 3 for Multiplication\n");
        printf("Press 4 for Division\n");
        printf("Your choice: ");
        getline(&my_string, &nbyte, stdin);
        sscanf(my_string, "%d", &op);
        if (op != 1 && op != 2 && op != 3 && op != 4) {
            printf("\nINVALID INPUT. Please re-enter.\n");
        }
    }
    free(my_string);
    return op;
}

char do_continue() {
    size_t nbyte=1;
    char answer=' ';
    char *my_string = malloc(nbyte+1);
    puts(" ");
    while (answer != 'Y' && answer != 'y' && answer != 'N' && answer != 'n') {
        printf("Continue <y/n>: ");
        getline(&my_string, &nbyte, stdin);
        sscanf(my_string, "%s", &answer);
        if (answer != 'Y' && answer != 'y' && answer != 'N' && answer != 'n') {
            printf("\nINVALID INPUT. Please re-enter.\n");
        }
    }
   free(my_string);   
   return answer;
}

Try ko pong i code mamaya sir, maraming salamat po! 😇
 
Status
Not open for further replies.

Similar threads

Back
Top