c problem

I should write a program for simple calculator that does addition,subtraction,multiplication,division and module.
It should work like that :ask user to enter expression and the echo back user expression(removing all spaces ) with the correct answer.
we can only use getchar, putchar, no arrays, no scanf for the calculations. printf is ok.
It is kind of impossible program for me.


so this program continously loops around the first if statement never processing the rest of the information.. how do i stop it and allow it to run the actualy calculation.

the user is supposed to put in " 3 + 5" and then the program outputs "3 + 5 = 8"

BUT IT DOESNT it just echoe's back what i put in. " 3 + 5 "
Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <math.h>

char Cont (void);
int isdigit1 (int C);
int Convert(char );
int main(void)
{
int Error = 0 , Num1 , Num2, Oper, c;
char Flag;
double Result;
do
{
while ( ( c=getchar() ) != EOF)
{
if ((c != ' ') || (c != '\t'))
{
putchar(c);
continue;
}


if /*while*/ (isdigit1 (c))
{
Num1 = Convert(c);
}
if ((c=='+') || (c=='-') || (c=='*') || (c=='/') || (c=='%'))
{
switch (c)
{
case '+':
Oper = 1; // current month days
break;

case '-':
Oper = 2;
break;

case '*':
Oper = 3;
break;

case '/':
Oper = 4;
break;

case '%':
Oper = 5;
break;
}
putchar (c);
}

if /*while*/ (isdigit1 (c))
{
Num2 = Convert(c);
}

switch (Oper)
{
case 1:
Result = (double)(Num1 + Num2);
break;

case 2:
Result = (double)(Num1 - Num2);
break;

case 3:
Result = (double)(Num1 * Num2);
break;

case 4:
Result = (double)(Num1 / Num2);
break;

case 5:
Result = (double)(Num1 % Num2);
break;
}
printf (" = %.2f", Result);

Flag = Cont();
}
}while ((Flag == 'y') || (Flag == 'Y') );
return 0;
}

int Convert (char c)
{
int ch,
sum = c - '0';
while ((( ch = getchar() ) != ' ' ) && (ch != '\n' ))
{
if ( !isdigit1 (ch) )
break;
sum = sum *10 + ( ch - '0');
putchar(ch);
}
return (sum);
}
int isdigit1 (int c)
{
if ((c>= '0') && (c<= '9'))
return 1;
return 0;
}
char Cont ()
{
char Flag;
printf ("******************\nWould you like to continue?\nEnter y for yes, Anything else for no.\n");
scanf_s ("%c%*c", &Flag);
return Flag;
}
Last edited on
Firstly, without proper indentation code is hard to read. You should fix that.
Your first problem is the continue on line 19. It says "if char is not a space, do nothing", which is not what you want. You should put the continue in an else statement.
Also the or in the if is wrong. No char is both space and tab, so this is always true. It should be &&.
Your code still wont work right, though.
Try to figure it out.
I think you should have ch = getchar(); before line 27.

EDIT: You should also try to use ungetc with stdin where necessary. For example, you should perform ungetc(ch, stdin) before returning from Convert. This way you wont loose the operation symbol that may follow immediately after a number.

http://www.cplusplus.com/reference/clibrary/cstdio/ungetc/

If you can not use ungetc, you may have to resort to some global variable ugliness.

Regards
Last edited on
Topic archived. No new replies allowed.