Hey guys,
Im a beginner in programming...so please bare with me. my project is to create a simple calculator program with a twist. the user needs to input 3 variables which is 1st number, 2nd number and the operator(+ - * /). but when the user wants a square root or a reciprocal of the 1st number then 2 variables are needed. The 1st number and either 'S' or 'R' for square root and reciprocal respectively.
here's what ive done so far:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream.h>
int first, ans = 0;
int second;
char op;
char end[4];
char R, S;
void Calc();
void Recip();
void Squared();
void main()
{
while (strcmp(end, "exit")!=0)
{
printf("\nEnter the required calculations\n\n");
cin >> first >> second >> op;
Calc();
printf("\nThe answer is %d", ans);
printf("\n\nPress ENTER to continue, or type EXIT to exit: ");
gets(end);
if ((int)end != '\n')
ans = 0;
clrscr();
}
}
void Calc()
{
switch (op)
{
case '+':
ans = (first + second);
break;
case '-':
ans = (first - second);
break;
case '*':
ans = (first * second);
break;
case '/':
if (second == 0)
printf("\nCannot divide by zero");
else ans = (first / second);
break;
}
}
void Recip()
{
if(first == 0)
printf("\nInvalid Function!!!");
else ans = ( 1 / first);
}
void Squared()
{
ans = (first * first);
}
this code works fine, the problem is that how can i tell the program that if an input of only 2 variables is entered it will consider it as either a square root or a reciprocal of the first number. example: 25 S (square root of 25). Or 5 R (reciprocal of 5 i.e. 1 divided by 5).
if the user only inputs only 2 variables and hit enter then there is no need to scan for the operator. is there any way of doing this?
any help would be greatly appreciated. Thanks