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.
//
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
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.
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.
@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.
#include <iostream>
#include <string>
usingnamespace 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 (constauto 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;
}
}