0

I built it when I click to run it's showing no errors no warnings nothing

#define __USE_MINGW_ANSI_STDIO 1
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char name[20], address[30];
    char c;
    printf("put your pass key: ");
    c = getchar();

    printf("Enter your name: ");
    scanf("%s",name);

    printf("Enter your address: ");
    scanf("%s",address);

    printf("--------------------------\n");
    printf("Entered Name: %s\n",name);
    printf("Entered Address: %s\n",address);

    printf("Your pass key: ");
    putchar(c);
}

I tried to run it from pass key point and it works but when I continue it's given me nothing

Barmar
  • 741,623
  • 53
  • 500
  • 612

3 Answers3

0

I have compiled you code and it works fine for me. Everything is fine. Maybe the input values provided by you is not in correct format.

rng70
  • 25
  • 9
  • That probably should have been a comment (but others have said pretty much the same thing). – Keith Thompson May 29 '20 at 19:12
  • actually comments and my answer was post almost at the same time. I was just typing and they were just commenting. That's why same thing twice. – rng70 May 29 '20 at 19:14
  • Yet this still does not make an answer. I undertand that you don't have enough repo to write a comment. That is no reason to write an answer instead which does not answer anything instead. – Gerhardh May 29 '20 at 19:49
0

The Code is working perfectly fine. Check your results here.

As rng70 said, recheck your input values. Also, if you're adding spaces between your string input, it won't work because whitespace acts as a delimiter for scanf() by default.

Sorry to bother!

To be precise, characters after spaces are not considered as input for strings, if that's what your error is.

To solve that problem, you can use scanf("%[^\n]", name);

Check md5's post on this for assistance. Thanks.

  • The solution is *not enough* if you make the same change to `address` input, where the input buffer will already contain the newline entered at the end of `name`. – Weather Vane May 29 '20 at 19:25
0

Unfortunately, I'm not sure if I understood your question correctly.

However, under Linux the program would work the following way:

You close your eyes, assume your program works correctly and type. You will see:

put your pass key: KMartin
Enter your name: Enter your address: Hello
--------------------------
Entered Name: Martin
Entered Address: Hello
Your pass key: K

Why?

When you type some keys in Linux, the operating system will store the keys pressed in some buffer until the "enter" ("return") key is pressed.

Maybe MINGW has the same behaviour.

So the program reaches getchar() and I type "K" "M" "a" "r" "t" "i" "n".

Linux stores these keys into a buffer and instead of passing these keys to the program.

I press the "enter" ("return") key. Then Linux will pass the "K" to the getchar() function and the remaining keys ("Martin") to the scanf() function.

This behaviour can be disabled:

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>

int main(void) {
    char name[20], address[30];
    char c;
    struct termios tios;

    printf("put your pass key: ");

    /* Disable the buffer */
    tcgetattr(1,&tios);
    tios.c_lflag&=~ICANON;
    tios.c_cc[VTIME]=0;
    tios.c_cc[VMIN]=1;
    tcsetattr(1,TCSANOW,&tios);

    c = getchar();

    /* Re-enable the buffer
     * If the buffer is not enabled,
     * sscanf() won't work correctly. */
    tios.c_lflag|=ICANON;
    tcsetattr(1,TCSANOW,&tios);

    printf("\nEnter your name: ");
    ...

... and the output will be:

put your pass key: K
Enter your name: Martin
Enter your address: Hello
--------------------------
Entered Name: Martin
Entered Address: Hello
Your pass key: K

EDIT

i run it on eclipse and it's shows nothing but when using repl like @user366312 it works I don't see anything in console on eclipse

The consoles in Eclipse typically do not allow input so programs can write output but not receive input.

However, I know that there is some check box (at least in some Eclipse versions) that allow input...

I don't know if you will have the problem I described above when using Eclipse.

However, if you have that behaviour, the settermios method probably won't work...

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38