I have this global variable: -
...
-
typedef struct{
-
int numero;
-
char nome[200];
-
char morada[200];
-
int contacto;
-
} Cliente;
-
Cliente clientes[2000];
-
...
-
and this part of code: -
void adiccionarCliente(){
-
char nomeCliente[200];
-
char moradaCliente[200];
-
int contactoCliente=0;
-
-
-
printf("Introduza o nome do cliente:\n");
-
gets(nomeCliente);
-
strcpy(clientes[numeroCliente].nome, nomeCliente);
-
....
-
}
-
But when i run the program it just skips that gets(nomeCliente) and dont ask me for a nomeCliente.
Sorry about my variable aren't in english.
If you could help i would be gratefull.
Thank you, Barbosa.
12 1783
It jumps both printf and gets?
Have you tried flushing the streams?
It jumps both printf and gets?
Have you tried flushing the streams?
only skips gets...and yes same problem even with flush
I have this after: -
...
-
printf("Introduza a morada do cliente:\n");
-
gets(moradaCliente);
-
strcpy(clientes[numeroCliente].morada, moradaCliente);
-
...
-
and it works...
Do not use gets. Use fgets.
gets is kept around for historical uses, but it has been marked in documentation and on numerous forums as a "Do not use under any circumstances".
gets has absolutely no bounds checking on input.
-
...
-
printf("Introduza o nome do cliente:\n");
-
fgets(nomeCliente,200,stdin);
-
strcpy(clientes[numeroCliente].nome, nomeCliente);
-
...
-
same problem
Can you post the code that calls adiccionarCliente() ??.
I want to be sure you are calling the function in the first place because I compiled and ran your code using Visual Studio.NET 2005 and it worked.
-
void menuPrincipal(){
-
int opcao=0;
-
printf("Escolha uma opccao:\n");
-
printf("1 Atender Cliente\n");
-
printf("2 Adiccionar Cliente\n");
-
printf("3 Editar Cliente\n");
-
printf("4 Estatisticas\n");
-
printf("0 Sair\n");
-
do {
-
scanf("%i",&opcao);
-
switch (opcao){
-
case 1:
-
atenderCliente();
-
break;
-
case 2:
-
adiccionarCliente();
-
break;
-
case 3:
-
editarCliente();
-
break;
-
/*case 4:
-
estatisticas();*/
-
case 0:
-
return;
-
break;
-
default:
-
printf("Erro: a opcao escolhida nao existe!\n");
-
printf("Escolha uma opcao:\n");
-
break;
-
}
-
} while(opcao<0||opcao>4);
-
return;
-
}
-
Both of these functions(gets and fgets) on error or EOF returns NULL.Have you checked to see whether this functions passes or not?
Also,I'm not suggesting that following is a good solution but it might work:
You said that next call to gets worked,so have you tried to put another call to gets(or fgets) after that one?
Your code confirms my suspicions. Let’s work backwards, to see how one might diagnose the problem. Remember that information about the various C functions can be found in documentation, easily googled. Problem: fgets seems to be skipped.
What does fgets do? It extracts from the file stream (in your case stdin) until it reaches a newline. Then it returns. So perhaps fgets isn’t being skipped, but is returning right away. Why would it do that? Perhaps because there is something already in stdin. Since it’s returning right away, it stands to reason there is a newline left in stdin. So let’s work through our previous I/O operations and see what might involve putting a newline in stdin.
And we see scanf. How does scanf work? It takes input from the user. Then it attempts to parse the input into various tokens you specify. In your case, you want the user to type in a number. So let’s say I type in 4. Since I also hit the enter key, what we really have is 4\n. Now, scanf parses that input, and extracts 4. And it leaves everything else alone. So stdin now contains \n. And when you call fgets, it picks out the \n and returns immediately.
Wrong solution: fflush(stdin). Flushing is only defined for output streams. Not input.
Solution 1: Change your scanf to fgets, and manually convert it to an integer.
Solution 2: -
int ch = 0;
-
while((ch = getc(stdin)) != EOF && ch != '\n');
-
That is how you “flush” stdin.
Both of these functions(gets and fgets) on error or EOF returns NULL.Have you checked to see whether this functions passes or not?
Also,I'm not suggesting that following is a good solution but it might work:
You said that next call to gets worked,so have you tried to put another call to gets(or fgets) after that one?
That worked thank you Savage.
oler1s thanks too...but im too lazy to do that :P
oler1s thanks too...but im too lazy to do that :P
Goal in life to be featured in thedailywtf.com?
Otherwise, I'd say put the effort in now. Then you'll learn it, and it won't take effort, you'll know to design it that way instead of changing it later (or finding other odd errors later).
Your code confirms my suspicions. Let’s work backwards, to see how one might diagnose the problem. Remember that information about the various C functions can be found in documentation, easily googled. Problem: fgets seems to be skipped.
What does fgets do? It extracts from the file stream (in your case stdin) until it reaches a newline. Then it returns. So perhaps fgets isn’t being skipped, but is returning right away. Why would it do that? Perhaps because there is something already in stdin. Since it’s returning right away, it stands to reason there is a newline left in stdin. So let’s work through our previous I/O operations and see what might involve putting a newline in stdin.
And we see scanf. How does scanf work? It takes input from the user. Then it attempts to parse the input into various tokens you specify. In your case, you want the user to type in a number. So let’s say I type in 4. Since I also hit the enter key, what we really have is 4\n. Now, scanf parses that input, and extracts 4. And it leaves everything else alone. So stdin now contains \n. And when you call fgets, it picks out the \n and returns immediately.
Wrong solution: fflush(stdin). Flushing is only defined for output streams. Not input.
Solution 1: Change your scanf to fgets, and manually convert it to an integer.
Solution 2: -
int ch = 0;
-
while((ch = getc(stdin)) != EOF && ch != '\n');
-
That is how you “flush” stdin.
You are my hero! Now, why didn't I think about that earlier? Thank you soooooo much :-*
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ohaya |
last post by:
Hi,
We've been having a problem with one particular page that has a button
on it, and a "tall" image (top-to-bottom).
The button calls some simple Javascript to print the frame in which the...
|
by: Bartek |
last post by:
Compiler: g++
code:
char data;
int e;
//..
gets(data);
// ..
|
by: Marcus |
last post by:
We all know that the "gets" function from the Standard C Library (which
is part of the Standard C++ Library) is dangerous. It provides no
bounds check, so it's easy to overwrite memory when using...
|
by: jorntk |
last post by:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define MAX 256
#define CMD_MAX 10
char *valid_cmds = " ls ps df ";
int main (void) {
char line_input, the_cmd;...
|
by: Teh Charleh |
last post by:
OK I have 2 similar programmes, why does the first one work and the second does
not? Basically the problem is that the program seems to ignore the gets call if
it comes after a scanf call. Please...
|
by: Lee |
last post by:
Hi
Whenever I use the gets() function, the gnu c compiler gives a
warning that it is dangerous to use gets(). Is this due to the
possibility of array overflow? Is it correct that the program...
|
by: Cuthbert |
last post by:
After compiling the source code with gcc v.4.1.1, I got a warning
message:
"/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the
'gets' function is dangerous and should not be...
|
by: Zulander |
last post by:
Hi,
have a problem with comparing text in my function for some reason my
code get hang at fgets.
i am uploading this code in an IC, but i have isolated the problem to
fgets(SettingInfo);
could...
|
by: jacob navia |
last post by:
In the discussion group comp.std.c Mr Gwyn wrote:
< quote >
.... gets has been declared an obsolescent feature and
deprecated, as a direct result of my submitting a DR about it
(which...
|
by: jayapal |
last post by:
Hi all,
Whenever I use the gets() function, the gnu c compiler gives a
warning that it is dangerous to use gets(). why...?
regards,
jayapal.
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |