Hi, i have this project where the size, 2d array of size n is scanned from a file; asks for the vertex pairs from the command prompt until the user decides to stop inputting them. There is something wrong with my code because it skips the scanning of int variables from and to. Please help me and tell if you spot the problem. Thanks :)

int repeat() {
    char rep;
    printf("\n\n Proceed to Next Matrix? Y / N: ");
    rep = getchar();
    scanf("%c", &rep);
    if(rep == 'Y')
        return 1;
    else if(rep == 'N')
        return 0;
}

int operate() {
    char reply;
    printf("\n\n Test More Vertices Pairs? Y / N: ");
    reply = getchar();
    scanf("%c", &reply);
    if(reply == 'Y')
        return 1;
    else if(reply == 'N');
        return 0;
}

int main() {
    int choice = 0, perform2=1,i,a,b, from , to;
    char filename[200]="";
    FILE * fileIn;
    FILE * fOut;
    do{
        printf("\nChoose an option: \n"
                    "[1] Read Input\n"
                    "[2] Exit\n");
        printf("Enter number: ");
        scanf("%d",&choice);
        printf("\n\n");
        fOut = fopen("output.txt", "a");
        if(choice == 1) {
            printf("Enter File name: ");

            scanf("%s", &filename); 
            if((fileIn = fopen(filename, "r")) == NULL) {
                printf("File could not be opened. \n");}
            else {
                do{
                    fscanf(fileIn, "%d", &size);

                        int input[size][size];
                            for(a = 1; a<=size; a++) {
                                for(b=1; b<=size; b++) {
                                    fscanf(fileIn, "%d", &input[a][b]);
                                }
                            }
                        fprintMatrix(input);
                        do{ 
                            printf("Enter vertex pair Ex.(4,3): ");
                            scanf("( %d , %d )", &from, &to);

                        }while(operate() ==1); //test more vertices?
                        fgetc(fileIn);
                        fprintf(fOut, "%s", "\nOutput:\n\n");

                        fclose(fOut);
                        perform2 = repeat();
                }while((perform2 == 1) && !(feof(fileIn))); //Proceed to next matrix?
                fclose(fileIn);
            }
        }
        else if(choice == 2) {
            printf("Exiting. \n");
            break;
        }
    fclose(fileIn);
    }while(choice != 2);
    return 0;
}

reverse lines 4 and 5, then lines 15 and 16. The purpose of lines 4 and 15 is to remove the '\n' from the keyboard buffer after calling scanf(). You need to do the same after line 33.

what should I use if I'm going to scan for integers, because I used getchar() for the other lines. Is there something wrong about the scanning of my input?

Myself, I usually write a function (for C programs) or a class (for C++ programs) that handles tokenizing of data - ie, extracts each token to be analyzed. I have found that strtok is unsuitable for all but the most elementary uses. Usually, you will pass a list of delimiters to the function, along with the string you are looking at. Then, you can extract the numeric or other data that the token (substring) contains. So, properly done, this string will result in the tokens 1, 2, 3, 300, 400, etc by passing the delimiters " and ' in the the function argument (it assumes that a token ends with a whitespace unless the string is within a delimiter pair): 1 2 3 "300" '400'.

This is called, for lack of a better term here, "generic programming". As a result, this function can be used in many situations, and can be tuned to the local requirements simply by changing the delimiter list.

Example:

const char* numlist = "1 2 3 \"300\" '400'";

char* getToken(const char* pStr, const char* delimiters)
{
    /* Note, the caller has to free the returned string as necessary. */
    char* output = calloc(strlen(pStr), sizeof(char));
    size_t outposn = 0;
    bool indelim = false;
    for (size_t = 0, j = strlen(pStr); i < j; i++)
    {
        if (strchr(delimiters, pStr[i]))
        {
            if (indelim == false)
            {
                indelim = true;
            }
            else
            {
                output[outposn] = 0;
                return output;
            }
        }
        else if (isspace(pStr[i]) && !indelim)
        {
            output[outposn] = 0;
            return output;
        }
        else
        {
            output[outposn] = pStr[i];
            ++outposn;
        }
    }
    output[outposn] = 0;
    return output;
}

This is clunky code, but it should give you an idea how to procede.

what should I use if I'm going to scan for integers

The same as for single characters, no different.

Is there something wrong about the scanning of my input?

No, that's just the way scanf() works.

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.