Virtual calculator problem...


4 replies [Last post]
Sirius
Sirius's picture
Offline
Joined: 2012-07-17
Points: 0

Hello fellow programmers of wibit.net (this is my first post)! I have been in the programming business for about 3 months now I am perfectionizing my skills in C programming, and I decided to make a program (for no professional purpose, just practice) that would serve as a virtual calculator. Sadly, it isn't working properly, Please help!

The program prompts the user what kind of mathematical function you would like it to perform and then it prompts you to enter the numbers you would like to perform the function on. It has an extra feature as well, that allows you to save the result of the answer in one of ten memory slots, and it also allows you to load one of these values as well, and also to exit.

Incredibly, I managed to compile it (by suppressing all warnings)! However, it allways crashes at the same part, here is the code:

#include stdio.h
#include stdlib.h
#include string.h
#include math.h

char* Add = "Add";
char* Sub = "Subtract";
char* Mul = "Multiply";
char* Div = "Divide";
char* Sqt = "Square Root";
char* Squ = "Square";
char* Sav = "Save Value";
char* Lod = "Load Value";
char* Exi = "Exit";

char* A = "FileA";
char* B = "FileB";
char* C = "FileC";
char* D = "FileD";
char* E = "FileE";
char* F = "FileF";
char* G = "FileG";
char* H = "FileH";
char* I = "FileI";
char* j = "FileJ";

int FileA;
int FileB;
int FileC;
int FileD;
int FileE;
int FileF;
int FileG;
int FileH;
int FileI;
int FileJ;

int SubtractNumbers (int, int);
int AddNumbers (int,int);
int DivideNumbers (int, int);
int MultiplyNumbers (int, int);
int SqurootOfNumber (int);
int RaiseNumToPow (int);
void SaveNumber (int);
void LoadNumber (char [255]);

int main()
{
  int Num1;
  int Num2;
  char FunctionChoice [255];
  char FileChoice [255];

  printf ("Welcome to iMath, the virtual calculator\n");

  START:

  printf ("Enter the mathmatical function you would like to perform\n");
  printf ("%s, %s, %s, %s, %s, %s, %s, %s or %s: ", Add, Sub, Mul, Div, Squ, Sqt, Sav, Lod, Exi);
  scanf ("%s", FunctionChoice);
  if (strcmp (FunctionChoice, Exi) == 0)
  {
    goto EXIT;
  }
  printf ("\nNow enter number(s) you would like to include in the function %s: ", FunctionChoice);
  scanf ("%d", &Num1 );
  if (strcmp (FunctionChoice, Add || Sub || Div || Mul) == 0)
  {
    scanf ("%d", &Num2);
  }
 
  EXIT:

  \\This is the part where it screws up during execution

  if (strcmp (FunctionChoice, Add) == 0)
  {
    printf ("\n%d plus %d is %d", Num1, Num2, AddNumbers (Num1, Num2));
    goto START;
  }
  else if (strcmp (FunctionChoice, Sub) == 0)
  {
    printf ("\n%d minus %d is equal to %d", Num1, Num2, SubtractNumbers (Num1, Num2));
    goto START;
  }
  else if (strcmp (FunctionChoice, Mul) == 0)
  {
    printf ("\n%d multiplied by %d is equal to %d", Num1, Num2, MultiplyNumbers (Num1, Num2));
    goto START;
  }
  else if (strcmp (FunctionChoice, Div) == 0)
  {
    printf ("\n%d divided by %d is equal to %d", Num1, Num2, DivideNumbers (Num1, Num2));
    goto START;
  }
  else if (strcmp (FunctionChoice, Sqt) == 0)
  {
    printf ("\nThe square root of %d is equal to %d", Num1, SqurootOfNumber (Num1));
    goto START;
  }
  else if (strcmp (FunctionChoice, Squ) == 0)
  {
    printf ("\nThe %d squared is equal to %d", Num1, RaiseNumToPow (Num1));
    goto START;
  }
  else if (strcmp (FunctionChoice, Sav) == 0)
  {
    SaveNumber (Num1);
    goto START;
  }
  else if (strcmp (FunctionChoice, Lod) == 0)
  {
    LoadNumber (FileChoice);
    goto START;
  }
  else if (strcmp (FunctionChoice, Exi) == 0)
  {
    printf ("\nThank you for using iMath");
  }
  else
  {
    printf ("\n%s is an Unknown function", FunctionChoice);
    goto START;
  }
  return 0;
}

