473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

gets() problem

I have this global variable:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. typedef struct{
  3.     int numero;
  4.     char nome[200];
  5.     char morada[200];
  6.     int contacto;
  7. } Cliente;
  8. Cliente clientes[2000];
  9. ...
  10.  
and this part of code:
Expand|Select|Wrap|Line Numbers
  1. void adiccionarCliente(){
  2.     char nomeCliente[200];
  3.     char moradaCliente[200];
  4.     int contactoCliente=0;
  5.  
  6.  
  7.     printf("Introduza o nome do cliente:\n");
  8.     gets(nomeCliente);
  9.     strcpy(clientes[numeroCliente].nome, nomeCliente);
  10. ....
  11. }
  12.  
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.
Jan 10 '08 #1
12 1783
Savage
1,764 Expert 1GB
It jumps both printf and gets?
Have you tried flushing the streams?
Jan 10 '08 #2
It jumps both printf and gets?
Have you tried flushing the streams?
only skips gets...and yes same problem even with flush
Jan 10 '08 #3
I have this after:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. printf("Introduza a morada do cliente:\n");
  3.     gets(moradaCliente);
  4.     strcpy(clientes[numeroCliente].morada, moradaCliente);
  5. ...
  6.  
and it works...
Jan 10 '08 #4
oler1s
671 Expert 512MB
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.
Jan 10 '08 #5
Expand|Select|Wrap|Line Numbers
  1. ...
  2. printf("Introduza o nome do cliente:\n");
  3.     fgets(nomeCliente,200,stdin);
  4.     strcpy(clientes[numeroCliente].nome, nomeCliente);
  5. ...
  6.  
same problem
Jan 10 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Jan 10 '08 #7
Expand|Select|Wrap|Line Numbers
  1. void menuPrincipal(){
  2.     int opcao=0;
  3.     printf("Escolha uma opccao:\n");
  4.     printf("1 Atender Cliente\n");
  5.     printf("2 Adiccionar Cliente\n");
  6.     printf("3 Editar Cliente\n");
  7.     printf("4 Estatisticas\n");
  8.     printf("0 Sair\n");
  9.     do {
  10.         scanf("%i",&opcao);
  11.         switch (opcao){
  12.             case 1:
  13.                 atenderCliente();
  14.                 break;
  15.             case 2:
  16.                 adiccionarCliente();
  17.                 break;
  18.             case 3:
  19.                 editarCliente();
  20.                 break;
  21.             /*case 4:
  22.                 estatisticas();*/
  23.             case 0:
  24.                 return;
  25.                 break;
  26.             default:
  27.                 printf("Erro: a opcao escolhida nao existe!\n");
  28.                 printf("Escolha uma opcao:\n");
  29.                 break;
  30.         }
  31.     } while(opcao<0||opcao>4);
  32.     return;
  33. }
  34.  
Jan 10 '08 #8
Savage
1,764 Expert 1GB
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?
Jan 10 '08 #9
oler1s
671 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. int ch = 0;
  2. while((ch = getc(stdin)) != EOF && ch != '\n');
  3.  
That is how you “flush” stdin.
Jan 10 '08 #10
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
Jan 10 '08 #11
sicarie
4,677 Expert Mod 4TB
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).
Jan 10 '08 #12
mikh
3
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:
Expand|Select|Wrap|Line Numbers
  1. int ch = 0;
  2. while((ch = getc(stdin)) != EOF && ch != '\n');
  3.  
That is how you “flush” stdin.

You are my hero! Now, why didn't I think about that earlier? Thank you soooooo much :-*
Jan 12 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
8
by: Bartek | last post by:
Compiler: g++ code: char data; int e; //.. gets(data); // ..
32
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...
14
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;...
39
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...
302
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...
89
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...
34
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...
280
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...
104
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.
0
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,...
0
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$) { } ...
0
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...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.