[FUNTIONS] How do I sum the result of 2 multiplications?

Hey I am making this program and I want to know how to after multiplying a * b and c * d, independently how I take both of their results and sum the?:


Here is the code,to show you what I mean:

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
58
59
60
61
62
class JosephInstalation1
{
public: 

	//Asing the feet of cable needed.
	void setFeetcable(int num)
	{
		feetCable = num;
	}

	//Asing cost of cable per feet.
	void setCostfeet(int num)
	{
		costFeet = num;
	}

	//Asing quantity of conectors.
	void setQuantconect(int num)
	{ 
		quantConect = num;
	}

	//Asing cost of each conector.
	void setCostconect(int num)
	{
        costConect = num;
	}

	//Return the value of feet of cable.
	int getFeetcable()const
	{
		return feetCable;
	}

	//Return the value of cost per feet.
	int getCostfeet()const 
	{
		return costFeet;
	}

	//Return value of the quantity of conectors.
	int getQuantconect()const
	{
		return quantConect;
	}

	//Return the value of the cost of each conector.
	int getCostconect()const
	{
		return costConect;
	}

	//Return the product of two values in each function.
	int mult()const
	{
		return feetCable * costFeet;
    }
    
	int mult()const
	{
		return quantConect * costConect;
	}


How do I do a Sum the result of each multiplication?

Also if anyone can tell me is there some errors in what is done, that would be nice too.

Thanks I hope Someone answer my doubts.

:)

hi,

[code]

result = Instance_Of_JosephInstallation1.mult() + Instance_Of_JosephInstallation1.mult()

[/quote]

Only problem is that you have two functions with the same name. i doubt that will compile. Call them multi1 and multi2.

Your second mult() is outside the class. Why?

Btw. your class-name is pretty long and thus bound for spelling mistakes. I would try to shorten it a bit.

And the next time, try to tell us which multiplication you like to use.
Ok now I get it I finish the program but I get this 4 errors and 2 warnings, how do I fix them?

1
2
3
4
5
6
 Error	1	error C2065: 'Instance_Of_Wiring' : identificador no declarado	e:\stufff\my documents\cosc 131\2008 vs projects\josephinstalation1\josephinstalation1\josephinstalation1.cpp	80
Error	2	error C2228: el operando izquierdo de '.mult1' debe tener class/struct/union	e:\stufff\my documents\cosc 131\2008 vs projects\josephinstalation1\josephinstalation1\josephinstalation1.cpp	80
Error	3	error C2065: 'Instance_Of_Wiring' : identificador no declarado	e:\stufff\my documents\cosc 131\2008 vs projects\josephinstalation1\josephinstalation1\josephinstalation1.cpp	80
Error	4	error C2228: el operando izquierdo de '.mult2' debe tener class/struct/union	e:\stufff\my documents\cosc 131\2008 vs projects\josephinstalation1\josephinstalation1\josephinstalation1.cpp	80
Advertencia	5	warning C4129: 'T' : secuencia de escape de carácter no reconocida	e:\stufff\my documents\cosc 131\2008 vs projects\josephinstalation1\josephinstalation1\josephinstalation1.cpp	101
Advertencia	6	warning C4129: 'W' : secuencia de escape de carácter no reconocida	e:\stufff\my documents\cosc 131\2008 vs projects\josephinstalation1\josephinstalation1\josephinstalation1.cpp	141



Check it:

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

//This project will calculate the cost of an  wiring installation of a comunication system.
//The installation consists of an amount of communication cable feet and an amount of connectors. 

#include <iostream>

using namespace std;

//Definition of the class Wiring.

class Wiring 
{
public: 

	//Asing the feet of cable needed.
	void setCable(int num)
	{
		cable = num;
	}

	//Asing cost of cable per feet.
	void setCostcable(int num)
	{
		costCable = num;
	}

	//Asing quantity of conectors.
	void setConectors(int num)
	{ 
		 Conectors = num;
	}

	//Asing cost of each conector.
	void setCostconectors(int num)
	{
        costConectors = num;
	}

	//Return the value of feet of cable.
	int getCable()const
	{
		return cable;
	}

