Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Simple program. Strange results.
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 24th April 22:20
kenton w. mellott
External User
 
Posts: 1
Default Simple program. Strange results.



When compiling the following program with gcc (no switches) almost all the
input variable end up displaying strange results.


#include <stdio.h>

int scanf(const char *format, ...);

int main()
{
puts("Please enter a string.");
char buf[100];
scanf("%s", buf);
printf("you just entered: ''%s''. \n", buf );


puts("Please enter a floating point number.");
float x,y;
scanf("%f", !x);
printf("you just entered: '%g'. \n", !x );


puts("Please enter 2 floating point numbers and a string.");
scanf("%f %f %s", !x, !y, buf);
printf("you just entered: '%g','%g', '%s'.\n",
!x, !y, buf);


}


Sincerely,

Gregory D. MELLOTT
  Reply With Quote


  sponsored links


2 24th April 22:20
martin ambuhl
External User
 
Posts: 1
Default Simple program. Strange results.



Don't do this. Trust <stdio.h> to have the correct prototype.


^^^
The '!' is wrong. You mean `scanf("%f", &x);'

^^^
The '!' is wrong. You mean `printf("you just entered: '%g'.\n", x);

As before, all the '!'s are wrong.


Please use spaces instead of tabs when preparing code for posting. You can
see above what a mess you got.

Here's a version that is better formatted, will compile under either C89 or
C99 (as well as gnu89 and gnu99), and takes "enter a string" more seriously:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
char buf[100], *nl;
float x, y;
int nchar;
puts("Please enter a string.");
fgets(buf, sizeof buf, stdin);
if ((nl = strchr(buf, '\n')))
*nl = 0;
printf("you just entered: \"%s\".\n", buf);


puts("Please enter a floating point number.");
fgets(buf, sizeof buf, stdin);
sscanf(buf, "%f", &x);
printf("you just entered: '%g'.\n", x);


puts("Please enter 2 floating point numbers and a string.");
fgets(buf, sizeof buf, stdin);
if ((nl = strchr(buf, '\n')))
*nl = 0;
sscanf(buf, "%f %f %n", &x, &y, &nchar);
printf("you just entered: '%g','%g', \"%s\".\n", x, y, buf + nchar);
return 0;
}

--
Martin Ambuhl
  Reply With Quote
3 24th April 22:20
kenton w. mellott
External User
 
Posts: 1
Default Simple program. Strange results.


Thanks for the corrections! DJGPP's help gave no reason for the ampersand.
But if I had be wise enough to read the tutorial for scanf I had already
downloaded for 'The Art of Computer Programming'; I would have noted that C
uses it to express passing the address for the variable, instead of the
variable contents itself.

From what I can grasp, the asterisk symbol means the same thing. Though I
have yet to really grasp the proper place(s) and form(s) for its usage.

Sincerely,


Gregory D. MELLOTT
  Reply With Quote
4 24th April 22:20
dj delorie
External User
 
Posts: 1
Default Simple program. Strange results.


The very first line in the scanf do***entation is:

"This function scans formatted text from `stdin' and stores it in the
variables pointed to by the arguments."

The "pointed to by the arguments" should have indicated that the
arguments are pointers, not values.

In addition, the examples show:

scanf("%d %d %s", &x, &y, buf);

But, if you can suggest a more obvious wording, we'll listen.

Nope, asterisk is the inverse of ampersand. Ampersand takes the
address of something, converting values into pointers that point to
those values. Asterisks dereference pointers, turning pointers into
the values they point to.
  Reply With Quote
5 24th April 22:20
eplmst
External User
 
Posts: 1
Default Simple program. Strange results.


: > Though I have yet to really grasp the proper place(s) and form(s)
: > for its usage.

: Nope, asterisk is the inverse of ampersand. Ampersand takes the
: address of something, converting values into pointers that point to
: those values. Asterisks dereference pointers, turning pointers into
: the values they point to.

Except when you declare/define variables and functions. Then * takes
the place where newbies would expect &.

Then comes C++ where & is allowed in declarations/definitions too
(with some semantics I've never bothered to look up; I don't do C++).


Right,

MartinS
  Reply With Quote
6 24th April 22:21
kenton w. mellott
External User
 
Posts: 1
Default Simple program. Strange results.


Compiles fine and runs as described.

Perhaps you can help me grasp the trouble I'm having understanding and using
some previously suggested code.
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
char buf[100], *nl;
float x, y;
int nchar;
puts("Please enter a string.");
fgets(buf, sizeof buf, stdin);
if ((nl = strchr(buf, '\n')))
-> *nl = 0;
I read this as: if strchr makes nl anything but zero then make it equal to
zero.
Why? What other folloing statement uses it?
Also, why us a pointer to nl used, instead of plain nl.
printf("you just entered: \"%s\".\n", buf);


puts("Please enter a floating point number.");
fgets(buf, sizeof buf, stdin);
sscanf(buf, "%f", &x);
printf("you just entered: '%g'.\n", x);


puts("Please enter 2 floating point numbers and a string.");
fgets(buf, sizeof buf, stdin);
if ((nl = strchr(buf, '\n'))) *nl = 0;

The program errors somewhere in here if I enter to few values.
Is there a way to capture the error and warn and let the user retry?

printf("you just entered: '%g','%g', \"%s\".\n", x, y, buf + nchar);
return 0;
}
  Reply With Quote
7 24th April 22:21
hans-bernhard broeker
External User
 
Posts: 1
Default Simple program. Strange results.


Some of us could, but I think you really should find a better place
for this kind of questions. There's a special newsgroup for beginners
learning C or C++ programming in the 'alt' hierarchy:
alt.comp.lang.learn.c-c++, which is much better suited to this kind of
question.

Or just get yourself a good textbook and learn it.

No. Note how one assignment is made to 'n', the other to '*n'? That discinction is _important_.

It's not a 'pointer to nl'. 'nl' is a 'pointer to something'.
--
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666