13 Replies - 512 Views - Last Post: 04 December 2009 - 03:59 PM Rate Topic: -----

#1 corbinc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 07-November 09

c++ storing information in a class

Post icon  Posted 04 December 2009 - 11:51 AM

How do i store the information gathered in my class, but gather the information in main()
when i do it this way it says "variable was not declared in this scope" but i thought the point of making them public, was so that you could have access to them outside th class

#include <iostream>
#include <string>

using namespace std;

class fitness
{

  public:
  string Fname, Lname;
  int age;
  double Mtime;
  char gender;

};

int main()
{
  cout << "Welcome to Corbins Fitness Calculator!" << endl;

  cout << "Enter your First Name: ";
  cin >> Fname;
  cout << endl;

  cout << "Enter your Last Name: ";
  cin >> Lname;
  cout << endl;

  cout << "Enter your Age: ";
  cin >> age;
  cout << endl;

  cout << "Enter your gender - M or F: ";
  cin >> gender;
  cout << endl;

  cout << "Enter the time it takes you to walk 3 miles - i.e. 15: ";
  cin >> Mtime;
  cout << endl;

  return 0;

} // end main


Is This A Good Question/Topic? 0
  • +

Replies To: c++ storing information in a class

#2 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: c++ storing information in a class

Posted 04 December 2009 - 11:54 AM

you need to create an object of your class, something like myFitnessObject = Fitness()
Was This Post Helpful? 0
  • +
  • -

#3 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: c++ storing information in a class

Posted 04 December 2009 - 11:58 AM

just write member functions like this:
class fitness
{
  string Fname, Lname;
public:
  int age;
  double Mtime;
  char gender;
  void set_fname(string &str);
  void set_lname(string &str);
};
Define these two member functions so that they assign the argument to the member variables. This way you have access to private members to assign value to them.

This post has been edited by Anarion: 04 December 2009 - 12:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 corbinc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 07-November 09

Re: c++ storing information in a class

Posted 04 December 2009 - 12:02 PM

Im not sure what either of you mean, could you give me an example in code?
Was This Post Helpful? 0
  • +
  • -

#5 GenHornet18  Icon User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

Re: c++ storing information in a class

Posted 04 December 2009 - 12:02 PM

fitness Fit1
Fit1.Fname


Was This Post Helpful? 0
  • +
  • -

#6 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: c++ storing information in a class

Posted 04 December 2009 - 12:05 PM

I edited my post, read it... here's a more complete code in case you need:
class fitness
{
  string Fname, Lname;
public:
  int age;
  double Mtime;
  char gender;
  void set_fname(string &str);
  void set_lname(string &str);
};

void fitness::set_fname(string &str) {
	Fname = str;
}
void fitness::set_lname(string &str) {
	Lname = str;
}

Now in your main, whenever you need to assign to Fname, just call like fitness_instance.set_fname(fname_string);

This post has been edited by Anarion: 04 December 2009 - 12:09 PM

Was This Post Helpful? 0
  • +
  • -

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: c++ storing information in a class

Posted 04 December 2009 - 12:09 PM

class fitness
{
  string Fname, Lname;
public:
  int age;
  double Mtime;
  char gender;
  void set_fname(string &str);
  void set_lname(string &str);
};


Why are you passing set_fname and set_lname arguments as references? Not need.
Try this:

[code]class fitness
{
  string Fname, Lname;
public:
  int age;
  double Mtime;
  char gender;
  void set_fname(const string str) { Fname = str; }
  void set_lname(const string str) { Lname = str; }

  // return private members values
  string ret_fname() const { return Fname; }
  string ret_lname() const { return Lname; }
};



And an example:

int main()
{
   // create an instance of class 'fitness'
   fitness ft;
   ft.set_fname("test"); // Fname will be test
   ft.set_lname("test2"); // Lname will be test2

  // return fname and lname
  cout << ft.ret_fname() << endl; // Output 'test'
  cout << ft.ret_lname() << endl; // Output 'test2'
}



Hope that helps!

This post has been edited by sarmanu: 04 December 2009 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#8 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: c++ storing information in a class

Posted 04 December 2009 - 12:12 PM

I used references because it is more efficient when passing strings(I always pass strings) :)

This post has been edited by Anarion: 04 December 2009 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#9 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: c++ storing information in a class

Posted 04 December 2009 - 12:16 PM

View PostAnarion, on 4 Dec, 2009 - 11:12 AM, said:

I used references because it is more efficient. :)

It isn't the case here. You use references as an argument, when you want that argument to keep the value that it got inside the function.
Let me give you an example:

void test(int a, int b ) // arguments not passed as references, so they will be destroyed after procedure ended the call
{
   a = 3;
   b = 4;
} // a and b are 0 now

