Input problem

L

Les Coover

Consider this source code

/* str_test */

#include <stdio.h> /* standard header */

int main(void)
{
int id, number;
char in_string[]= "2 blue 6";
char out_string[100], tmp[100];

sscanf(in_string, "%d %s %d", &id, tmp, &number);
sprintf(out_string, "%d %s %d \n", id, tmp, number);
printf("%s", out_string);


return (0);
}

When compiled and executed the screen output is 2 blue 6

But how can I enter 2 blue 6 FROM THE KEYBOARD and have the program
give the same output?

Thanks,

Les
 
A

Aaron Isotton

Consider this source code
[snip]

sscanf(in_string, "%d %s %d", &id, tmp, &number);
sprintf(out_string, "%d %s %d \n", id, tmp, number);
printf("%s", out_string);
[snip]

When compiled and executed the screen output is 2 blue 6

But how can I enter 2 blue 6 FROM THE KEYBOARD and have the program
give the same output?

scanf("%d %s %d", &id, tmp, &number);

might be a solution. In real world applications, you should use something
like fgets and parse the inputted string in a more "manual" way because it
is a pain to detect all cases of bad input using scanf.
 
S

Simon Biber

Les Coover said:
Consider this source code

/* str_test */

#include <stdio.h> /* standard header */

int main(void)
{
int id, number;
char in_string[]= "2 blue 6";
char out_string[100], tmp[100];

sscanf(in_string, "%d %s %d", &id, tmp, &number);
sprintf(out_string, "%d %s %d \n", id, tmp, number);

Why do you have a space between the 'd' and the '\n'?
printf("%s", out_string);

return (0);

Unnecessary parentheses.
}

When compiled and executed the screen output is 2 blue 6

But how can I enter 2 blue 6 FROM THE KEYBOARD and have the program
give the same output?

Remove the definitions of in_string and out_string, replace
sscanf and sprintf with scanf and printf respectively and remove
their first arguments, then remove the last printf statement.

For safety, replace the %s in scanf with %99s to limit the
number of characters read to the length of the 'tmp' buffer
provided.
 
L

Les Coover

Thanks Simon!

Les

Simon Biber said:
Les Coover said:
Consider this source code

/* str_test */

#include <stdio.h> /* standard header */

int main(void)
{
int id, number;
char in_string[]= "2 blue 6";
char out_string[100], tmp[100];

sscanf(in_string, "%d %s %d", &id, tmp, &number);
sprintf(out_string, "%d %s %d \n", id, tmp, number);

Why do you have a space between the 'd' and the '\n'?
printf("%s", out_string);

return (0);

Unnecessary parentheses.
}

When compiled and executed the screen output is 2 blue 6

But how can I enter 2 blue 6 FROM THE KEYBOARD and have the program
give the same output?

Remove the definitions of in_string and out_string, replace
sscanf and sprintf with scanf and printf respectively and remove
their first arguments, then remove the last printf statement.

For safety, replace the %s in scanf with %99s to limit the
number of characters read to the length of the 'tmp' buffer
provided.
 
R

Richard Heathfield

Les said:
Consider this source code

/* str_test */

#include <stdio.h> /* standard header */

int main(void)
{
int id, number;
char in_string[]= "2 blue 6";
char out_string[100], tmp[100];

sscanf(in_string, "%d %s %d", &id, tmp, &number);
sprintf(out_string, "%d %s %d \n", id, tmp, number);
printf("%s", out_string);


return (0);
}

When compiled and executed the screen output is 2 blue 6

But how can I enter 2 blue 6 FROM THE KEYBOARD and have the program
give the same output?

Two obvious ways spring to mind: fgets (to get the string) followed by
sscanf as above, or fscanf (instead of sscanf). Use stdin as your stream
argument - either the third arg to fgets, or the first arg to fscanf.

Don't forget to check the return values, every time.
 
M

Mick

Richard Heathfield said:
Les Coover wrote:

Hi, i'm a c beginner but frfrom ur question it seems u need to use the
fgets function to read input from stdin, ,which is the keyboard.
Look at this :

char buf[BUFSIZ];
while( (fgets(buf,BUFSIZ, stdin)!=NULL) )

