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

Your problem is that your input is variable. You can enter one of:

number  operator
  number  number  operator

However, your code only permits the second type of input:

cin >> first >> second >> op;

I'll give you a hint: don't use int variables as the targets of your cin statement. Use strings and convert them to a number if necessary.

Good luck.

still doesnt answer my question though

Actually, I've answered your question quite thoroughly. You have to think now. I'll not do your homework for you. If I did, you wouldn't learn anything.

commented: Good. +18
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.