By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,364 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
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 <iostream.h>
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 <iostream.h>
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
Share this Question
Share on Google+
11 Replies


Banfa
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
  1. int value;
  2.  
  3. cin >> value;
  4.  
  5. cout << "The value entered was: " << value << "\n";
  6.  

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
  1. #include <iostream.h>
  2. void main(void)
  3. {
  4. int age = 32;
  5. float salary = 25000.75;
  6. long distance_to_the_moon = 238857;
  7. cout << The employee is  << age << “ years old” << endl;
  8. cout << The employee makes $ << salary << endl;
  9. cout << The moon is  << distance_to_the_moon <<
  10. cout << miles from the earth << endl;
  11. }
Sep 10 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. cout << The employee is  << age << “ years old” << endl;
  2.  
should be

Expand|Select|Wrap|Line Numbers
  1. cout << "The employee is"  << age << “ years old” << endl;
  2.  
You've made similar errors in all the other cout statements
Sep 11 '06 #6

P: 35
Expand|Select|Wrap|Line Numbers
  1. cout << The employee is  << age << “ years old” << endl;
  2.  
should be

Expand|Select|Wrap|Line Numbers
  1. cout << "The employee is"  << age << “ years old” << endl;
  2.  
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
  1. //
  2. //
  3. # include <iostream.h>
  4. void main()
  5. {
  6. float cost = 15.50; // The cost of an item
  7. float sales_tax = 0.06; // Sales tax is 6 percent
  8. float amount_paid = 20.00; //The amount the buyer paid
  9. float tax, change, total; // Sales tax, buyer change and total bill
  10. tax = cost * sales_tax;
  11. total = cost + tax;
  12. change =amount_paid  total;
  13. cout << "Item Cost:" "$" << cost <<  Tax: $ << tax <<
  14. tTotal: "$" << total << endl;
  15. cout << "Customer change:" "$" << change << endl;
  16. }
Errors
Expand|Select|Wrap|Line Numbers
  1. cpp2.cpp(7) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
  2. cpp2.cpp(12) : error C2146: syntax error : missing ';' before identifier 'total'
  3. cpp2.cpp(13) : error C2065: 'Tax' : undeclared identifier
  4. cpp2.cpp(13) : error C2143: syntax error : missing ';' before ':'
  5. Error executing cl.exe.
  6.  
  7. Cpp2.obj - 3 error(s), 1 warning(s)
  8.  
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

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

Banfa
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

Post your reply

Sign in to post your reply or Sign up for a free account.