C++ Logic Error

I just started C++ a few days ago and I'm just trying basic arithmatic. It builds, but it doesn't add together. What am I not getting?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main()
{
	int num1 = 64;
	int num2 = 32;
	int result = num1 + num2; 
	
	cout << "\n64 + 32 = " + result << endl;
	cin.get();
	return 0;
}
Last edited on
cout << "\n64 + 32 = " + result << endl;
should be
cout << "\n64 + 32 = " << result << endl;

you can't add an int to a string literal
Last edited on
I am trying to get these to display in a prompt window, but it only displays the main() function and then exits the prompt window. Can anyone help me understand what I'm doing wrong?

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
#include <iostream>

using namespace std;

int main()
{ 
	cout << "Part 1: Basic Output" << endl;
	cout << "\nHello World!" << endl;
	cout << "My Name is ..." << endl;
	cout << "I'm from ..." << endl;
	cout << "To gain more insight into Programming." << endl;
	cout << "I'm from ..." << endl;
	cout << "All i have is from Programming Logic." << endl;
	cin.get();
	return 1;
}
int integerExample()
{
cout << "\nPart 2: Basic Arithmetic" << endl;
cout << "Integer:\n" << endl;
{
	int num1 = 64;
	int num2 = 32;
	int result = num1 + num2;	
	cout << "64 + 32 = " << result << endl;
}
{
	int num1 = 256;
	int num2 = 128;
	int result = num1 - num2;
	cout << "256 - 128 = " << result << endl;
}
{
	int num1 = 8;
	int num2 = 256;
	int result = num1 + num2;	
	cout << "8 * 256 = " << result << endl;
}
{
	int num1 = 1024;
	int num2 = 512;
	int result = num1 / num2;	
	cout << "1024 / 512 = " << result << endl;
}
{
	int num1 = 52;
	int num2 = 2;
	int result = num1 % num2;	
	cout << "52 % 2 = " << result << endl;
}
{
	int num1 = 52;
	int num2 = 3;
	int result = num1 % num2;	
	cout << "52 % 3 = " << result << endl;
}
{
	int num1 = 52;
	int num2 = 4;
	int result = num1 % num2;	
	cout << "52 % 4 = " << result << endl;
}
{
	int num1 = 1024;
	int num2 = 512;
	int num3 = 256;
	int num4 = 128;
	int num5 = (2*2*2*2*2*2);
	int result = (num1 - (num3 + (num4 + (num5)))) / num2;	
	cout << "(1024 - (256 + (128 + (2*2*2*2*2*2)))) / 512 = " << result << endl;
}
	cin.get();
	return 1;
}
int doubleExample()
{
	cout << "\nDouble:\n" << endl;
{
	double num1 = 63.5;
	double num2 = 31.5;
	double result = num1 + num2;	
	cout << "63.5 % 31.5 = " << result << endl;
}
{
	double num1 = 256.8;
	double num2 = 128.8;
	double result = num1 - num2;	
	cout << "256.8 - 128.8 = " << result << endl;
}
{
	double num1 = 8.9;
	double num2 = 256.1;
	double result = num1 * num2;	
	cout << "8.9 * 256.1 = " << result << endl;
}
{
	double num1 = 1024.0;
	double num2 = 512.0;
	double result = num1 / num2;	
	cout << "1024.0 - 512.0 = " << result << endl;
}
{
	double num1 = 1024.0;
	double num2 = 512.9;
	double num3 = 256.1;
	double num4 = 128.2;
	double num5 = (2.3*2.4*2.5*2.6*2.7*2.8);
	double result = (num1 - (num3 + (num4 + (num5)))) / num2;	
	cout << "(1024.0 - (256.1 + (128.2 + (2.3*2.4*2.5*2.6*2.7*2.8)))) / 512.9 = " << result << endl;
}

	cin.get();
	return 0;
}
wow... what is that integer and double example? O_o what is it supposed to do? + how can u use {} on cout? O_o
Last edited on
I kinda get the feeling you didn't quite understand what exactly functions are. You can think of them as some sort of program on their own, with their own variables, their own logic etc. A function isn't executed automatically, it only runs when it is called.

It works like this:
1
2
3
4
5
6
7
8
9
10
11
12
void do_stuff_function();

int main()
{
do_stuff_function(); /* here, do_stuff_function is "called"
- this means, control is handed over to the do_stuff_function, 
which will then do whatever it does, and then return control to the caller.
*/
return 0;
}

The same goes for the [code]main
function, but it is special in one way: it is the entry point of the program and is automatically called by the system.

I am not sure if I explained this well, so bear with me if it's hard to understand.
@ hanst99

I get the logic of it, but I don't get the code part of it.
you need to call integerExample from your main function like this:
1
2
3
4
int main()
{
integerExample();
}
I still don't get it, You're telling answer but not the way to it. I do not get the problem, so it natural I will not understand the solution.
I told you the logic behind it. Then you said you got that part, you just didn't get the code part.
Then I showed you the code part. Now you're telling me you don't get the logic?
@lord didn't u said that u got the logic? is it that hard to call function within a function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void ShowMeNumber();

int main()
{
cout << "The number is:";
ShowMeNumber(); //this will call function called "ShowMeNumber()" and it will do everything coded in function
cin.get();
return 0;
}

void ShowMeNumber() // this is function, everything in it will happen when fucntion will be called
{
cout << "3";
}
The number is: 3
// output, this will be displayed in console

wow... that was hard

how do u wanna make program to do something if u won't tell him what to do?

if u won't understand this, please, leave programming, and do something easier, like cooking or something

edit: +, why are u still initializing variables? like

1
2
3
4
5
6
7
8
9
10
	int num1 = 64;
	int num2 = 32;
	int result = num1 + num2;	
	cout << "64 + 32 = " << result << endl;
}
{
	int num1 = 256;
	int num2 = 128;
	int result = num1 - num2;
	cout << "256 - 128 = " << result << endl;


it should be:

1
2
3
4
5
6
7
8
9
10
	int num1 = 64;
	int num2 = 32;
	int result = num1 + num2;	
	cout << "64 + 32 = " << result << endl;
}
{
	num1 = 256;
	num2 = 128;
	result = num1 - num2;
	cout << "256 - 128 = " << result << endl;
Last edited on
Topic archived. No new replies allowed.