scanf and printf

Hello folks,


I have a question again: This time I tried to built a C project for university. The user should prompt a single word into the command line. Before, the user is told to do so by a statement which is printed into the command line using the printf function. After that the user is requested to insert a letter by another printf statement. Then the programm wants the user the to type in letter by calling the scanf function for a second time.

In the end, a for loop counts the times the letter appears in the given word. Finally it prints out the number which was created by the loop.


The problem is if I typed in the word, the system requests me to type in the letter directly by easily skipping the printf.

After I typed in the letter, the second printf statement appears and I have to type the letter a second time.


I already tried the flush method, but it not solved the problem.


Does anyone know the solution to this answer? I have listed the code below.


Thank you!


#include <stdio.h>


int main() {

char word[5];

char letter;

int count = 0;


printf("Please type in a word.\n");

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


printf("Please type in a letter.\n");

scanf("%c\n", &letter);


for (int i = 1; i <= 20; i++) {

if (word[i] == letter) {

count++;

}

}

printf("The letter %c is used %d times.\n", letter, count);


return 0;

}

I would suspect a thread issue, where scanf is executed in a different thread. Thus, the 2 printf are executed befoer the first scanf


may have a look here:

h ttps://openclassrooms.com/courses/la-programmation-systeme-en-c-sous-unix/les-threads-3


could try also :


scanf("%s\n", word, { printf("Please type in a letter.\n" }() )


closure with printf will be evaluated immediately (hence printing) then scanf.

Hi Claude31,


thank you for your reply. How do you type in the line of code? I have just deleted the two lines which are replaced by your line of code and pasted your line of code. However, Xcode still expects an expression. Do you know how to solve the problem?


Thank you and see you.


CX8060

Hello folks,


I have just created another program. This time the same error appeared again. I have listed the code and the console output underneath this text. Maybe this helps you to understand my problem.


Enjoy the week.


CX8060


Code in C:


#include <stdio.h>


int main() {


printf("Please enter a number:\n");

int i, j;

scanf("%d\n", &i);

printf("Please enter a second number:\n");

scanf("%d\n", &j);

printf("Please enter an operator:\n");

char o;

scanf("%c\n ", &o);

int z;


switch (o) {

case '*':

z = i * j; break;

case '/':

z = i / j ; break;

case '+':

z = i + j; break;

case '-':

z = i - j; break;

default:

z = 0; break;

}


printf("The operator %c has been used. The result is: %d.\n", o, z);


return 0;

}


console output:


Please enter a number:

3

4

Please enter a second number:

*

Please enter an operator:

*

The operator * has been used. The result is: 12.

Program ended with exit code: 0

There's a C FAQ that explains scanf problems and suggests alternate approaches.


Site: c-faq.com

Page: stdio/scanfprobs.html


(Broken up to avoid infinite moderation.)

Idea would be to replace

    scanf("%d\n", &i);
    printf("Please enter a second number:\n");
    scanf("%d\n", &j);
    printf("Please enter an operator:\n");

with

    scanf("%d\n", &i, { printf("Please enter a second number:\n") } () )
    scanf("%d\n", &j, { printf("Please enter an operator:\n") } ()  )


Did you try it to adapt to objc this swift version ?

Hi Claude31,


thank you for your answer. Is there any objective C framework to import into C code? I do not want to adapt this to swift because for my training courses I need to code in C. But a big thank you to you.


Wish all of you a happy holiday season.

CX8060

Hi folks,


I have solved the problem. It is such simple: Just leave out the "\n" in the scanf-statement.


Wish all of you a happy new year.


CX8060

scanf and printf
 
 
Q