What the first line does is, it create and assigned BUFSIZ to the
amount of character you are reading in from the keyboard.You can read
as many as you wnat up tp BUFSIZ which is the maximum length anyways.
So now the next line is saying, as long as there are characters to
read from the stdin(keyboard), and you are reading into buf which has
the maximum size of BUFSIZE, then it checks to see if you are not at
the end of line ('\o'), then you have to follow up with the remnants
of your codes in the while loop.

Get back if you find it confusing...
(e-mail address removed) is my address.
Consider this source code

/* str_test */

#include <stdio.h> /* standard header */

int main(void)
{
int id, number;
char in_string[]= "2 blue 6";
char out_string[100], tmp[100];

sscanf(in_string, "%d %s %d", &id, tmp, &number);
sprintf(out_string, "%d %s %d \n", id, tmp, number);
printf("%s", out_string);


return (0);
}

When compiled and executed the screen output is 2 blue 6

But how can I enter 2 blue 6 FROM THE KEYBOARD and have the program
give the same output?

Two obvious ways spring to mind: fgets (to get the string) followed by
sscanf as above, or fscanf (instead of sscanf). Use stdin as your stream
argument - either the third arg to fgets, or the first arg to fscanf.

Don't forget to check the return values, every time.
 
S

Steve Zimmerman

Les said:
Consider this source code

/* str_test */

#include <stdio.h> /* standard header */

int main(void)
{
int id, number;
char in_string[]= "2 blue 6";
char out_string[100], tmp[100];

sscanf(in_string, "%d %s %d", &id, tmp, &number);
sprintf(out_string, "%d %s %d \n", id, tmp, number);
printf("%s", out_string);


return (0);
}

When compiled and executed the screen output is 2 blue 6

But how can I enter 2 blue 6 FROM THE KEYBOARD and have the program
give the same output?

Thanks,

Les

Thank you for your post, Les.


This program is from p. 17 of _The C Programming Language_,
second edition, by Dennis Ritchie and Brian Kernighan.

#include <stdio.h>

/* copy input to output */
int main()
{
int c;

while ((c = getchar()) != EOF)
putchar(c);

return 0;
}

Input from keyboard: I like C. <enter>
Output to screen: I like C.

--Steve
 
J

Joona I Palaste

Hi, i'm a c beginner but frfrom ur question it seems u need to use the
fgets function to read input from stdin, ,which is the keyboard.

First, write in English, not in d00dsp33k. Real people say "you", not
"u".
Second, whoever said stdin *had* to come from the keyboard? Whoever
said you *had* to even have a keyboard?
Look at this :
char buf[BUFSIZ];
while( (fgets(buf,BUFSIZ, stdin)!=NULL) )

fgets(buf, sizeof buf, stdin) would be better.

PS. Please don't top-post, thanks.
 
R

Richard Heathfield

Mick said:
Hi, i'm a c beginner but frfrom ur question it seems u need to use the
fgets function to read input from stdin, ,which is the keyboard.

Hi, C beginner. Whilst it is true that stdin is often associated with some
kind of keyboard device, the Standard does not require this, and exceptions
are commonplace.

Look at this :

char buf[BUFSIZ];
while( (fgets(buf,BUFSIZ, stdin)!=NULL) )

Better: while(fgets(buf, sizeof buf, stdin) != NULL)

Doing it this way drops a set of redundant parentheses, and removes an
unnecessary dependency of one line on another.
What the first line does is, it create and assigned BUFSIZ to the
amount of character you are reading in from the keyboard.

No, it doesn't. It defines an array BUFSIZ bytes in size.
You can read
as many as you wnat up tp BUFSIZ which is the maximum length anyways.

What makes you think that?
So now the next line is saying, as long as there are characters to
read from the stdin(keyboard), and you are reading into buf which has
the maximum size of BUFSIZE,

Where did BUFSIZE come from? Did you mean BUFSIZ?
then it checks to see if you are not at
the end of line ('\o'),

In C, the newline character is '\n', not '\o'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,082
Messages
2,570,588
Members
47,209
Latest member
Ingeborg61

Latest Threads

Top