Note that there are some explanatory texts on larger screens.

plurals
  1. PONot getting correct variable assignment from getchar in C
    text
    copied!<p>I'm writing a simple calculation program, however the only string handling functions I can use are getchar and putchar. Right now I'm just trying to assign the numbers from input to variables, but when I print the variable it's some random number. For example, I entered 3 into the console and the output was 505110. Any help would be appreciated. Thank you. </p> <pre><code>#include &lt;stdio.h&gt; #include "math.h" int addFunction( int, int); int subtractFunction(int, int); int multiplyFunction(int, int); int modulusFunction(int, int); float divideFunction(float, float); int main(int argc, const char * argv[]) { int iochar = 0; char num1 = 0, num2 = 0, continuePrompt, operator = 0; do { iochar = getchar(); getchar(); if ((iochar &gt;= 0) &amp;&amp; (iochar &lt;= 20000)) { num1 = iochar; } if ((iochar == '+') || (iochar == '-') || (iochar == '*') || (iochar == '/') || ( iochar == '%')) { operator = iochar; } if ((num1 &gt;= 0) || ((iochar &gt;= 0) &amp;&amp; (iochar &lt;= 20000))){ num2 = iochar; } switch (operator) { case '+': iochar = addFunction(num1, num2); break; case '-': iochar = subtractFunction(num1, num2); break; case '*': iochar = multiplyFunction(num1, num2); break; case '%': iochar = modulusFunction(num1, num2); break; case '/': iochar = divideFunction(num1, num2); break; } putchar(iochar); printf("Would you like to make another calulation? (y or n)"); scanf("%c", &amp;continuePrompt); } while (continuePrompt != 'n'); return 0; } int addFunction(int x, int y){ return x + y; } int subtractFunction(int x, int y){ return x - y; } int multiplyFunction(int x, int y){ return x * y; } int modulusFunction(int x, int y){ return x % y; } float divideFunction(float x, float y){ return x / y; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload