# [SOLVED] My first Calculator in C language



## deleted10242017b (Dec 18, 2010)

As you may have seen in another thread, i am just starting to learn C language, since only starting C 2 days ago, i'd say i'm doing quite well considering i'm 13, but anyways here's the code:

PS there is an attachment with the source file and the exe, if you wan't to use it / edit it.


```
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main (void)
{
float first, second, result;
char usershit;

printf("Calculator © by echo off productions\nPress enter to begin");
getche();
printf("\n\nPlease enter the first number, then press enter: ");
scanf("%f", &first);
printf("\nPlease select the type of problem, then press enter [+|-|*|/] : ");
usershit=getche();
printf("\n\nPlease enter the second number: ");
scanf("%f", &second);

switch(usershit)
{
 case '+':
 printf("The answer is: %f\n\nThankyou for using Calculator.\nPress enter to exit", first+second);
 getche();
 break;

 case'-':
 printf("The answer is: %f\n\nThankyou for using Calculator.\nPress enter to exit", first-second);
 getche();
 break;

 case '*':
 printf("The answer is: %f\n\nThankyou for using Calculator.\nPress enter to exit", first*second);
 getche();
 break;

 case '/':
 if (second<=0)
 {
  printf("You cannot divide by the number zero.\n\nThankyou for using Calculator.\nPress enter to exit");
  getche();
}

 else
 printf("The answer is: %f\n\nThankyou for using Calculator.\nPress enter to exit", first/second);
 getche();
 break;

 default:
 printf("The problem you entered is not recognized.\n\nThankyou for using Calculator.\nPress enter to exit");
 getche();
 break;



}



}
```


----------



## Ninjaboi (Dec 27, 2010)

*Re: My first Calculator in C language*

Nice for a first try indeed, and only two days into it. Keep up the work echo off . It compiled without a problem. I have just a few things I'd mention that would make the program better.

- scanf is unsafe to use. Try using scanf_s if possible.
- When it asks for the type of problem in the application, you might want to allow them to press enter, or take out the user-prompt to press enter.
- You forgot a space in between 'Thank' and 'you'.
- You can comment to add better understanding of what your are trying to accomplish in the program.
- getche is depreciated. Try using _getche ( ISO C++ ).
- You didn't use the float 'result', so I removed it from the code.

Here is my 'revised' edition to your code:


```
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main (void)
{

float first, second;
char usershit; /* Used for the problem-type input. */

printf("Calculator © by echo off productions\nPress enter to begin");
_getche();
printf("\n\nPlease enter the first number, then press enter: ");
scanf_s("%f", &first);
printf("\nPlease select the type of problem. [+] [-] 
[*] [/] : ");
usershit=_getche();
printf("\n\nPlease enter the second number: ");
scanf_s("%f", &second);

switch(usershit)
{
/* If the problem is addition. */
 case '+':
 printf("The answer is: %f\n\nThank you for using Calculator.\nPress enter to exit",  first+second);
 _getche();
 break;

/* If the problem is subtraction. */
 case'-':
 printf("The answer is: %f\n\nThank you for using Calculator.\nPress enter to exit",  first-second);
 _getche();
 break;

/* If the problem is multiplication. */
 case '*':
 printf("The answer is: %f\n\nThank you for using Calculator.\nPress enter to exit",  first*second);
 _getche();
 break;

/* If the problem is division. */
 case '/':
 if (second<=0)
 {
  printf("You cannot divide by the number zero.\n\nThank you for using Calculator.\nPress enter to exit");
  _getche();
}

 else
 printf("The answer is: %f\n\nThank you for using Calculator.\nPress enter to exit",  first/second);
 _getche();
 break;

 default:
 printf("The problem you entered is not recognized.\n\nThank you for using Calculator.\nPress enter to exit");
 _getche();
 break;

    }
}
```
I hope this helps you become a better programmer. Again, good job for the first two days of practice, just keep hacking away at it and you'll prosper.


----------



