Am I doing it right?
Jun 27, 2015 at 6:09pm UTC
I want to go back from the class to main.
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <iostream>
#include <string>
#include <fstream>
#include <limits>
#include "fcreate.h"
std::fstream& GotoLine(std::fstream& file, unsigned int num){
file.seekg(std::ios::beg);
for (int i=0; i < num - 1; ++i){
file.ignore(std::numeric_limits<std::streamsize>::max(),'\n' );
}
return file;
}
main()
{
std::string filename, trash, mname;
int hp, power;
char yORn;
std::cout << "Do you have a file for your datamonster yet (Y/N)? " ;
std::cin >> yORn;
if (yORn == 'n' || yORn == 'N' )
{
fcreate fobject;
}
else if (yORn == 'Y' || yORn == 'y' )
std::cout << "Enter filename (Case senstive): " ;
std::cin >> filename;
std::fstream monster(filename.c_str());
monster >> trash >> mname;
GotoLine(monster, 2);
monster >> trash >> hp;
GotoLine(monster, 3);
monster >> trash >> power;
monster.close();
std::cout << mname << " has " << hp << " hp" << std::endl << "and " << power << " power" << std::endl << std::endl;
std::cout << "Do you want to do another datamonster (Y/N)? " ;
std::cin >> yORn;
if (yORn == 'y' || yORn == 'Y' )
{
fcreate fobject;
}
return 0;
}
Write your question here.
fcreate.h
1 2 3 4 5 6 7 8 9 10 11
#ifndef FCREATE_H
#define FCREATE_H
class fcreate
{
public :
fcreate();
};
#endif // FCREATE_H
fcreate.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
#include "fcreate.h"
#include <fstream>
#include <string>
fcreate::fcreate()
{
std::string filename, name;
int hp, power, done;
std::cout << "Your datamonster needs some data. We save the data" << std::endl
<< "inside a file enter filename: " ;
std::cin >> filename;
std::ofstream monster;
monster.open (filename.c_str());
std::cout << "What will the monsters name be: " ;
std::cin >> name;
monster << "name: " << name << std::endl;
std::cout << "How much hp does " << name << " have? " ;
std::cin >> hp;
monster << "hp: " << hp << std::endl;
std::cout << "How much power(pwr) does " << name << " have? " ;
std::cin >> power;
monster << "power: " << power;
}
Last edited on Jun 27, 2015 at 6:09pm UTC
Jun 27, 2015 at 8:07pm UTC
you have a couple of issues in your code.
Your main function should be of type int.
you have
1 2 3 4 5
main()
...
return 0;
it should be
1 2 3 4 5
int main()
....
return 0;
then you class header (.h) file should have a function prototype for each function in your implementation (.cpp) file and it appears you are using your class constructor to build your class functions, where you constructor should help you build your class object.
your class (.h) file should look like this (based on the data you provided )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#ifndef __cplusplus__dataMonster__
#define __cplusplus__dataMonster__
#include <iostream>
class DataMonster {
private :
std::string filename, name;
int hp, power, done;
public :
void setFilename();
void setName();
void setHP();
void setPower();
void setDone();
std::string getFilename();
std::string getName();
int getHP();
int getPower();
int getDone();
};
#endif /* defined(__cplusplus__dataMonster__) */
I will leave it up to you to write your cpp file. Take a look at
http://www.cplusplus.com/doc/tutorial/classes/ if you have further questions or feel free to ask here!
Last edited on Jun 27, 2015 at 8:43pm UTC
Jun 28, 2015 at 12:29pm UTC
Thanks, Bdanielz.
Topic archived. No new replies allowed.