Note that there are some explanatory texts on larger screens.

plurals
  1. POC getchar input problems
    primarykey
    data
    text
    <p>I need to receive a single line of characters and convert them from the base they are written in to a decimal base, <em>as soon as a character is typed</em>.</p> <p>I did this by using the following function:</p> <pre><code>int inToDec(int base) //My assumption is that Enter will not be pressed as the first character - if it is, I am not sure what to do in that case { int numdec=0, c;//decimalized number, a variable for character inpu while((c = getchar()) != '\n' &amp;&amp; c != EOF) { if(c &gt;= 97)//Checks if the char is an actual char c = c - 'a' - 1; else//or a digit char c = c; numdec += numdec * base + c;//every time another char is inputted, it multiplies the numdec by the base and adds the char's value in decimal base - which is essentially the algorithm for conversion to decimal base. } return numdec; } </code></pre> <p>My main():</p> <pre><code>#include &lt;stdio.h&gt; #include "funcs.h" int main() { int option, fbase, tbase, num1, num2; do { scanf("%d",&amp;option); switch(option) { case 1: scanf("%d",&amp;fbase); num1 = inToDec(fbase); num2 = inToDec(fbase); outFromDec(num1+num2,fbase); break; case 2: scanf("%d",&amp;fbase); num1 = inToDec(fbase); num2 = inToDec(fbase); if(num1&gt;=num2) outFromDec(num1-num2,fbase); else outFromDec(num2-num1,fbase); break; case 3: scanf("%d",&amp;fbase); scanf("%d",&amp;tbase); num1 = inToDec(fbase); outFromDec(num1,tbase); break; } }while(option != 4); return 0; } </code></pre> <p>Except that when I input the number(after the option and the desired base) and type Enter to continue to the next one, suddenly it starts to print a character time after time without end. What did I do wrong and how do I fix it? Or, if you do not want to explain some obvious fact to a noob, where do I read about it (preferably without too much heavy duty tech-talk, as I am relatively new to programming).</p> <p>outFromDec() - </p> <pre><code>void outFromDec(int num, int base) //converts num into the base given and prints { int count = 0 , temp = num, i; char c; while(temp != 0) { temp /= base; count++; } temp = num; do { for(i=1;i&lt;count;i++) temp /= base; if(temp&lt;=9) c = '0' + temp; else c = 'a' + temp - 1; putchar(c); count--; temp = num / temp; }while(temp != 0); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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