On (28 May 97) Steven Dyck wrote to All...
SD> Hello. I am new to this area. I have worked with C++ for almost a
SD> year, but have done nothing serious with it (a few games). I have
SD> three question to whoever can answer them.
SD> 1. I have this program that always gives me a warning and I was
SD> wondering if there was a way to get rid of the warning.
SD> #include
SD> main()
SD> {
SD> unsigned char Monster_Name;
SD> Monster_Name = "Bear";
SD> clrscr();
SD> printf("%s", Monster_Name);
SD> getch();
SD> return(0);
SD> }
SD> The warning I get is this :
SD> Warning Nonportable pointer conversion in function main
SD> Any ideas how to get rid of this? I know it doesn't hurt the program,
SD> but when you have 20 or so of them, it gets anoying.
This has a couple of problems, some severe enough that it won't actually
compile as C++ at all. A working version would look like this:
#include
#include // contains declarations of clrscr() and getch().
main()
{
// Note that added `*' here, which makes it a pointer instead of a
// single char. Also note that we've initialized it instead of
// assigning to it after it's created.
//
char *Monster_Name = "Bear";
clrscr();
printf("%s", Monster_Name);
getch();
return(0);
}
SD> My next question is how do you use dos commands in C++. I mean if I
SD> wanted to make a program that could show all the files in a certain
SD> directory, or make directories, etc. how would I do this?
Two possible methods. The cheap and sleazy method would be:
system("dir");
which simply spawns a copy of command.com and has it execute a `dir'
command.
The more difficult method would be to use _dos_findfirst and
_dos_findnext to find all the files in the directory and list them out
as you wish. The advantage here is that you get real control over how
things are done.
SD> My last question is this. How do you save to a seperate file (name of
SD> person, age, other information), and how do you retrieve this
SD> information in the same program?
This can get pretty long and complex, so I'll stick to a relatively
simplified version for the moment:
#include
int main(void) {
// open a file named `people.dat' for output.
ostream x("people.dat");
// Write my name and age to it...
x << "Jerry Coffin" << endl << "very old" << endl;
// we're finished writing - close it up.
x.close();
// Now open `people.dat' for input.
istream y("people.dat");
// This is where we'll store the data as it's read from the file.
char buffer[256];
// Read in the name.
y.readline(buffer, 256);
// and print it out to the screen.
cout << "Name: " << buffer << endl;
// Now read the age:
y.readline(buffer, 256);
// and display it:
cout << "\tAge: " << buffer << endl;
// And we're done. The file will be closed automatically.
return 0;
}
Later,
Jerry.
... The Universe is a figment of its own imagination.
--- PPoint 1.90
---------------
* Origin: Point Pointedly Pointless (1:128/166.5)
|