void test(int &a, int &b ) // they will keep the values
{
   a = 3;
   b = 4;
} // a is 3 now, and b is 4


Hope you understand the meaning of references as arguments.

This post has been edited by JackOfAllTrades: 04 December 2009 - 12:17 PM

Was This Post Helpful? 0
  • +
  • -

#10 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: c++ storing information in a class

Posted 04 December 2009 - 12:20 PM

I know this, look, forgot to mention that part in my post(edited it now). But thanks for trying to explain :)
And if he wants to get user input, he has to store them in a string, and then it is useful to use references

This post has been edited by Anarion: 04 December 2009 - 12:23 PM

Was This Post Helpful? 0
  • +
  • -

#11 corbinc  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 07-November 09

Re: c++ storing information in a class

Posted 04 December 2009 - 12:29 PM

Wow your posts just flew right over my head :)

Here is all the code i have up to this point (note i have not fixed the parts you tried to help me with) and needless to say its full of bugs
Is the logic correct? and will i be able to get this working?
i still have to figure out a way to make it go into the male or female function depending on what they put as there gender, alas im still a nooblet


#include <iostream>
#include <string>

using namespace std;

//***************************************************
// Definition of function FcalcFitness
// This Function calculates fitness level based
// on the time it takes to walk 3 miles for females
//***************************************************
int FcalcFitness()
{
  double Mtime1 = Mtime;

  if (Mtime1 > 48)
  cout << "You have a fitness level of 1" << endl;

  else if (Mtime >= 44 && Mtime <= 47)
  cout << "You have a fitness level of 2" << endl;

  else if (Mtime >= 40 && Mtime <= 43)
  cout << "You have a fitness level of 3" << endl;

  else if (Mtime >= 36 && Mtime <= 39)
  cout << "You have a fitness level of 4" << endl;

  else if (Mtime < 36)
  cout << "You have a fitness level of 5" << endl;

}

//***************************************************
// Definition of function McalcFitness
// This Function calculates fitness level based
// on the time it takes to walk 3 miles for males
//***************************************************
int McalcFitness()
{
  double Mtime1 = Mtime;

  if (Mtime1 > 46)
  cout << "You have a fitness level of 1" << endl;

  else if (Mtime >= 42 && Mtime <= 45)
  cout << "You have a fitness level of 2" << endl;

  else if (Mtime >= 38 && Mtime <= 41)
  cout << "You have a fitness level of 3" << endl;

  else if (Mtime >= 34 && Mtime <= 37)
  cout << "You have a fitness level of 4" << endl;

  else if (Mtime < 34)
  cout << "You have a fitness level of 5" << endl;

}

class fitness
{
  string Fname, Lname;
public:
  int age;
  double Mtime;
  char gender;

};

int main()
{
  cout << "Welcome to the c2 Fitness Calculator!" << endl;
  cout << "The scale is 1 to 5, 1 being the worst, 5 being the best" << endl;
  cout << endl;

  cout << "Enter your First Name: ";
  cin >> Fname;
  cout << endl;

  cout << "Enter your Last Name: ";
  cin >> Lname;
  cout << endl;

  cout << "Enter your Age: ";
  cin >> age;
  cout << endl;

  if (age < 20 || age > 29)
  cout << "Im sorry you must be between 20 and 29 to use this program" << endl;

  cout << "Enter your gender - M or F: ";
  cin >> gender;
  cout << endl;

  cout << "Enter the time it takes you to walk 3 miles - i.e. 15: ";
  cin >> Mtime;
  cout << endl;

  return 0;

} // end main

This post has been edited by corbinc: 04 December 2009 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,554
  • Joined: 19-February 09

Re: c++ storing information in a class

Posted 04 December 2009 - 01:17 PM

Hi, yes it is common to use methods to access private data of a class, this is to ensure the integrity of the data.

If you are using public properties then the class acts like a struct.

I have changed the class name from fitness to Fitness, because that is the way I differentiate between the class and instance/variable.

#include <iostream>
#include <string>

using namespace std;

class Fitness
{

  public:
  string Fname, Lname;
  int age;
  double Mtime;
  char gender;

};

int main()
{
  Fitness fitness;

  cout << "Welcome to Corbins Fitness Calculator!" << endl;

  cout << "Enter your First Name: ";
  cin >> fitness.Fname;
  cout << endl;

  cout << "Enter your Last Name: ";
  cin >> fitness.Lname;
  cout << endl;

  cout << "Enter your Age: ";
  cin >> fitness.age;
  cout << endl;

  cout << "Enter your gender - M or F: ";
  cin >> fitness.gender;
  cout << endl;

  cout << "Enter the time it takes you to walk 3 miles - i.e. 15: ";
  cin >> fitness.Mtime;
  cout << endl;

  cout << "You entered" << endl;
  cout << fitness.Fname << endl;
  cout << fitness.Lname << endl;
  cout << fitness.age << endl;
  cout << fitness.gender << endl;
  cout << fitness.Mtime << endl;

  cin.sync();
  cin.get();

  return 0;

} // end main

