Groovy hepcat Cyber Con jived with All on 26 Jan 98 11:08:07!
C++ Saveding stuff's a cool scene. Dig it!
CC> ok i know how you have some one enter a number
CC> scanf , but will this work for words.
Yuck! scanf(), ICK! Bad medicine, kimosabe. scanf() is a C function
that even C programmers hate with a passion. It harbours many traps
that even the most experienced programmers find dificult to navigate,
let alone a new learner.
In C there are much better functions to read a line of input, such
as fgets(), but since this is the C++ echo, a more C++ aproach is to
use the iostream stuff.
CC> input "Enter Name " , name$ 'Note this is Qbasic code and i see it in
CC> c++
CC> if name$ = "Cyber Con" then print "Hi Cyber Con"
CC> open "file1.fil" as 1
CC> put 1, name$
CC> Close
CC> END
OK, basically you want to enter a name, compare the entered name to
your name and display a message if it is your name, then open a file
and put the name in it. Have I got that right? I haven't left anithing
out? Well, that should be pretty easy (but keep in mind that I know
about C, and know little of C++, so if I make a mistake I'm sorry):
#include
#include
#include
#include
int main(void)
{
char name[30];
char *yourname = "Cyber Con";
char *fname = "file1.fil";
FILE *file;
cout << "Enter name: ";
cin >> name; /* get the name */
if(strcmp(name, yourname) == 0) /* if name is yourname... */
cout << "Hi, " << name << endl; /* ...print a message */
if(NULL == (file = fopen(fname, "w"))) /* open file (error checking) */
{
cerr << "Cannot open file: " << fname << endl;
return EXIT_FAILURE;
}
fputs(name, file); /* put name in file */
return 0;
}
CC> //' next program
CC> open "file1.fil" as 1
CC> get 1, name$
CC> print name$
CC> END
OK, now you want to open the file created (or used) in the previous
program, read the name from the file, then display that name, right?
That's easy:
#include
#include
#include
#include
int main(void)
{
char name[30];
char *fname = "file1.fil";
FILE *file;
if(NULL == (file = fopen(fname, "r"))) /* open file (error checking) */
{
cerr << "Could not open file: " << fname << endl;
return EXIT_FAILURE;
}
fgets(name, file, 30); /* get name from file */
cout << name << endl; /* print name */
return 0;
}
This works, except for one thing. cin only reads up to the first
whitespace. What this means is that if you enter "Cyber Con", it will
only read "Cyber". With my limited knowledge of C++, I'll leave it to
the experts to tell you how to deal with this. But, a simple solution
that comes from C is to simply use fgets() to enter the name. So you
could rewrite the first program thusly:
#include
#include
#include
#include
int main(void)
{
char name[30];
char *yourname = "Cyber Con";
char *fname = "file1.fil";
FILE *file;
cout << "Enter name: ";
fgets(name, 30, stdin); /* here's the change */
if(strcmp(name, yourname) == 0)
cout << "Hi, " << name << endl;
if(NULL == (file = fopen(fname, "w")))
{
cerr << "Cannot open file: " << fname << endl;
return EXIT_FAILURE;
}
fputs(name, file);
return 0;
}
Wolvaen
... That's a common language on micros, said Tom basically.
--- Blue Wave/RA v2.20
---------------
* Origin: The Gate, Melbourne Australia, +61-3-9809-5097 33.6k (3:633/159)
|