446,364 Members | 1,493 Online
Need help? Post your question and get tips & solutions from a community of 446,364 IT Pros & Developers. It's quick & easy.

# Need help replacing text

 P: 35 Struggling greatly here with C++ but I find it interesting at the same time. Any good books to buy or web sites to help with this program? For the following - I'm suppose to replace the first three statements with cin and cout statements so that the values for the age, salary, and distance_to_the_moon can be obtained from the user. This make the program flexible so that everytime is is run different numbers are used. #include void main(void) { int age = 32; float salary = 25000.75; long distance_to_the_moon = 238857; cout << “The employee is “ << age << “ years old” << endl; cout << “The employee makes \$” << salary << endl; cout << “The moon is “ << distance_to_the_moon << “ miles from the earth” << endl; } # comments appear at the top of the file indicating the assignment number question number and your name # the tax amount is a constant value of .085 # the user enters from the keyboard the cost # a discount of 10% will be declared as a constant and given on the cost that was entered # a new variable will be declared called new_cost and will be the cost minus the discount # the tax will be figured out on the new_cost after the discount of 10% is taken off of the cost # the total will be the new_cost plus the tax # the user will enter the amount_paid from the keyboard # the change will be the amount_paid minus the total # and the cost, discount, new cost, tax, total, and amount_paid will all be printed with appropriate format. # include void main() { float cost = 15.50; // The cost of an item float sales_tax = 0.06; // Sales tax is 6 percent float amount_paid = 20.00; //The amount the buyer paid float tax, change, total; // Sales tax, buyer change and total bill tax = cost * sales_tax; total = cost + tax; change =amount_paid – total; cout << “Item Cost: \$” << cost << “ Tax: \$” << tax << “\tTotal: \$” << total << endl; cout << “Customer change: \$” << change << endl; } Sep 10 '06 #1
11 Replies

 Expert Mod 5K+ P: 8,916 In the first problem I feel sure that you can attempt to replace the declaration and assigment of a variable with the declaration of a variable and cout and cin statements to aquire the variables value. cin works as Expand|Select|Wrap|Line Numbers int value;   cin >> value;   cout << "The value entered was: " << value << "\n";   In the second you have very clearly not followed the bullet points you have been given. Salves tax 0.085 (8.5%), cost_new variable = cost minus discount. I suggest you start by correcting these simple errors. Sep 10 '06 #2

 P: 35 I sent you a PM Sep 10 '06 #3

 P: 35 Errors! I got 37 errors for the first problem, how can I correct these errors? Sep 10 '06 #4

 P: 35 I made some changes and I am still getting 28 errors! It's improvement over 37 but still a lot of problems. Updated code so far - Expand|Select|Wrap|Line Numbers #include  void main(void) { int age = 32; float salary = 25000.75; long distance_to_the_moon = 238857; cout << The employee is  << age << “ years old” << endl; cout << The employee makes \$ << salary << endl; cout << The moon is  << distance_to_the_moon << cout << miles from the earth << endl; } Sep 10 '06 #5

 Expert Mod 5K+ P: 8,916 Expand|Select|Wrap|Line Numbers cout << The employee is  << age << “ years old” << endl;   should be Expand|Select|Wrap|Line Numbers cout << "The employee is"  << age << “ years old” << endl;   You've made similar errors in all the other cout statements Sep 11 '06 #6

 P: 35 Expand|Select|Wrap|Line Numbers cout << The employee is  << age << “ years old” << endl;   should be Expand|Select|Wrap|Line Numbers cout << "The employee is"  << age << “ years old” << endl;   You've made similar errors in all the other cout statements I fixed it and I'm going to try second problem now. Sep 11 '06 #7

 P: 35 I am stuck on the second part of the problem, I can't figure out what else is needed to be done. I keep getting three errors. Expand|Select|Wrap|Line Numbers // // # include  void main() { float cost = 15.50; // The cost of an item float sales_tax = 0.06; // Sales tax is 6 percent float amount_paid = 20.00; //The amount the buyer paid float tax, change, total; // Sales tax, buyer change and total bill tax = cost * sales_tax; total = cost + tax; change =amount_paid  total; cout << "Item Cost:" "\$" << cost <<  Tax: \$ << tax << tTotal: "\$" << total << endl; cout << "Customer change:" "\$" << change << endl; } Errors Expand|Select|Wrap|Line Numbers cpp2.cpp(7) : warning C4305: 'initializing' : truncation from 'const double' to 'float' cpp2.cpp(12) : error C2146: syntax error : missing ';' before identifier 'total' cpp2.cpp(13) : error C2065: 'Tax' : undeclared identifier cpp2.cpp(13) : error C2143: syntax error : missing ';' before ':' Error executing cl.exe.   Cpp2.obj - 3 error(s), 1 warning(s)   I am suppose to..... # comments appear at the top of the file indicating the assignment number question number and your name # the tax amount is a constant value of .085 # the user enters from the keyboard the cost # a discount of 10% will be declared as a constant and given on the cost that was entered # a new variable will be declared called new_cost and will be the cost minus the discount # the tax will be figured out on the new_cost after the discount of 10% is taken off of the cost # the total will be the new_cost plus the tax # the user will enter the amount_paid from the keyboard # the change will be the amount_paid minus the total # and the cost, discount, new cost, tax, total, and amount_paid will all be printed with appropriate format. Oct 4 '06 #8

 Expert Mod 5K+ P: 8,916 This warning cpp2.cpp(7) : warning C4305: 'initializing' : truncation from 'const double' to 'float' relates to this code line float cost = 15.50; // The cost of an item a floating point literal (15.50 in this case) has type double. When you assign it to a float you are loosing precission hence the warning. You can declare a floating point literal of type float by appending an F to the number. float cost = 15.50F; // The cost of an item This will remove this warning This error cpp2.cpp(12) : error C2146: syntax error : missing ';' before identifier 'total' relates to this line of code change =amount_paid total; I would say that you have left out a minus sign, you should be able to spot something like this. change =amount_paid - total; These errors cpp2.cpp(13) : error C2065: 'Tax' : undeclared identifier cpp2.cpp(13) : error C2143: syntax error : missing ';' before ':' relate to this line of code cout << "Item Cost:" "\$" << cost << Tax: \$ << tax << You have left out the quotes round the Tax: \$ string cout << "Item Cost:" "\$" << cost << "Tax: \$" << tax << Oct 4 '06 #9

 P: 35 Expand|Select|Wrap|Line Numbers Cpp2.cpp(14) : error C2065: 'tTotal' : undeclared identifier \Cpp2.cpp(14) : error C2143: syntax error : missing ';' before ':' Error executing cl.exe.   Cpp2.obj - 2 error(s), 0 warning(s) Expand|Select|Wrap|Line Numbers tTotal: "\$" << total << endl; What I'm I missing? Oct 4 '06 #10

 Expert Mod 5K+ P: 8,916 What I'm I missing? More " . Oct 4 '06 #11

 P: 35 Got it, thanks for all your help Oct 4 '06 #12