Aaron
Aaron

Reputation: 15

Why isn't the calculator I'm coding with C working?

I am pretty new to C, and am working on a pretty big project. I am trying to make a calculator, and so far I only have the addition. They way it works is that it asks for how many numbers they want in the addition statement, and then I use a while() loop to try to get all the info. Just some side info - I am using the Cygwin terminal on a Windows 10 operating system.

#include <stdio.h>

int main(){
        char Etype;
        printf("Hello! Welcome to your virtual calculator!\n");

        printf("Press 'a' for addition!\n");
        scanf("%c", &Etype);

        if(Etype == 'a' || Etype == 'A') {
                int Anum;
                int x = 1;
                int y;

                printf("How many numbers do you want in this addition statement?\n");
                scanf("%f", &Anum);

                while(x < Anum) {
                        printf("Emter number %d\n", x);
                        scanf("%d", &y);
                        x = x + 1;
                }
        }

}

whenever I answer how many numbers I want on my statement, nothing happens. I hope you can help, and if so thank you!

Upvotes: 0

Views: 175

Answers (3)

mr. code
mr. code

Reputation: 67

i noticed that your code isnt that easy to read try using varables with easier names like Etype = operation it makes it user friendly

2. try adding more operators like - / *

3. when you declare the variable and the user enters "add" you can use a switch statement to to scan only the a like:

switch(var){
case'a':

it will scan only the a even if the input is "aquaman""aeroplane" it will still read the a

3. instead of while use for

declare an i variable make this for statement

for(i=0; i>(number of numbers to entered+1);i++ )
printf("number: ");
scanf(...)

Upvotes: 0

Bill Lynch
Bill Lynch

Reputation: 81926

At the very least, you should either look at the compiler warnings, or get a better compiler and look at the compiler warnings.

clang-7 -pthread -lm -o main main.c
main.c:16:29: warning: format specifies type 'float *' but the argument has type
      'int *' [-Wformat]
                scanf("%f", &Anum);
                       ~~   ^~~~~
                       %d
1 warning generated.

Upvotes: 1

dctewi
dctewi

Reputation: 130

Perhaps you just forget to summary the answer.

To input an integer, you should use %d instead of %f.

What's more, x should be initialized with 0.

You can edit your codes as:

#include <stdio.h>

int main(){
        char Etype;
        printf("Hello! Welcome to your virtual calculator!\n");

        printf("Press 'a' for addition!\n");
        scanf("%c", &Etype);

        if(Etype == 'a' || Etype == 'A') {
                int Anum;
                int x = 0; // count from 0
                int y;
                int sum = 0; // to summary the answer

                printf("How many numbers do you want in this addition statement?\n");
                scanf("%d", &Anum); // use %d to input integer

                while(x < Anum) {
                        printf("Emter number %d\n", x);
                        scanf("%d", &y);
                        sum += y; // summary
                        x = x + 1; 
                }
                printf("Answer: %d\n", sum); // print the answer after calculation
        }

}

Or a clearer version:

#include <stdio.h>

int main(){
        char eType[2];

        printf("Hello! Welcome to your virtual calculator!\n");
        printf("Press 'a' for addition!\n");

        scanf("%s", eType);

        if(eType[0] == 'a' || eType[0] == 'A') 
        {
                int n, sum = 0;

                printf("How many numbers do you want in this addition statement?\n");
                scanf("%d", &n);

                for (int i = 0; i < n; i++)
                {
                        int x;

                        printf("Enter number %d\n", i);
                        scanf("%d", &x);
                        sum += x;
                }

                printf("Answer: %d\n", sum);
        }
}

Upvotes: 1

Related Questions