Learning C++ syntax

Hi all and thanks for the help! My intro class was in VB (I know lol).

I just started C++ and the syntax is giving me a problem. I know it's a quick fix but I'm stuck.

Why is the var I'm assigning a value to not being affected by the if statement? Driving me nuts.

Also please excuse the utter noobery of the programming style but like I said I'm just trying to get used to the syntax. Ignore the last cout/cin that is just there so i can see the program run.

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

using namespace std;

int main()
{
    int num1, num2, num3, sum, prod, avg, lrg, sml;

	cout << "Hello, This program will display The sum, products, average, and the largest and \n";
	cout << "and smallest of three numbers you enter...\n";

	cout << "Please enter your first number...\n";
	cin  >> num1;

	cout << "Please enter your second number...\n";
	cin  >> num2;

	cout << "Please enter your third number...\n";
	cin  >> num3;
	cout << '\n';

	sum = num1 + num2 + num3;
	cout << "Here is the sum of your numbers..." << sum << '\n';
	cout << '\n';

	prod = num1 * num2 * num3;
	cout << "Here is the product of your numbers..." << prod << '\n';
	cout << '\n';

	avg = (num1 + num2 + num3) / 3;
	cout << "Here is the average of your numbers..." << avg << '\n';
	cout << '\n';

	lrg = num1;
	if (num2 > num1)
	{
		num2 = lrg;
	}
	else if (num3 > num2)
	{
		num3 = lrg;
	}

	cout << "The largest of your numbers..." << lrg << '\n';
	cout << '\n';

	cout << '\n';
	cin  >> sml;
	return 0;
}
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
#include <iostream>

using namespace std;

int main()
{
    int num1, num2, num3, sum, prod, /*avg,*/ lrg, sml; // *****
    double avg ; // *****

	cout << "Hello, This program will display The sum, products, average, and the largest and \n";
	cout << "and smallest of three numbers you enter...\n";

	cout << "Please enter your first number...\n";
	cin  >> num1;

	cout << "Please enter your second number...\n";
	cin  >> num2;

	cout << "Please enter your third number...\n";
	cin  >> num3;
	cout << '\n';

	sum = num1 + num2 + num3;
	cout << "Here is the sum of your numbers..." << sum << '\n';
	cout << '\n';

	prod = num1 * num2 * num3;
	cout << "Here is the product of your numbers..." << prod << '\n';
	cout << '\n';

	//avg = (num1 + num2 + num3) / 3; // *****
	avg = (num1 + num2 + num3) / 3.0 ; // *****
	cout << "Here is the average of your numbers..." << avg << '\n';
	cout << '\n';

	lrg = num1;
	if (num2 > num1)
	{
		//num2 = lrg; // *****
		lrg = num2 ; // *****
	}
	//else if (num3 > num2) // *****
	if( num3 > lrg ) // *****
	{
		//num3 = lrg; // *****
		lrg = num3 ; // *****
	}

	cout << "The largest of your numbers..." << lrg << '\n';
	cout << '\n';

	cout << '\n';
	cin  >> sml;
	return 0;
}
Last edited on
Note that if num2>num1, else if will not occur. Yet num3 could be greater than num1 or num2.
Thanks, mariostg.
I've corrected it now.
Thank you! Wow I didn't even see the improper syntax. Guess I needed to step away for a break.
Last edited on
Just something that you would find interesting based on
Ignore the last cout/cin that is just there so i can see the program run.


Look at the second topic that is in the Beginners section. It is titled "Console closing down" take a look at that. It will help you.
Topic archived. No new replies allowed.