I've been stuck at a roadblock for almost an hour now. I'm trying to write a program that takes user input from the command prompt of the form:
[n # spaces] [operand] [n # spaces] [operator] [operand] [n#spaces]
Where each [n # spaces] component means some unknown number of spaces. For example, the user should be able to enter some expression such as: " 380 * 4 " and get both their output echoed and the result of the expression. We can only use getchar() to read input; no string handling functions are allowed. We can also assume the operands are positive integers. For now I believe these are the only relevant conditions of the problem necessary to share.
Initially I thought the program would be straightforward and to some extent it was; I got the echo to work up to and including the operator as well as the answer function to work, but after a few small changes I was foolish enough to not keep track of the program seems to hardly work at all. I had to quickly teach myself pointers because I wanted to circumvent the supposed fact that C cannot pass parameters to functions by reference like you can in C++, so I'm betting the problem lies there, but again all of my functions seem to have been working just a while ago.
Source Code:
Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
//Prototypes
int SpaceCounts(int* iovar_p);
int Operand(int* iovar_p);
char Operator(int* iovar_p);
double answer(int operand1, int operand2, char Arsign);
void PrintSpaces(int spaces);
int main()
{
int iovar = getchar();
int spaces1, spaces2, spaces3, spaces4;
char ArSign;
int operand1, operand2;
spaces1 = SpaceCounts(&iovar);
printf("%d", spaces1);
operand1 = Operand(&iovar);
spaces2 = SpaceCounts(&iovar);
ArSign = Operator(&iovar);
spaces3 = SpaceCounts(&iovar);
operand2 = Operand(&iovar);
spaces4 = SpaceCounts(&iovar);
//Output
PrintSpaces(spaces1);
printf("%d", operand1);
PrintSpaces(spaces2);
printf("%c", ArSign);
PrintSpaces(spaces3);
printf("%d", operand2);
PrintSpaces(spaces4);
printf("= ");
printf("%6.2f", answer(operand1, operand2, ArSign));
system("pause");
return 0;
}
int SpaceCounts(int* iovar_p)
{
*iovar_p;
int spaces = 0;
while (*iovar_p == ' ');
{
spaces++;
*iovar_p = getchar();
}
return spaces;
}
//Converts characters to integers; using code from textbook here
int Operand(int* iovar_p)
{
int operand = 0;
while (isdigit(*iovar_p))
{
operand = operand * 10 + (*iovar_p - '0');
*iovar_p = getchar();
}
return (operand);
}
//Reads in arithmetic operator
char Operator(int* iovar_p)
{
*iovar_p;
char ch = (char)*iovar_p;
switch (ch)
{
case '+':
return ch;
*iovar_p = getchar();
break;
case '-':
return ch;
*iovar_p = getchar();
break;
case '*':
return ch;
*iovar_p = getchar();
break;
case '/':
return ch;
*iovar_p = getchar();
break;
case '%':
return ch;
*iovar_p = getchar();
break;
default:
exit(1);
}
}
//Calculator Function
double answer(int operand1, int operand2, char Arsign)
{
if (Arsign = '+')
return (operand1 + operand2);
else if (Arsign = '-')
return (operand1 - operand2);
else if (Arsign = '*')
return (operand1 * operand2);
else if (Arsign = '/')
return ((double)operand1 / operand2);
else
return (operand1 % operand2);
}
void PrintSpaces(int spaces)
{
int i;
for (i = 0; i < spaces; i++)
printf(" ");
}