## deleted10242017b (Dec 18, 2010)

*Re: My first Calculator in C language*

First problem i see with that on is
void main (void)void main (void) is not valid

and second problem is:


```
scanf_s
```
is not valid,

i think i'll stick to the original, obviously with Thank you, not Thankyou, my mistake that's generally how i always spell it.


thanks anyway,

echo off


----------



## Ninjaboi (Dec 27, 2010)

*Re: My first Calculator in C language*

Wait, what compiler are you using? I know that void main(void) is invalid for a lot of compilers, with the exception of a few that don't mind you doing it. C++ does not allow you to pass main as void. If you wanted me to, I could of told you to try int main(). I assumed that since you used void, you were using a compiler that allowed it. You can stay with the old one, whatever makes you feel more comfortable when you code.


----------



## deleted10242017b (Dec 18, 2010)

*Re: My first Calculator in C language*

Also what is bad anout scanf("") and how would scanf_s("") make things better. and anyway i use dev C++.


----------



## deleted10242017b (Dec 18, 2010)

*Re: My first Calculator in C language*

Helloo please answer


----------



## Ninjaboi (Dec 27, 2010)

*Re: My first Calculator in C language*

[url]http://www.edcc.edu/faculty/paul.bladek/CS131/scanf_s.htm[/URL]

That links gives a great description of the difference between the two. Dev C++ is nice because it is open-source, so if you ever want to see how it works, you can always take a look.


----------



## deleted10242017b (Dec 18, 2010)

*Re: My first Calculator in C language*

Thanks, but it doesn't work, it says there are errors


----------



## Ninjaboi (Dec 27, 2010)

*Re: My first Calculator in C language*

OK, I fixed and compiled using Dev-C++. This code below should work just fine for you:


```
#include <stdio.h>
#include <conio.h>
#include <math.h>

int main (int)
{

float first, second;
char usershit; /* Used for the problem-type input. */

printf("Calculator © by echo off productions\nPress enter to begin");
_getche();
printf("\n\nPlease enter the first number, then press enter: ");
scanf("%f", &first);
printf("\nPlease select the type of problem. [+] [-] 
[*] [/] : ");
usershit=_getche();
printf("\n\nPlease enter the second number: ");
scanf("%f", &second);

switch(usershit)
{
/* If the problem is addition. */
 case '+':
 printf("The answer is: %f\n\nThank you for using Calculator.\nPress enter to exit",  first+second);
 _getche();
 break;

/* If the problem is subtraction. */
 case'-':
 printf("The answer is: %f\n\nThank you for using Calculator.\nPress enter to exit",  first-second);
 _getche();
 break;

/* If the problem is multiplication. */
 case '*':
 printf("The answer is: %f\n\nThank you for using Calculator.\nPress enter to exit",  first*second);
 _getche();
 break;

/* If the problem is division. */
 case '/':
 if (second<=0)
 {
  printf("You cannot divide by the number zero.\n\nThank you for using Calculator.\nPress enter to exit");
  _getche();
}

 else
 printf("The answer is: %f\n\nThank you for using Calculator.\nPress enter to exit",  first/second);
 _getche();
 break;

 default:
 printf("The problem you entered is not recognized.\n\nThank you for using Calculator.\nPress enter to exit");
 _getche();
 break;

    }
}
```


----------



## deleted10242017b (Dec 18, 2010)

*Re: My first Calculator in C language*

Cool, it works, just had to change the int main (int) to int main(void) to get it to work.


----------



## Ninjaboi (Dec 27, 2010)

*Re: My first Calculator in C language*

Cool, you could also add 'return 0' at the end of your main loop to complete it. Finally glad we got it all worked out .


----------



## deleted10242017b (Dec 18, 2010)

*Re: My first Calculator in C language*

Cool, Thanks a bunch, Ninja Power!!


----------



## Ninjaboi (Dec 27, 2010)

*Re: My first Calculator in C language*

YEAH!!! Lol thank you, and no problem.


----------

