TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: STEVEN DYCK
from: CLIFF RHODES
date: 1997-05-29 18:42:00
subject: Three questions

--> Steven Dyck wrote to All  <--
 
SD>main()
SD>{
SD>unsigned char Monster_Name;
SD>Monster_Name = "Bear";
 
"Bear" is a string literal which is viewed by the compiler as a
pointer to char. You have typed Monster_Name as a char--but not a
pointer.
 
unsigned char *Monster_Name = "Bear";
 
will do the trick. Monster_Name is now a pointer to a char.
 
SD>The warning I get is this :
SD>Warning Nonportable pointer conversion in function main
 
That's because the types of Monster_Name and "Bear" were not the same.
 
SD>My next question is how do you use dos commands in C++.  I
SD>mean if I wanted to make a program that could show all the
SD>files in a certain directory, or make directories, etc. how
SD>would I do this?
 
The easiest way is to use the system() function. Look it up in your
documentation.
 
SD>My last question is this.  How do you save to a seperate
SD>file (name of person, age, other information), and how do
SD>you retrieve this information in the same program?
 
Let's use C++ for this:
 
#include 
#include 
#include 
 
struct Person {
  char name[36];
  int  age;
  int  weight;
};
 
int main(void)
{
  const char *fname = "PERSON.DAT";  // Filename we'll use
 
  Person aPerson;  // Create a person
 
  // Give the person an identity...
  strcpy(aPerson.name, "Joe Blow");
  aPerson.age = 21;
  aPerson.weight = 175;
 
  ofstream outfile(fname, ios::binary); // Open the file for writing
  if(!outfile)
  {
     cerr << "Couldn't open data file for writing" << endl;
     return 1;
  }
  // Write the struct to the opened file
  // Notice the cast of &aPerson to const char *
  outfile.write((const char *) &aPerson, sizeof(Person));
  outfile.close(); // Close the stream
 
  Person bPerson;   // Create another person without any identity
 
  ifstream infile(fname, ios::binary);   // Open the file for reading
  if(!infile)
  {
     cerr << "Couldn't open data file for reading" << endl;
     return 1;
  }
  // Read in the data previously stored in the file
  // Notice the cast of &bPerson to char *. I used the new C++ style
  // cast here, but you could use the old C way as shown above, too.
  infile.read(reinterpret_cast(&bPerson), sizeof(Person));
  infile.close(); // Close the stream
  // Display the data read to see if it is the same as written... 
  cout << "bPerson.name: " << bPerson.name << '\n';
  cout << "bPerson.age:  " << bPerson.age << endl;
 
  return 0;
}
Cliff Rhodes
cliff.rhodes@juge.com
 
X CMPQwk 1.42 1692 XMental Floss prevents Moral Decay.
--- Maximus/2 3.01
---------------
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.