Member Avatar for thendrluca

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;
}
Member Avatar for thendrluca

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!

fflush(stdin); //clear the buffer???!

Don't use 'fflush' with 'stdin' ,see why.also thismight 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.

Member Avatar for thendrluca

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();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.