geting input in C, problem in getting input

an infinite loop asking input, storing it in an array and showing it

Page 1 of 1

5 Replies - 1002 Views - Last Post: 18 April 2009 - 04:42 AM Rate Topic: -----

#1 BujarM   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-February 07

geting input in C, problem in getting input

Posted 17 April 2009 - 04:25 PM

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

int main(int argc, char *argv[])
{
	while(1)
	{
	char *client_balance[]={"20000", "18000", "16000", "14000", "12000"};
	int client_num=2;
	char buf[6];
	start:
	system("PAUSE");	
	printf("Please type the new PIN:");
	fgets(buf,7,stdin);
	printf("%s\n", buf);
	client_balance[client_num]=buf;
	printf("%s\n", client_balance[client_num]);
	}
   
  system("PAUSE");	
  return 0;
}



My Input:
PIN: 123456789
Output:
123456
123456

then it does not wait for my input, it uses the "789" leftover from before,
How to fix this bug? Make it ask me for input each time and use the 6 digits i enter only?

thanks in advance

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: geting input in C, problem in getting input

#2 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,326
  • Joined: 14-February 08

Re: geting input in C, problem in getting input

Posted 17 April 2009 - 04:41 PM

Hi

fgets() reads characters into str until num-1 characters or end of file

fgets( char * str, int num, FILE * stream ) 



In your example you are only using 7 as your value for num which is 6 characters.

Am I understanding you wrong in that you want it to throw an error that you used extra numbers?
Was This Post Helpful? 0
  • +
  • -

#3 BujarM   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-February 07

Re: geting input in C, problem in getting input

Posted 17 April 2009 - 04:45 PM

hi,
i want the extra numbers to be discarded,
i.e
if first time i entered 123456789
i want before and after to have the first 6 digits
and next time, to ask for my iput again, so that i can enter new numbers

This post has been edited by BujarM: 17 April 2009 - 04:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: geting input in C, problem in getting input

Posted 17 April 2009 - 06:43 PM

View PostBujarM, on 17 Apr, 2009 - 03:45 PM, said:

hi,
i want the extra numbers to be discarded,
i.e
if first time i entered 123456789
i want before and after to have the first 6 digits
and next time, to ask for my iput again, so that i can enter new numbers


Not 100% sure what that means.
So if the user enters more than 6 characters you want to use the first 6 and discard the rest.
Is that right?
Was This Post Helpful? 0
  • +
  • -

#5 BujarM   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 06-February 07

Re: geting input in C, problem in getting input

Posted 18 April 2009 - 04:19 AM

View Postjanotte, on 17 Apr, 2009 - 05:43 PM, said:

View PostBujarM, on 17 Apr, 2009 - 03:45 PM, said:

hi,
i want the extra numbers to be discarded,
i.e
if first time i entered 123456789
i want before and after to have the first 6 digits
and next time, to ask for my iput again, so that i can enter new numbers


Not 100% sure what that means.
So if the user enters more than 6 characters you want to use the first 6 and discard the rest.
Is that right?


yes
Was This Post Helpful? 0
  • +
  • -

#6 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: geting input in C, problem in getting input

Posted 18 April 2009 - 04:42 AM

Cool. Just wanted to make sure we weren't giving you useless advice.

There are a few options. Here's a couple:
1 - create a new character array called something like "junk" which is 255 chars long and after you get the first 6 characters into "buf" make another call to fgets() to read any junk characters into "junk" and do nothing with them.
2 - use fread() to read in the whole input (say 255 characters) and then extract a substring of the first 6 characters and use that.

Read more here:
http://www.cplusplus...library/cstdio/

This post has been edited by janotte: 18 April 2009 - 05:13 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1