I am trying to write a simple calculation program, that returns the mathmatical sequence entered written out with the total.
Code:
//still doesnt add the number to the string "total" as it goes through the string
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
void test8();
void test8() {//Input of mathmatical sequence and output
//of the eqn in text
char s[256]; int i; int r=0; char *p=s; char c;
char total[]={" "};
cout<<"Please enter the mathmatical statement"<<endl;
cout<<"Include the = sign at the end of the statement"<<endl;
//could use a search and strcopy fucn to add = sign if it wasnt entered
cin>>s;
while (isdigit(*p)) r=r*10+(*p++)-'0';
do {
c=*p++;
if (c =='=') strcat(total," equals ");
if (c =='=') break;
if (isdigit (*p)) strcat(total,p);
i=0;
while (isdigit(*p)) i=i*10+(*p++)-'0';
switch (c) {
case '+': r += i;strcat(total," plus "); break;
case '-': r -= i;strcat(total," minus "); break;
case '*': r *= i;strcat(total," times "); break;
case '/': r /= i;strcat(total," divided by "); break;
default: cout<<"Bad input"<<endl;
} //this does the computation in the order it was typed
//no preference is given for * and / over + and -
} while (true);
cout <<total<<r<<endl;
}
int main() {test8(); return 0;}
I realize there are one or two things i still need to touch up here, but my problem is how to add the numberical value in the string s to the string total. The line if (isdigit (*p)) strcat(total,p); is not working. I have tried variation on this, and tried to include it in the switch function, but to no avail. I would appreciate any suggestions on how to go about this.
Thanks