0

I am runing my code but it doesn't let me give it an input as i told.

I suspect it to be a buffer problem related to c language. However i tried flushing the buffer using fflush(stdin) before the problems input and after it.

so the first input is here:

do {

printf("Welcome to Full Adder\n");
printf("(1)Compute and display the outputs\n(2)Quiet\nYou choose: ");
scanf("%d", &QorR);
fflush(stdin);
printf("You have chosen option %d\n",QorR)

as you could see theirs an fflush there ust after i give it the input

the code with the problem is as follows:

for(A=0, B=0, C_in=0, D=0; BBase==8 ;BBase == 0){       
        A=0;B=0;C_in=0;D=0;

        fflush(stdin);
        printf("enter a number"); //"it stops exactly here"
        scanf("%c",&D);         //%c to be able to compare it to it's ASCII value
                                //i think thats why theirs
            if (D > 067){
                printf("Octal %d cannot be represented with 3 bits! Please try again!",D);continue;
            }//go back to input


            C_in=D%2;
            D=D/2;

            B=D%2;
            D=D/2;

            A=D%2;
            D=D/2;


            switch (A) {

                case 1:                                             

and it jumps to the end of the loop. Can someone help me with this problem? the funny thing is that it works perfectly at BBase==2, but at BBase==16&BBase==8 it doesn't, although the three of them are written the same way.


Update I put a scanf(%d,&scrap); just befor the enter a numbermessage and it waited for me to give it an input. how so?


Update Problem solved! Added __fpurge(stdin) and it's working in a better way now. Thanks to all the engagement in the comments.

  • I'm working on ubuntu if the OS has a relation to the problem – Sanad AlArousi Jul 19 '18 at 18:23
  • More people will be able to help you if you give a MCVE. However: `fllush(stdin)` is not actually defined by the standard, and most implementations have it clear any input that has yet to be read. That won’t help you here. – Davislor Jul 19 '18 at 18:27
  • I also doubt that the loop in your second fragment sets its variables correctly, but it’s impossible to tell. – Davislor Jul 19 '18 at 18:29
  • can you provide entire code..? – anoopknr Jul 19 '18 at 18:30
  • You are having one of the common problems with `scanf`. You do indeed have a problem with extra characters lurking in your input buffer -- however, `fflush(stdin)` is *not* the way to fix them. See [this old question](https://stackoverflow.com/questions/6277370/replacement-of-fflushstdin) for hints. – Steve Summit Jul 19 '18 at 18:30
  • so `fpurge()` will help? – Sanad AlArousi Jul 19 '18 at 18:32
  • @SteveSummit is there any other way in taking input in C other than `scanf()`? something with the feature of `endl;` in C++ – Sanad AlArousi Jul 19 '18 at 18:33
  • @SanadAlArousi Yes. You can read one line using `fgets()`. (It does have the issue that it leaves the `\n` character in the string, so sometimes you have to take steps to remove it.) `scanf` is typically used only in introductory texts and classes; it is not generally used and is not recommended for "real" programs. – Steve Summit Jul 19 '18 at 18:46
  • The loop is definitely fishy to say the least: the last Argument to it, `BBase==10` is without side-effects, its value never used. Sure you want *this* exactly *there*? – Michael Beer Jul 19 '18 at 19:46
  • @MichaelBeer i didn't get what you mean – Sanad AlArousi Jul 19 '18 at 20:00
  • 1
    @Sanad Well, your loop head is: `for(A=0, B=0, C_in=0, D=0; BBase==8 ;BBase == 0)` which means: When entering the loop, do `A=0, B=0, C_in=0, D=0;` which is reasonable. Then in each Iteration check whether `BBase==8` is true and exit the loop if not. Also plausible (at the first glance at least). And then if not exited, do `BBase==0` which is useless to say the least. `BBase==0` just compares `BBase` to `0`, nothing beyond. You could replace the head of your loop with `for(A=0, B=0, C_in=0, D=0; BBase==8;;)` or `for(BBase==0, A=0, B=0, C_in=0, D=0; BBase==8;;)` . – Michael Beer Jul 19 '18 at 20:31
  • Just drop the last semicolon in the 2 last for loop heads - typo. I am on my mobile... Anyhow, I strongly suspect that you wanted something else as the third argument to the loop. I mean, you are not even aware of it which indicates it did not happen on purpose... – Michael Beer Jul 19 '18 at 20:39
  • @MichaelBeer I noticed this now. Thanks a lot. Even my colleague when I stopped by to him to help me, he didn't notice this one. – Sanad AlArousi Jul 19 '18 at 20:46

0 Answers0