This question already has answers here:
How do you allow spaces to be entered using scanf?
(11 answers)
Closed 6 years ago.
this program with this value :
ABCDEF
is ok at the frist
but again when you enter a value with space,like :
ABC DEF
the program works wrong!!!!
while loop ignores scanf the second time
What am I doing wrong?!
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(){
bool checkSrc = false;
bool checkDst = false;
while (!checkSrc && !checkDst)
{
char ins[10];
printf("White Player : ");
scanf("%s",&ins);
}
}
%s - String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).
I recommend you using fgets() instead of scanf() since the latter has no buffer overflow protection.
#define namesize 15
char *ins = malloc (namesize);
fgets(ins, namesize, stdin);
Related
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
After printing "Second text", fgets expect from me to enter a string but a program is always being stopped. That happens when I try to enter a char by scanf or getchar. What's happening?
#include <stdio.h>
int main()
{
char c[100],cc;
int x;
printf("First text\n");
scanf("%d",&x);
printf("Second text\n");
fgets(c,100,stdin);
//scanf("%c",&cc);
//cc=getchar();
printf("\n %s %d",c,x);
}
You probably press "enter" after having entered a number; scanf will then read the number, but will leave a '\n' (i.e. the newline represening "enter") in the buffer; This will be treated as an "empty" line then by gets. (BTW: use fgets instead of gets).
To overcome this enter the number and the text seperated by a space in a single line (i.e. without a newline in between).
This question already has answers here:
Why scanf("%d", [...]) does not consume '\n'? while scanf("%c") does?
(4 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 6 years ago.
The program won't compile fgets in the teste1 function. Or at least it isn't working properly, it won't let me type the string, the program will end right after it prints "Nome do cliente".
If I disable the other scanf's in the function, it will run without any issue.
How do I make fgets to work?
#include <stdio.h>
void teste1(){
char teste[50];
printf("Nome do cliente\n");
fgets(teste,50,stdin);
}
void teste2(){
teste1();
}
void teste3(){
int opc1,opc2;
printf("\nSeleccione a área desejada\n1- Clientes\n2- Concessionários\n3- Carros de demonstração\n");
scanf("%d",&opc1);
printf("\nSeleccione a área desejada\n1- Inserir\n2- Alterar\n3- Remover\n4- Consultar\n");
scanf("%d",&opc2);
teste2();
}
int main()
{
teste3();
}
The Enter key you pressed for the last scanf input will be left in the input buffer as a newline. The fgets function will read this newline and think it is finished.
This question already has answers here:
fgets instructions gets skipped.Why?
(3 answers)
Closed 6 years ago.
I have written a simple program in C which is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int length;
printf("Enter the length of the string:\t");
scanf("%d",&length);
char str1[10];
printf("Enter string:\t");
gets(str1);
printf("%s",str1);
return 0;
}
When I execute it - I get an output as:
Enter the length of the string: 5
Enter string:
Process returned 0 (0x0) execution time : 1.740 s
Press any key to continue.
I don't know why it doesn't ask for the string input and simply quits the program.
When you type '5’ followed by the enter key, you are sending two chars to the program - '5' and newline. So your first scanf gets the '5' and the second gets the newline, which it converts to the number zero.
See How to read a line from the console in C?
When you enter 5 and press enter which is "\n" then "\n" remains in stream and gets assigned to str1. You need to take that "\n" out of the input stream for which many choices are there. You can figure that out. :) Perhaps later I will edit this answer to let you know.
Edit 1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int length;
char c;
printf("Enter the length of the string:\t");
scanf("%d%c",&length, &c);
char str1[10];
printf("Enter string:\t");
gets(str1);
printf("%s",str1);
return 0;
}
This is incorrect way of doing it but your code will at least start working. You can also simply call getc(stdin) which is slightly better. The scanf regex specified in the other answers where it has been marked as duplicate will also work but is ugly and unnecessarily complicated.
I have not tested this and it may not work.
This question already has answers here:
How do you allow spaces to be entered using scanf?
(11 answers)
Closed 6 years ago.
I've a problem and I couldn't find answer.
I need to get 2 strings from user using scanf(must) and print them.
I have 2 problems
1. when I type the first string it's somehow skip the second one.
2. if my string includes spaces, I takes only the chars till the space, from what I think the 2nd word goes to the second "scanf"
this is my code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
void main()
{
char format[30];
char numbers[20];
scanf("%s", format);
scanf("%s", numbers);
printf("\n %s %s", format, numbers);
}
is it possible to get 2 strings from user 1 after another including spaces?
Thanks in advance
I solved it this way
I added another scanf which I don't need and now skipping the second scanf but it stops on the third.
not the best way but it helps.
scanf("%[^\n]s", format);
scanf("%c", &temp);
scanf("%[^\n]s", numbers);
This question already has answers here:
Disable warning: the `gets' function is dangerous in GCC through header files?
(10 answers)
why gets() is not working?
(4 answers)
Closed 6 years ago.
I'm doing a school project in C where I have to make a function that gets a string input and reverses the string as an output. I could use scanf("%s", answer);, but this only stores the first word of the string. Therefore I decided to go with gets(answer). I know this is unsafe because it doesn't allocate a specific memory size in answer, but I allready did this when defining the array: char answer[100];
I'm not interested in using fgets because the teachers will compile using Cygwin and this warning usually only shows up on Terminal when using a Mac:
warning: this program uses gets(), which is unsafe.
It will display this in the terminal right before prompting the user to type in a string. The other problem I have is that gets(answer) sometimes catches input from the printf("Type a string: ") above it. Is there any way to unvoid this?
Source code:
#include <stdio.h>
#include <string.h>
void reverseString();
int main() {
char str[100];
printf("Type your string here: ");
gets(str); /* Catching return from prinf so add one extra */
gets(str);
reverseString(str);
}
void reverseString(char string[]) {
char temp;
int startChar = 0;
int endChar = strlen(string) - 1;
while (startChar < endChar) {
temp = string[startChar];
string[startChar] = string[endChar];
string[endChar] = temp;
startChar++;
endChar--;
}
printf("\nReverse string is: %s\n", string);
}
Running the code:
warning: this program uses gets(), which is unsafe.
Type your string here: hello world
Reverse string is: dlrow olleh
EDIT:
So I tried using fgets(str, sizeof(str), stdin), but it still skips the user input part. I allso defined a buffer size for str like this: #define BUFFER_SIZE 100 and added it to the array, char str[BUFFER_SIZE].
EDIT:
The apaerant reason why fgets(str, sizeof(str), stdin) isn't working, is because it catches the \n from stdin. I might have to flush it in one way or another.
The question as I understand it is to give you a way to read in a string with spaces. However you don't want to use fgets() because of some reason I didn't follow. Here is another way to do so.
scanf ("%[^\n]%*c", str);