int SubtractNumbers (int i, int j)
{
  return i - j;
}

int AddNumbers (int i, int j)
{
  return i + j;
}

int DivideNumbers (int i, int j)
{
  return i / j;
}

int MultiplyNumbers (int i, int j)
{
  return i * j;
}

int SqurootOfNumber (int i)
{
  return sqrt (i);
}

int RaiseNumToPow (int i)
{
  return i * i;
}

void SaveNumber (int x)
{
  char FileChoice [255];

  printf ("\nEnter which file you would like to save your data in: ");
  scanf ("%s", FileChoice);
  
  if (strcmp (FileChoice, A) == 0)
  {
    FileA = x;
  }
  else if (strcmp (FileChoice, B) == 0)
  {
    FileB = x;
  }
  else if (strcmp (FileChoice, C) == 0)
  {
    FileC = x;
  }
  else if (strcmp (FileChoice, D) == 0)
  {
    FileD = x;
  }
  else if (strcmp (FileChoice, E) == 0)
  {
    FileE = x;
  }
  else if (strcmp (FileChoice, F) == 0)
  {
    FileF = x;
  }
  else if (strcmp (FileChoice, G) == 0)
  {
    FileG = x;
  }
  else if (strcmp (FileChoice, H) == 0)
  {
    FileH = x;
  }
  else if (strcmp (FileChoice, I) == 0)
  {
    FileI = x;
  }
  else
  {
    FileJ = x;
  }
}

void LoadNumber (char FileChoice [255])
{
  printf ("\nEnter which file you would like to load data from: ");
  scanf ("%s", FileChoice);

  if (strcmp (FileChoice, A) == 0)
  {
    printf ("\n%d", FileA);
  }
  else if (strcmp (FileChoice, B) == 0)
  {
    printf ("\n%d", FileB);
  }
  else if (strcmp (FileChoice, C) == 0)
  {
    printf ("\n%d", FileC);
  }
  else if (strcmp (FileChoice, D) == 0)
  {
    printf ("\n%d", FileD);
  }
  else if (strcmp (FileChoice, E) == 0)
  {
    printf ("\n%d", FileE);
  }
  else if (strcmp (FileChoice, F) == 0)
  {
    printf ("\n%d", FileF);
  }
  else if (strcmp (FileChoice, G) == 0)
  {
    printf ("\n%d", FileG);
  }
  else if (strcmp (FileChoice, H) == 0)
  {
    printf ("\n%d", FileH);
  }
  else if (strcmp (FileChoice, I) == 0)
  {
    printf ("\n%d", FileI);
  }
  else
  {
    printf ("\n%d", FileJ);
  }
  
}

Many thanks in advance to the one that can solve this problem (sorry for the long post...)!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dirtypitbull18
dirtypitbull18's picture
Offline
Joined: 2012-07-08
Points: 0

char* for you variales and %d to printf it isn't gonna work without doing  conversion from character to decimal with type cast or building a string to decimal or other variable type to use them as numbers. Not sure if im correct on that but I would check that as a start until Brian and Kevin can get back with you. And tell us wether or not I am correct.

Sirius
Sirius's picture
Offline
Joined: 2012-07-17
Points: 0

Thanks for responding, but where in my program do you see this problem?

LoveCats
LoveCats's picture
Offline
W007! I watched 10 vids!
Joined: 2011-06-25
Points: 10

Line 67

if (strcmp (FunctionChoice, Add || Sub || Div || Mul) == 0)

You're appling LOGICAL OR on the strings. The OR function will produce following results.

0 OR 0 = 0

0 OR 1 = 1

1 OR 0 = 1

1 OR 1 = 1

Now, what you're trying to do is "ADD" OR "SUB" OR "DIV". The program will obviously crash since you're trying to LOGICALLY OR the strings. You can only perform logical operations on boolean returns i.e. on 1 and 0 (true or false).

Besides I don't see any purpose of doing this block

  if (strcmp (FunctionChoice, Add || Sub || Div || Mul) == 0)
  {
    scanf ("%d", &Num2);
  }

Despite of the operation select by the user, you'll compulasarily require the second number. You should just accept both the numbers and then perform the operation. Just removing that block of code makes your program working.

 

Sirius
Sirius's picture
Offline
Joined: 2012-07-17
Points: 0

Thanks a lot LoveCats!

You were right on the subject of that logical operation being performed on strings!

I suppose it wasn't necessary anyway so thanks for pointing that out!