This post has been edited by #define: 05 December 2009 - 06:39 AM

Was This Post Helpful? 0
  • +
  • -

#13 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: c++ storing information in a class

Posted 04 December 2009 - 01:20 PM

Your program is full of errors. The class should be declared first. Secondly, you need to declare the functions inside the class.

This is the correct code. I'm not really expecting you to understand anything, since you don't really handle Classes very well:

#include <iostream>
#include <string>

using namespace std;

class fitness
{
	string Fname, Lname;
	int age;
	double Mtime;
	char gender;
public:
		// this is the constructor. it's name must be the same as the
		// class name. it has the role to initialize the class variables.
	fitness(const string fn, const string ln, int a, double mt, char g)
	{
		Fname = fn; // Fname is initialized with fn
		Lname = ln; // Lname is initialized with ln
		age = a; // Age with a
		Mtime = mt; // Mtime with mt
		gender = g; // gender with g
	}

		// those functions shouldn't be int, because they do not
		// return any values. Just a message.
	void FcalcFitness();
	void McalcFitness();
};

//***************************************************
// Definition of function FcalcFitness
// This Function calculates fitness level based
// on the time it takes to walk 3 miles for females
//***************************************************
// whenever you have a function declared in a class, you must use
// the class name like that.
// if you have a class test, with a function 'void tst()' inside, declaring
// the function outside the class should be done like that:
// void test /* this is the name of class */::tst() { ... code ... }
// Notice the ::
void fitness::FcalcFitness() 
{
		// i do not understand why did you create another variable here,
		// but i didn't modify anything.
	double Mtime1 = Mtime;

	if (Mtime1 > 48)
		cout << "You have a fitness level of 1" << endl;
	else if (Mtime >= 44 && Mtime <= 47)
		cout << "You have a fitness level of 2" << endl;
	else if (Mtime >= 40 && Mtime <= 43)
		cout << "You have a fitness level of 3" << endl;
	else if (Mtime >= 36 && Mtime <= 39)
		cout << "You have a fitness level of 4" << endl;
	else if (Mtime < 36)
		cout << "You have a fitness level of 5" << endl;
}

//***************************************************
// Definition of function McalcFitness
// This Function calculates fitness level based
// on the time it takes to walk 3 miles for males
//***************************************************
void fitness::McalcFitness()
{
	double Mtime1 = Mtime;

	if (Mtime1 > 46)
		cout << "You have a fitness level of 1" << endl;
	else if (Mtime >= 42 && Mtime <= 45)
		cout << "You have a fitness level of 2" << endl;
	else if (Mtime >= 38 && Mtime <= 41)
		cout << "You have a fitness level of 3" << endl;
	else if (Mtime >= 34 && Mtime <= 37)
		cout << "You have a fitness level of 4" << endl;
	else if (Mtime < 34)
		cout << "You have a fitness level of 5" << endl;
}

int main()
{
	string Fname, Lname;
	int age;
	double Mtime;
	char gender;

	cout << "Welcome to the c2 Fitness Calculator!" << endl;
	cout << "The scale is 1 to 5, 1 being the worst, 5 being the best" << endl;
	cout << endl;

	cout << "Enter your First Name: ";
	cin >> Fname;
	cout << endl;

	cout << "Enter your Last Name: ";
	cin >> Lname;
	cout << endl;

	cout << "Enter your Age: ";
	cin >> age;
	cout << endl;

	if (age < 20 || age > 29)
	{
		// Exit the program, not only show the message.
		cout << "Im sorry you must be between 20 and 29 to use this program" << endl;
		system("PAUSE");
		exit(1);
	}

	cout << "Enter the time it takes you to walk 3 miles - i.e. 15: ";
	cin >> Mtime;
	cout << endl;

	cout << "Enter your gender - M or F: ";
	cin >> gender;
	cout << endl;

		// create the class, using the values you've entered. 
		// if you've entered "Michelle" as Fname, your private member
		// Fname will become "Michelle"
	fitness ft(Fname, Lname, age, Mtime, gender);
	if (gender == 'F') // If the gender is F (female), then show female stats
		ft.FcalcFitness();
	else
		ft.McalcFitness();

	return 0;
} // end main



This program does basically nothing. Age is not used, but that's your code. Enjoy, and try to understand.
ps: i've tried not to modify any variable name, or code structure.

This post has been edited by sarmanu: 04 December 2009 - 01:21 PM

Was This Post Helpful? 1
  • +
  • -

#14 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: c++ storing information in a class

Posted 04 December 2009 - 03:59 PM

that was nice of you :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1