Mail Archives: djgpp/2003/08/28/21:45:19
"Richard Dawe" <rich AT phekda DOT freeserve DOT co DOT uk> wrote in message
news:3F4E8FD4 DOT AD6096D8 AT phekda DOT freeserve DOT co DOT uk...
> Hello.
>
>
> & is used to indicate that a variable is passed by reference in
> declarations/definitions in C++. E.g.:
>
> ---Start test.cpp---
> #include <cstdio>
> #include <cstdlib>
>
> using namespace std;
>
> void
> blargh (int& ref)
> {
> ref = 12;
> }
>
> int
> main (void)
> {
> int a = 5;
>
> blargh(a);
> printf("%d\n", a);
> return EXIT_SUCCESS;
> }
> ---End test.cpp---
>
> No idea if that will compile. But if it does, it should print 12 when it
runs.
> blargh modifies the int passed by reference.
>
> I like C++'s Standard Template Library (STL) - type-safe generic container
> classes. Saves you having to do generic list classes with macros in C,
which
> is just horrible. (I strongly dislike C macros most of the time.)
>
> Bye, Rich =]
>
> --
> Richard Dawe [ http://www.phekda.freeserve.co.uk/richdawe/ ]
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;
> sscanf(buf, "%f %f %n", &x, &y, &nchar);
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;
}
- Raw text -