Newbie here. C function problem during execution

Edit: Since I understand that I need to provide more info to make it clear for you guys, I added the main function and the getchoice and also two images of the program running. My problem is that after entering the endword, I want to see the menu first and then make a choice, whereas it prompts me to give an input without showing the menu.

This function is part of a bigger program, but this is where a problem occurs. It reads words inputed, places them into an array, until the keyword ****END is entered. However, when this keyword is entered, it doesn't go immediatelly in the specified if clause (you will see that in the code). I'm a newbie and it could be something really obvious, but any help is greatly appreciated.

#include <string.h>

#define M 50
#define N 15

void getText(char a[M][N])
{
    int i, j;
    char temp[N];
    for (i = 0; i < 50; i++) {
        for (j = 0; j < 15; j++) {
            if (i == 49 && j == 14) {
                printf("Maximum length of text reached.\n");
            }

            scanf("%s\n", temp);

            if (strcmp(temp, "****END") == 0) {
                printf("You entered the endkey.\n");
                return;
            }

            strcpy(a[i], temp);
        }
    }
}

int main(){

int input;

while(1){
    input = getChoice();

    if(input == 1){
        getText(text);
    }

    else if(input == 2){
        getDictionary();
    }

    else if(input == 3){
        correctText();
    }

    else if(input == 4){
        saveText();
    }

    else if(input == 5){
        getStats();
    }

    else if(input == 6){
        break;
    }

}


return 0;
}

int getChoice(){
    int temp;
printf("Choose function:\n1: Enter text\n2: Enter dictionary\n3:     Correct  text\n4: Save text\n5: Get text statistics\n6: Exit program\n");
scanf("%d", &temp);
return temp;
}

Entered the endword and now it waits for input instead of showing the menu.

I inputed 2 for the second program function, then it showed the menu and proceeded to function 2.

Upvotes: 0

Views: 79

Answers (2)

Swordfish
Swordfish

Reputation: 13134

#include <stddef.h>  // size_t
#include <ctype.h>   // isspace()
#include <stdio.h>   // scanf(), puts()
#include <string.h>  // strcmp()

// see https://stackoverflow.com/questions/2653214/stringification-of-a-macro-value
#define STRINGIFY(x) #x
#define STRING(x) STRINGIFY(x)

#define LINES 50
#define COLS 15

char const *end = "****END";

// throw away everything until a newline is found
void clear(FILE *stream)
{
    int ch;
    while ((ch = getc(stream)) != EOF && ch != '\n');
}

size_t getText(char dst[LINES][COLS + 1])
{
    size_t i = 0;
    for (; i < LINES; i++) {
        char temp[COLS + 1] = { 0 };
        scanf("%" STRING(COLS) "s", temp);  // "%15s" at runtime.

        int ch;
        // if the next character is not whitespace ...
        if ((ch = getchar()) != EOF && !isspace(ch)) {
            puts("Warning: Input too long, was truncated!");
            clear(stdin);
        }

        if (strcmp(temp, end) == 0) {
            puts("You entered the endkey.");
            return i;
        }

        strcpy(dst[i], temp);
    }
    return i;
}

int main(void)
{
    // COLS + 1 ... we need space for the terminating newline character.
    char foo[LINES][COLS + 1];
    size_t n = getText(foo);

    for (size_t i = 0; i < n; ++i)
        puts(foo[i]);
}

The %s conversion specifier should never be used without specifying a width to limit the characters that get stored:

char foo[10];
scanf("%9s");

Upvotes: 0

Weather Vane
Weather Vane

Reputation: 34585

Apart from the unnecessary double-nested loop, this line

scanf("%s\n", temp);

should be

scanf("%s", temp);

Usually, you should not try to match trailing whitespace with scanf, and the format specifier %s automatically filters out leading whitespace (but note that %c does not).

There are other faults and the code presented was originally incomplete, but notably the input length for %s must be restricted to prevent buffer overflow.

Upvotes: 1

Related Questions