How to Assign a value to an Integer?

Hey guys,

Im trying to assign a value to an Integer (Yeah I know probably extremely simple but im completely new to C++).
How do I do this?

This is what I have for now as to declare the variables:

1
2
3
4
5
  private:
	// -------------------------
	// Member functions
	// -------------------------
	int a, b, c;


I have to assign certain values to them.
Here is the actual assignment:

Declare in IntDoubleLab.h, three variables of type int.
Assign in IntDoubleLab.cpp the value -2147483640 to the first variable, and -10
to the second variable. Compute the sum of these two variables, and store in the
third variable. Print the contents of these variables on the screen. Test it, and
note that you get a positive result, although we added two negative numbers!


I hope someone can help me out.

Thank you!
closed account (o3hC5Di1)
Hi there,

initializing members is usually done through the class constructor and its initializer list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class my_class
{
private:
	// -------------------------
	// Member functions
	// -------------------------
	int a, b, c;

public:
    my_class() : a(-2147483640), b(-10)
    {
        c = a+b;
    }
};


Hope that helps.

All the best,
NwN
To assign a value you would do

1
2
3
4
5
6
IntDoubleLab::IntDoubleLab( /*params if any*/ )
{
    a = //value
    b = //value
    c = //value 
}


But you should initialize them instead

1
2
IntDoubleLab::IntDoubleLab( /*Params if any can be used to init*/ )
: a( /*value*/ ) , b( /*value*/ ) , c( /*value*/ ){}
Well, with IntDoubleLab.h, that is a header. Have you created the header yet? If so, then in the header you would do this:

1
2
3
int varType1 = 0;
int varType2 = 0;
int varType3 = 0;


or...
 
int varType1 = 0, varType2 = 0, varType3 = 0;


Always initialize them to '0' so that they don't have any other values at the start.
------------------------------------------------------------------------------------------------------------------

Then in the .cpp file

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
#include <iostream>
#include <IntDoubleLab.h>

using namespace std;

int main()
{

varType1 = -2147483640;
varType2 = -10;

varType3 = varType1 + varType2;    // Sum of the first two as defined by programmer

// Error checking and output statements

cout << "varType1 = " << varType1 << endl; // varType1 output
cout << "varType2 = " << varType2 << endl; // varType2 output
cout << "varType3 = " << varType3 << endl << endl; // varType3 output

system("pause");     // Pauses program until user continues
return 0; 
}


Last edited on
I don't see the need to have a header for this program, except for the sake of creating a header file.
Why would someone dealing with classes ask this question?
Hey guys,

thanks for the many answers although I have another question. My professor seems to want us to do it in a specific way of his (for some reason).

Im currently doing this in my .cpp

1
2
3
4
5
6
m_Number1 = -2147483640;

	m_Number2 = -10;
		m_Result = m_Number1 + m_Number2;
		
		OutputDebugString(String("The result is: " ) + m_Result + );


This doesnt return any errors but neither does it show up when I execute the program.
Here is the entire game paint code:

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
void IntDoubleLab::GamePaint(RECT rect)
{
	m_ResultExpr = 3 * 5 / 2;
	GAME_ENGINE->DrawString(String("3 / 5 + 2 = ") + m_ResultExpr,10,0);

	m_ResultExpr = 3 / 5 * 2;
	GAME_ENGINE->DrawString(String("3 / 5 + 2 = ") +m_ResultExpr,10,15);

	

	m_ResultExpr = 60 * 60 * 24 * 365;
	GAME_ENGINE->DrawString(String("60 * 60 * 24 * 365 = ") + m_ResultExpr,10,160);

	m_Number1 = -2147483640;

	m_Number2 = -10;
		m_Result = m_Number1 + m_Number2;
		
		OutputDebugString(String("The result is: " ) + m_Result + "\n");





	// Insert the code that needs to be executed each time a new frame needs to be drawn to the screen
	// Technical note: engine uses double buffering when the gamecycle is running

}
Last edited on
Topic archived. No new replies allowed.