Paul Filch
Paul Filch

Reputation: 917

C program that deals with sets issue

I am working on a C program that deals with sets of letters. However, I have encountered a problem. The code, for some reason, prints the message Enter next element of Set A twice at one time, and thus it does not ask for an input the first line of the two.

For example:

If the size of Set A is 5, the program will print:

Enter first element of Set A:(user inputs f)

Enter next element of Set A:(doesn't allow user to input value) <--printed at same time with line below
Enter next element of Set A:(allows a user input)

Enter next element of Set A:(doesn't allow user to input value) <--printed at same time with line below
Enter next element of Set A:(allows a user input)

Here is the code:

void getSetA(bool setA[], int sizeA) 
{
      letters element, letter;
      int position = 0, num;

      printf("\nEnter the first element in Set A: ");
      element = getchar();
      for(letter = a; letter <= z; letter++)
      {
                if(element == letter)       setA[position + 1] = true;
                else                        position++;           
      }
      for(num = 1; num <= sizeA -1; num++)
      {
                printf("\nEnter next element of Set A: ");
                element = getchar();
                for(letter = a; letter <= z; letter++)
                {
                           if(element == letter)       setA[position + 1] = true;
                           else                        position++;           
                }
      }
}

Note that type letters was defined by me as all the letters in the alphabet, and setA is an array of type boolean. sizeA is the size of Set A that he user has inputted previously.

Thank you in advance.

Upvotes: 2

Views: 377

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

The reason your code appears to skip every other line is that getchar returns all characters, including end-of-line markers. To address this, write your own function that wraps getchar, and skip all \n and \r characters there:

int getcharNoBreaks() {
    int ret;
    do {
        ret = getchar();
    } while (ret == '\n' || ret == '\r');
    return ret;
}

Put getcharNoBreaks or its prototype before getSetA, and replace all calls of getchar with calls of getcharNoBreaks to fix the immediate issue.

However, a larger issue with code duplication will remain: your code repeats itself, which is never a good idea. Consider switching the for loop in favor of do/while, the same kind I used in getcharNoBreaks to achieve the effect that you try to achieve by placing another copy of the code before the loop.

Finally, there is no point of iterating through all letters to see if element is one of them: you can replace that entire loop with a single if:

if (element >= 'a' && element <= 'z') {
    set[element-'a'] = true;
}

Upvotes: 1

No Idea For Name
No Idea For Name

Reputation: 11577

The problem that you're experiencing is that getchar() only grabs a single character from the input buffer. This would be fine, except that there's still a newline left in the input buffer that resulted from hitting 'enter' after your input.. You need to clear the input buffer after you use getchar() like this:

char c;
while ((c = getchar()) != '\n' && c != EOF);// the second part is only if you are working with files

Upvotes: 1

Related Questions