954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I can't get to the scanf("%c",&q);

This is my first post on daniweb :) and i am a beginner in C programming and i don't know so good english :) sorry!
This is my program

My problem consist of
I run the program and i can't get to the scanf("%c",&q); /* Row 21 after printf. */, the program considered that i have already asigned a char value to the variable q
Where i'm wrong!
thanks in advance for understanding! :thx:
ps: there are spoilers on topic, can i use them ?!
this program i finished in pascal and manualy converted to C

#include <stdio.h>
#include <conio.h>

int main()
{
    char tab[50];
    int i,n,j,k,b,h;
    char q,x;
    printf("Dati numarul de litere ale vectorului: "); scanf("%d",&n);
    puts("Dati literele vectorului incluzind litere din vectorul A:");
    printf("A=[ ");
    for (i=97; i<=103; i++)
    {
        printf("%c ",i);
    }
    puts(" ]");
    for (i=0; i<n; i++)
    {
        scanf("%c",&tab[i]);
    }
    printf("Dati o litera pentru a fi analizata: "); scanf("%c",&q);
    b=1; i=1;
    while (b==1 && i<=n)
    {
        if (((q=='a') || (q=='b') || (q=='c') || (q=='d') ||(q=='e') || (q=='f') || (q=='g')) && (q==tab[i]))
        {
            b=0;
        }
        i++;
    }
    if (b==1)
    {
        printf("Litera %c nu persista in vect A si vect dat,\n",q);
        printf("de aceea programul a sortat vectorul,\n");
        printf("si a introdus litera la pozitia necesara.\n");
        for (i=1; i<n-1; i++)
        {
            x=tab[i];
            for (j=i+1; i<n; i++)
            {
                if (tab[j]<x)
                {
                    x=tab[j];
                    tab[j]=tab[i];
                    tab[i]=x;
                }
            }
        }
        h=1; i=2;
        while ((h==1) && (i<n))
        {
            if ((tab[i-1]<=q) && (tab[i]>=q))
            {
                for (j=n; j>=i; j--)
                {
                    tab[j]=tab[j-1];
                }
                tab[i]=q; h=0;
            }
            else
            {
                tab[n]=q;
            }
            i++;
        }
    }
    else
    {
        printf("Litera %c persista in vect A si vect dat,\n",q);
        printf("de aceea programul scoate litera %c,\n",q);
        puts("si comprima locurile ocupate.\n");
        for (i=n-1; i>=0; i--)
        {
            if (tab[i]=q)
            {
                for (j=i;  j<n; j++)
                {
                    tab[j]=tab[j+1];
                }
            }
        }
    }
    for (i=0; i<n; i++)
    {
        printf("%c ",q);
    }
    getch();
    return 0;
}
thendrluca
Newbie Poster
4 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

searched the net and found
i used fflush(stdin); before reading the char variable

fflush(stdin); //clear the buffer???!
printf("Dati o litera pentru a fi analizata: "); scanf("%c",&q);


there are other methods!? HELP PLS!

thendrluca
Newbie Poster
4 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 
fflush(stdin); //clear the buffer???!

Don't use 'fflush' with 'stdin' , see why. also this might be helpful.
Either use getchar after each call to scanf to discard the '\n' from the input buffer or don't use scanf at all.There are other solutions for interactive inputs.

diwakar wagle
Posting Whiz in Training
203 posts since Nov 2008
Reputation Points: 48
Solved Threads: 31
 

Another solution

void bflush(void)
{
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
}

after each scanf(); we need to put this function bflush();

ex:
scanf("%d",&n);
bflush();

thendrluca
Newbie Poster
4 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: