Sep 7, 2014 at 6:11pm UTC
I am trying to write a program which will prompt the user for the type of wood needed to build a desk and the number of drawers in order to calculate the cost of the desk. I made the function "GetSpecs" to get these values. I want it so that the program prompts the user to put in a p for pine, o for oak, or m for Mahogany. The price for the wood is 100, 140, and 180 dollars, respectively. Each drawer costs $30. Below is what I have so far. I need help to find a way to change the users input of " p,o,or m " to a number
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <sstream>
using std::setw;
using namespace std;
void GetSpecs(int &NumDrawers, int &Choice);
void FinalPrice(int &NumDrawers, int &Choice);
string WoodType[4] = { "" , "Pine" , "Oak" , "Other" };
int WoodCost[4] = { 0, 100, 140, 180 };
int DrawerPrice = 30;
char WoodChoice[4] = { ' ' , 'p' , 'o' , 'm' };
int main()
{
int NumDrawers = 0;
int Choice = 0;
GetSpecs(Choice, NumDrawers);
FinalPrice(Choice, NumDrawers);
system("Pause" );
return 0;
}
void GetSpecs(int &Choice , int &NumDrawers)
{
while (Choice < 1 || (Choice > 3))
{
cout << "What type of wood would you like? 'p' for Pine, 'o' for Oak, or 'm' for Mahogany " ;
cin >> WoodChoice;
if (WoodChoice == 'p' || WoodChoice == 'P' )
{
Choice = 1;
}
if (WoodChoice == 'o' || WoodChoice == 'O' )
{
Choice = 2;
}
if (WoodChoice == 'm' || WoodChoice == 'M' )
{
Choice = 3;
}
else
{
Choice = 0;
}
}
while (NumDrawers <= 0)
{
cout << "How many drawers would you like in your desk? " ;
cin >> NumDrawers;
}
}
void FinalPrice(int &Choice, int &NumDrawers)
{
cout << "\nType of Wood: " << WoodType[Choice] << "\t\t$" << WoodCost[Choice] << endl;
cout << "Number of Drawers: " << NumDrawers << "\t\t$" << NumDrawers*DrawerPrice << endl;
cout << "_____________________________________________" << endl;
cout << "Final Cost: \t\t\t$" << WoodCost[Choice] + NumDrawers*DrawerPrice << "\n" << endl;
}
Last edited on Sep 7, 2014 at 7:17pm UTC
Sep 7, 2014 at 7:35pm UTC
Then use the int directly?
Aceix.
Sep 7, 2014 at 7:42pm UTC
Figured it out.
I needed to make WoodChoice a string and then use getline(cin, WoodChoice); to get the users input.
here is my final code, please let me know if you see somewhere that I messed up.
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <sstream>
#include <string>
using std::setw;
using namespace std;
void GetSpecs(int &NumDrawers, int &Choice);
void FinalPrice(int &NumDrawers, int &Choice);
string WoodType[4] = { "", "Pine", "Oak", "Other" };
int WoodCost[4] = { 0, 100, 140, 180 };
int DrawerPrice = 30;
int main()
{
int NumDrawers = 0;
int Choice = 0;
GetSpecs(Choice, NumDrawers);
FinalPrice(Choice, NumDrawers);
system("Pause");
return 0;
}
void GetSpecs(int &Choice , int &NumDrawers)
{
string WoodChoice;
while (Choice < 1 || (Choice > 3))
{
cout << "What type of wood would you like? 'p' for Pine, 'o' for Oak, or 'm' for Mahogany ";
getline(cin, WoodChoice);
if (WoodChoice == "p")
{
Choice = 1;
}
else if (WoodChoice == "o")
{
Choice = 2;
}
else if (WoodChoice == "m")
{
Choice = 3;
}
else
{
Choice = 0;
}
}
while(NumDrawers <= 0)
{
cout << "How many drawers would you like in your desk? ";
cin >> NumDrawers;
}
}
void FinalPrice(int &Choice, int &NumDrawers)
{
cout << "\nType of Wood: " << WoodType[Choice] << "\t\t$" << WoodCost[Choice] << endl;
cout << "Number of Drawers: " << NumDrawers << "\t\t$" << NumDrawers*DrawerPrice << endl;
cout << "_____________________________________________" << endl;
cout << "Final Cost: \t\t\t$" << WoodCost[Choice] + NumDrawers*DrawerPrice << "\n" << endl;
}