	//Return the value of cost per feet.
	int getCostcable()const 
	{
		return costCable;
	}

	//Return value of the quantity of conectors.
	int getConectors()const
	{
		return Conectors;
	}

	//Return the value of the cost of each conector.
	int getCostconectors()const
	{
		return costConectors;
	}

	//Return the product of two values.
	int mult1()const
	{
		return cable * costCable;
    }
    
	//Return the product of two values.
	int mult2()const
	{
		return Conectors * costConectors;
	}

	//Return the sum of mult1 + mult2
	int sum()const
	{
	    return Instance_Of_Wiring.mult1() + Instance_Of_Wiring.mult2();
	}

private:
    
    int
	    cable,         //Quantity of feet of cable.
		costCable,     //Cost of cable per feet..
		Conectors,     //Quantity of conectors.
		costConectors; //Cost of conectors.
};



int main()
{
	int number; //To asing temporal values.

	Wiring install; //Create the object install.

	cout << "\n\n\n\n\tWelcome to your wiring installation Guide!!\n\n";
	cout << "\t\t\This will help you calculate the cost of your wiring comunication system.\n\n\n";
    
	cout << "\n\n\tEnter the quantity in feet of the cable:";
	cin >> number;

	install.setCable(number); 

	cout << "\n\n\tEnter the cost of cable:"; 
	cin >> number; 

	install.setCostcable(number);

	cout << "\n\n\tEnter the desired amount of conectors:";
	cin >> number;

	install.setConectors(number);

	cout <<"\n\n\tEnter the cost of conectors:";
	cin >> number;

	install.setCostconectors(number);

	system("pause");
    
	system("CLS"); 

	cout << "\n\tThe cost  of cable per feet " << install.getCable() << " * " << install.getCostcable()
		<< " is " << install.mult1() << '.'<< endl << endl << endl;
                
	cout << "\n\tThe cost  of the conectors " << install.getConectors() << " * " << install.getCostconectors()
		<< " is " << install.mult2() << '.'<< endl << endl << endl;

	cout << "\n\tThe tolal cost of the installation " << install.mult1() << " + " << install.mult2()
		<< " is " << install.sum() << '.'<< endl << endl << endl;

	system("pause");

    system("CLS");

	cout << "\n\n\n\n\tThanks for using our program!!\n\n";
	cout << "\t\t\We hope that all your doubts were answered, please visit us again!!.\n\n\n";

	system("pause");

		return 0;

}












Sorry if this is too long.

Thanks!
Last edited on
About the class name --I disagree. As long as you keep it under 20-30 characters using complete words (as you have), and you are consistent with your studly caps, I don't think it can cause any problems.

