* This code is ran under Microsoft Visual C++ 2008 Express Edition.
* So when I do run the program, it runs, it lets me input 'The Name', 'Resistance Value', and 'Tolerance Value'.
*It then displays The maximum and minimum resistance.
*Now here's the problem!--It displays hexadecimals.
*HERE'S MY CODE

#include <iostream>
#include <string>
using namespace std;

class CResistor
{
			
	public:

		void InputInfo()
		{
			cout<<"Enter a text name for the CResistor object being created."<<endl;
			cin>>m_sResName;
			cout<<"\n";

			cout<<"Enter the Following Data:"<<endl;
			cout<<"Resistance Value= ";
			cin>>Res0;
			cout<<"Tolerance Value= ";
			cin>>Res1;

			ResTolValue=(Res0*Res1);
			Res2=(Res0+ResTolValue);
			Res3=(Res0-ResTolValue);
			cout<<":Max Resistance= "<<Res2<<endl;
			cout<<":Min Resistance= "<<Res3<<endl;
			cout<<"\n";
		
		}
		void DisplayResistor()
		{
			cout<<"\nDisplay:";
			cout<<m_sResName<<endl;
			cout<<Res0<<endl;
			cout<<"Current Tolerance: "<<Res1<<endl;
			cout<<"Max: "<<Res2<<endl;
			cout<<"Min: "<<Res3<<endl;
		}
		void EnterResistance()
		{
			cout<<"\n";
			cout<<"Current Resistance: "<<Res0<<endl;
			cout<<"Enter New Resistance: "<<endl;
			cin>>Res0;
				while(Res0<=0||Res0>10000000)
				{
					cin>>Res0;
				}
			cout<<"Currrent Tolerance: "<<Res1<<endl;
			cout<<"Enter New Tolerance: "<<endl;
			cin>>Res1;
				while(Res1<=0||Res1>50)
				{
					cin>>Res1;
				}
			ResTolValue=(Res0*Res1);
			Res2=(Res0+ResTolValue);
			Res3=(Res0-ResTolValue);
			
		}

	private:
		double ResTolValue;
		double Res0,Res1,Res2,Res3 ;
		string m_sResName;

};

int main()
{
	CResistor First;
	First.InputInfo();
	CResistor Second;
	Second.DisplayResistor();
	CResistor Third;
	Third.EnterResistance();
	CResistor Last;
	Last.DisplayResistor();
	return 0;
}

Recommended Answers

All 16 Replies

Post relevant trial input. You may just need to use an ostream manipulator to keep it displayed in the format you want.

What do you mean 'ostream'?

cout is a global ostream class object. It comes available with a number of class methods, just like you have written for your classes. The ostream class is declared in iostream header file. There are other standard functions/manipulators that are declared in the iomanip header. You can use these various methods to format the output displayed by ostream objects such as cout. Formatting means whether the out is filled, right/left justified, of specified width, has fixed decimal expression, what precision to use if decimal expression, whether to use hex or dec formtat, etc.

Sorry Lerner, I still don't get it--i'm quite slow. Do you think you can show me what you mean by replacing the incorrect or the missing substance within my code?

Your code looks okay as an initial look, which is why I asked you to post an example of input and resultant output. Based on what your input is and output is, then it might be a matter of formatting you output correctly. For a while at least I'm typing response on a lap top while watching a football game. My compiler isn't on this computer so I can't copy/paste your program to run it for now.

Yeah i'm totally watcing a football game too lol. Well here's the output. This is what's frustrating me, that weird hexadecimal that keeps popping out.

Here's a picture of what I mean:

[IMG]http://i37.tinypic.com/xp0ei0.jpg[/IMG]

Two things.

What you are calling hexadecimal output is actually called scientific notation given e follwed by a - or a + sign in the output.

Second, the reason you are seeing it is that none of the object variables for Second have been initialized by the time you call the DisplayResistor() method on the declared object. Therefore, it is display a junk double value using scientific notation. Somehow you need to give the member variables of Second some sort of meaningful value before you try to display them.

I am not sure if i should initialize void DisplayResistor() with or without ampersands and would i have to initialize the void DisplayResistor() in my main function as well?

Sorry, firstperson it's not working, I don't know where in my code i should initialize and how i should initialize.

Ideally, from my point of view, you would initialize the class member variables to a default value within the constructor. Then anytime you try to use the member variables in a class method without initializing them at least you will get something that you can recognize as being default values.

Ideally you would always provide your user defined types with a default constructor, a copy constructor, an assignment operator and a destructor. Since the compiler will (sometimes) create these these methods for you, you don't _have_ to write them, but until you have enough experience to determine when to accept the compilers version and when to write your own, I recommend you write your own.

Since you don't write a constructor, the default constructor written by the compiler probably looks something like this:

CResistor(){}

It would be better if you had something like this:

CResistory()
{
    ResTolValue = Res0 = Res1 = Res2 = Res3 = 0.0;
    m_sResName = "";
}

although if you've learned about initialize lists, using one of those would be better yet.

If you had that code in your class (or something like it), then when you fail to give other values to the member variables of Second, at least they will have the default.

If you wanted, you could create a second version of the constructor that takes 3 values, one for the name of the resistor, one for the intial value of the resistor and one for the variance of the resistor. You could calculate the values of the other variables with that information. Then you could write something like this:

CResistor Second("two", 4.66, 0.03);
Second.DisplayResistor();

and you should get some useful information displayed. Alternatively you could declare and define setter and getter methods (also known as mutators and accessors) with public access so you (or the end user)
can access an object's member variables from within the program itself without relying on default values provided by constructor(s) or values passed to constructors.

You should know that if you create a non-default constructor but you don't declare a default constructor, then the compiler won't declare and define a defaulst constructor for you. Again, in some classes (programs) that may be acceptable, but it frequently won't be.

Ok now that I have inserted that new piece of code that you gave me, it now outputs '0's rather than a scientific notation. It's a great sign! Though I would like the program to display all the entered data into the 'void DisplayResistor()' function.

After you declare First you then call InputInfo() on First. This allows you to alter/set/mutate the default values stored in First by the default constructor and display them all with a single function call.

After kyou declare Second and Last you immediately call DisplayResistor() without altering/setting/mutating the member variables. Therefore you will only get the default values of the member variables. If you want to display some non-default value of the member variables you will need to alter/set/mutate the member variables first.

After you declare Third you call EnterResistance() which allows you to alter/set/mutate the member variables.

If you call DispalyResistance() on First or Third you will display the non-default values they have been given.

My teacher has never taught me how to alter/set/or mutate member variables. I am kinda catching to what your saying. But the thing is, is how would I Alter/set/or mutate in this situation?

Haven't looked at your code but generally :

//setter function
void alterVariableX(data_type x_o)  { X = x_o; }
//alter/mutator function
data_type getVariableX() { return X; }

An alternate form of a alter/set/mutate method culled from InputInfo() could be:

void setM_sResName()
{
   cout << "Enter a text name for the CResistor object being created."<<endl;			
  cin >> m_sResName;
}

Post #16 line 4 by firstperson is a good example of the general form of a get/access method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.