I need to create a program that calculates the price of a table being built.
The main() function should call the following functions:
A function which accepts from standard in the number of chairs to go with the table.
A function which accepts from standard in the surface area (in m2) of the table.
A function which accepts from standard in the type of wood used to build the table and chairs; 'm' for mahogony, 'o' for oak or 'p' for pine. Ideally any other entry should be rejected.
A function that takes the number of chairs (N), the surface area of the table (S) and the type of wood (x) to calculate the price. The price for a table is x(S + 1/2N) where x is $50, $100 and $150 respectively for pine, oak, and mahogony.
A function to display the details of the purchase.
Results at the moment:
At the moment my code will only calculate the price of the table with 'm'(mahogony) and not the other two materials. For example:
Number of chairs: 5
Surface area of table (m2): 5
Wood type: m
Total = 1125
If I change the wood type to 'o' or 'p', the same price will result.
Code:
#include <iostream>
using namespace std;
int main(){
int N;
cout << "Number of chairs: ";
cin >> N;
int S;
cout << "Surface area (in meters squared) of the table: ";
cin >> S;
cout << "Type of wood ('m' = mahogony, 'o' = oak, 'p' = pine): ";
char x;
cin >> x;
while(x != 'm' && x != 'o' && x != 'p'){
cout << "Error. Invalid input. Please try again" << endl;
cout << "Type of wood ('m' = mahogony, 'o' = oak, 'p' = pine): ";
cin >> x;
}
int total;
if(x = 'm'){
total = 150*(S + (0.5*N));
} else if(x = 'o'){
total = 100*(S + (0.5*N));
} else if(x = 'p'){
total = 50*(S + (0.5*N));
}
cout << "--------------------\n";
cout << "Details of purchase:\n";
cout << "--------------------\n";
cout << "Number of chairs: " << N << endl;
cout << "Surface area of table (m2): " << S << endl;
cout << "Wood type: " << x << endl;
cout << "Total = " << total << endl;
return 0;
}
#include <iostream>
using namespace std;
int main(){
int N;
cout << "Number of chairs: ";
cin >> N;
int S;
cout << "Surface area (in meters squared) of the table: ";
cin >> S;
cout << "Type of wood ('m' = mahogony, 'o' = oak, 'p' = pine): ";
char x;
cin >> x;
while(x != 'm' && x != 'o' && x != 'p'){
cout << "Error. Invalid input. Please try again" << endl;
cout << "Type of wood ('m' = mahogony, 'o' = oak, 'p' = pine): ";
cin >> x;
}
int total;
if(x == 'm'){
total = 150*(S + (0.5*N));
} else if(x == 'o'){
total = 100*(S + (0.5*N));
} else if(x == 'p'){
total = 50*(S + (0.5*N));
}
cout << "--------------------\n";
cout << "Details of purchase:\n";
cout << "--------------------\n";
cout << "Number of chairs: " << N << endl;
cout << "Surface area of table (m2): " << S << endl;
cout << "Wood type: " << x << endl;
cout << "Total = " << total << endl;
return 0;
}
You must == to check a condition and = for assigning value
#include <iostream>
using namespace std;
int main(){
float N;
cout << "Number of chairs: ";
cin >> N;
float S;
cout << "Surface area (in meters squared) of the table: ";
cin >> S;
cout << "Type of wood ('m' = mahogony, 'o' = oak, 'p' = pine): ";
char x;
cin >> x;
while(x != 'm' && x != 'o' && x != 'p'){
cout << "Error. Invalid input. Please try again" << endl;
cout << "Type of wood ('m' = mahogony, 'o' = oak, 'p' = pine): ";
cin >> x;
}
float total=0.f;
if(x == 'm'){
total = 150.f*(S + (0.5f*N));
} else if(x == 'o'){
total = 100.f*(S + (0.5f*N));
} else if(x == 'p'){
total = 50.f*(S + (0.5f*N));
}
cout << "--------------------\n";
cout << "Details of purchase:\n";
cout << "--------------------\n";
cout << "Number of chairs: " << N << endl;
cout << "Surface area of table (m2): " << S << endl;
cout << "Wood type: " << x << endl;
cout << "Total = " << total << endl;
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.