Personally I tend to use somewhat longer names (you won't find any one- or two-letter names occupying large scopes of my code), just because I prefer to name things in a way that instantly identifies them.

Here are some of the longer identifiers I've used recently in my coding. See if you can figure out what they do:
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
58
59
60
61
62
63
64
65
66
    // From an s-expression reader
    void skip_whitespace();
    string read_comment( const string& begin_sequence, const string& end_sequence );
    sexpression_t* read_pair_or_value();

    // From GreyWolf's Sudoku challenge
    const int MINIMUM_GAMEBOARD_SIZE =  4;
    const int MAXIMUM_GAMEBOARD_SIZE = 25;
    const int MINIMUM_REGION_SIZE    =  2;

    // Functional predicates for use with the STL algorithms
    template <typename Container>
    struct member_of: std::unary_function <typename Container::value_type, bool>
      {
      ...
      };
    template <typename DataType>
    struct double_of: std::binary_function <DataType, DataType, bool>
      {
      ...
      };

    // From QWERTYman's BlackJack (challenge)
    enum Action { Hit, Stand, DoubleDown, Surrender };

    const char* SuitNames[] =
      {
      "Joker", "Ace",   "Two",  "Three", "Four", "Five",  "Six",
      "Seven", "Eight", "Nine", "Ten",   "Jack", "Queen", "King"
      };

    const int SuitValue[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
    enum Suit
      {
      Joker   =  0,
      Ace     =  1,
      Jack    = 11,
      Queen   = 12,
      King    = 13,
      SoftAce = 11
      };

    const char* FaceNames[] = { "Spades", "Diamonds", "Clubs", "Hearts" };
    enum        Face          {  Spades,   Diamonds,   Clubs,   Hearts  };

    void display_all_hands(
      const deque <Hand> & hands,
      bool                 is_show_hole_card
      );

    void display_player_winnings(
      const deque <Money> & winnings,
      const deque <Hand>  & hands = deque <Hand> ()
      );

    void get_bets( deque <Money> & bets );

    void ask_hit_or_stand(
      const string& playername,
      ShoeDeck&     deck,
      Money&        winnings,
      Money&        bet,
      Hand &        hand,
      Money&        insurance,
      bool &        standing
      );

I am a big believer in meaningful names. Scrunching a variable name down to less-than-eight characters has zero effect on the size of the output code, but it often as deleterious effects on readability when a longer --but much more readable-- name could have been used.

I presume PSPMAN90 that you are in a country where some romance language other than English is used? Because you do have some spelling errors for English, but otherwise I think your names are great. Just try to be consistent on how you use capitalization.

My $0.02.

[edit]
Ah, espanglish. Dé me un momentito repasar su código.
Last edited on
The first problem is that you are trying to use a variable, Instance_of_Wiring, that was never created anywhere. The only actual instance of the Wiring class that you have is install, in main() (line 96).

However, you should try to figure out a way to avoid using variables that are not declared as part of your class inside your class's methods. (But I think you really meant to say something like:
1
2
3
4
	int sum() const
	{
	    return mult1() + mult2();
	}
which is the same as
1
2
3
4
	int sum() const
	{
	    return this->mult1() + this->mult2();
	}
?)

The other problem is that you have accidentally put a \T and a \W in your strings (lines 99 and 139 respectively), which are not valid escape sequences --hence the compiler complains that it doesn't recognize them.

Hope this helps.
Ohh, yeah I correct the problems my self, I forgot to edit the Post lol!

Yeah I am from PR, spanish is my native language! "Tacos"! -_-'!

And "Thanks" for telling me that I have lots of spelling errors in my code :(

Thanks for telling me a different way to use return :)

Also thank you for making me notice that n\w warning! I correct it !

No errors now! Yay!

another thing! I don't want to show the multiplication process in the screen, you know the:

1
2
3
4
5
cout << "\n\tThe cost  of cable per feet " << install.getCable() << " * " << install.getCostcable()
		<< " is " << install.mult1() << '.'<< endl << endl << endl;
                
	cout << "\n\tThe cost  of the conectors " << install.getConectors() << " * " << install.getCostconectors()
		<< " is " << install.mult2() << '.'<< endl << endl << endl;


I just wanna show the result in the screen.

The same with the sum, just want to show the result in the screen, and not the whole process of A + B is AB just, The total cost is: AB, on the screen:

1
2
cout << "\n\tThe tolal cost of the installation " << install.mult1() << " + " << install.mult2()
		<< " is " << install.sum() << '.'<< endl << endl << endl;


Just like the results of the whole * and +, entendeis?

and one last thing....

So, what should I do about the language?


Last edited on
Erm, I'm only one person... :-)

If you don't want to show the process, just don't do it.

cout << "\n\tThe total cost of the cable is " << install.mult() << ".\n\n\n";
etc.

Does this have to be in English? (Not Spanish?)
You can always run the words through a spell-checker. I see you caught that "install" has two Ls. A lot of English words double-up on consonants like L, N, M, R, S, T, etc. where Spanish uses only one. The other two you have are "connector" and "communication".

Tengo muchos amigos de Puerto Rico, aunque jamás lo he visitado. (Al mirar la TV se pensaría que los puertoriqueños no hablen bien el Español, pero en mi experiencia todos quienes conozco lo hablan con precisión. Yo lo aprendí en San Diego, CA. Los de Tiajuana hablan con gramatica horrible, pero la mayoría de los mexicanos tienen una idioma bien sencillo y suave. Es decir, yo hablo como soy de México. :-) Pues, hablo el Inglés lentamente también.... :-\
/end hijack
Last edited on
Topic archived. No new replies allowed.