I'm in school and stuck trying to go from a double to an integer

Hi All,
I am taking a course in c++ and doing an assignment. I have to post the whole code I have right now.


Here is the code I am trying to go from a double to an int and am stuck. I have tried several things and am not getting what I need to do. Any help is appreciated.
I have added line numbers.
// Homework 5.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

4 #include <iostream>
5 #include <fstream>
6 #include <string>
7 #include <iomanip>

9 using namespace std;

11 int main()
12 {
13 double a, b, c, result;
14 string first, last;

16 cout << "Enter your first name" << endl;
17 cin >> first;
18 cout << "Enter your last name" << endl;
19 cin >> last;
20 cout << "Enter 3 grades" << endl;
21 cin >> a >> b >> c;

23 result = (a + b + c) / 3;


26 double d = int result;

28 switch (d)
29 {
30 case '10':
31 cout << "You have an A" << endl;
32 break;
33 case 9:
34 cout << "You have an A" << endl;
35 break;
36 case 8:
37 cout << "You have a B" << endl;
38 break;
39 case 7:
40 cout << "You have a B" << endl;
41 break;
42 case 6:
43 cout << "You have a C+" << endl;
44 break;
45 case 5:
46 cout << "You have a C" << endl;
47 break;
48 case 4:
49 cout << "You have a D=" << endl;
50 break;
51 case 3:
52 cout << "You have a F" << endl;
53 break;
54 case 2:
55 cout << "You have an F" << endl;
56 break;
57 case 1:
58 cout << "You have an F" << endl;
59 break;
60 case 0 :
61 cout << "You have an F" << endl;
62 break;
63 }
}64

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Please explain how you came up with (line 3):
1
2
3
double result;

double d = int result;
I thought that by doing the above that I was leaving the result as a double and the d as an integer.
Actually this is something I saw on the internet but it didn't work.
Last edited on
If I complete my code to a "program":
1
2
3
4
5
6
int main()
{
  double result;

  double d = int result;
}

A compiler says:

main.cpp:5:18: error: expected '(' for function-style cast or type construction
  double d = int result;
             ~~~ ^
1 error generated.

Syntax is thus wrong.

The double result; declares a variable that has type double and name "result".
The double d declares a variable that has type double and name "d".

You want integer, so you should declare an integer:
 
int d = int result;

Alas, the initializer = int result; still has a syntax error.

The error message mentions "function-style cast" and that '(' might be missing.
The "function-style cast" is from C. C++ has more precise casts.
1
2
int d = int (result);             // C cast
int d = static_cast<int>(result); // C++ cast 

However, what if you simply copy the value of result into d: int d = result;
What happens with that?
int d = int (result); // C cast
Apologies for the pedantry, but that's not C.
(int)result would be C.
OK I was able to open a console window. I got enter your first name but I cannot enter a first name. This is a problem. My last assignment did the same thing. I reinstalled VS community. It should allow me to enter a name. int d = result works. Thanks for that.
I am posting the output of this program.

'Homework 5.exe' (Win32): Loaded 'C:\C++ Code\Homework 5\x64\Debug\Homework 5.exe'. Symbols loaded.
'Homework 5.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'.
'Homework 5.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'.
'Homework 5.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'.
'Homework 5.exe' (Win32): Loaded 'C:\Windows\System32\msvcp140d.dll'.
'Homework 5.exe' (Win32): Loaded 'C:\Windows\System32\vcruntime140d.dll'.
'Homework 5.exe' (Win32): Loaded 'C:\Windows\System32\vcruntime140_1d.dll'.
'Homework 5.exe' (Win32): Loaded 'C:\Windows\System32\ucrtbased.dll'.
The thread 0x4e70 has exited with code 0 (0x0).
The thread 0x8080 has exited with code 3221225786 (0xc000013a).
The thread 0x54cc has exited with code 3221225786 (0xc000013a).
The thread 0x474c has exited with code 3221225786 (0xc000013a).
The program '[27256] Homework 5.exe' has exited with code 3221225786 (0xc000013a).
@mathman54. Please don't post line numbers as part of the code. It makes it very difficult to copy/paste provided code into a compiler. Also, please use code tags with formatted code so it displays readable.

Do you want something like:

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

using namespace std;

int main() {
	double a {}, b {}, c {};
	string first, last;

	cout << "Enter your first name: ";
	cin >> first;

	cout << "Enter your last name: ";
	cin >> last;

	cout << "Enter 3 grades (between 0 and 10): ";
	cin >> a >> b >> c;

	switch (const auto res { (a + b + c) / 3.0 }; static_cast<int>(res)) {
		case 10:
		case 9:
			cout << "You have an A\n";
			break;

		case 8:
		case 7:
			cout << "You have a B\n";
			break;

		case 6:
			cout << "You have a C+\n";
			break;

		case 5:
			cout << "You have a C\n";
			break;

		case 4:
			cout << "You have a D=\n";
			break;

		default:
			if (res > 10)
				cout << "You are cheating!\n";
			else
				cout << "You have a F\n";

			break;
	}
}

Last edited on
Topic archived. No new replies allowed.