experchange > learn.c-c++

chadrobot (12-08-18, 02:29 AM)
Will anyone please tell me how I add 1500 to the solution then
display that. Also to take 15% from that total and add it and display it again.

Thanks

#include <iostream>
#include <string>
#include <algorithm>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::transform;

int main()
{
//declare variable and array
string totalPrice = " ";
string carPackage = "X";
string products[5][2] = { {"BB", "Base Price $1500.00,"},
{"SP", "Sport Price $3250.00",},
{"NP", "Intermediate level Price $4575.00"},
{"HE", "Luxury Price $7500.00"},
{"UC", "User specified price $5220.00"}};
//get ID to search for, then convert to uppercase
cout << "Enter Package Code for Price (X to exit): ";
getline(cin, carPackage);
transform(carPackage.begin(), carPackage.end(),
carPackage.begin(), toupper);

while (carPackage != "X")
{
//locate position of product ID in the
//first column in the array
int row = 0; //keeps track of array subscripts
while (row < 5 && products[row][0] != carPackage)
row = row + 1;
//end while
//if ID was found, display price from the
//second column in the array
//otherwise, display error message
if (row < 4)
cout << "Price for package "
<< products[row][0] <<": "
<< products[row][1] << endl << endl;

else
cout << "Invalid product package" << endl << endl;
//end if
//get ID to search for, then convert to uppercase
getline(cin, carPackage);
transform(carPackage.begin(), carPackage.end(),
carPackage.begin(), toupper);

}

return 0;
} //end of main function
Barry Schwarz (12-08-18, 05:01 PM)
On Fri, 7 Dec 2018 16:29:42 -0800 (PST), chadrobot wrote:

>Will anyone please tell me how I add 1500 to the solution then
>display that. Also to take 15% from that total and add it and display it again.


What do you mean by solution? Currently your only action is to print
a line of text.

Did you mean add to the price after decoding the input? For that, you
need to extract the price from the selected string (there are
functions for that), convert it from character to numeric (there
functions for this also), and then perform the desired arithmetic on
the numeric data.

[..]
> {"UC", "User specified price 20.00"}};
>//get ID to search for, then convert to uppercase
>cout << "Enter Package Code for Price (X to exit): ";


It would be nice if you told the user what the valid codes were.

[..]
> while (row < 5 && products[row][0] != carPackage)
> row = row + 1;
> //end while


Useless comments serve only to clutter the code.

> //if ID was found, display price from the
> //second column in the array
> //otherwise, display error message
> if (row < 4)


Your indentation is misleading. This if is not part of the previous
while.

If row is 4, the user entered UC which is a valid code that you will
not accept.

> cout << "Price for package "
> << products[row][0] <<": "
> << products[row][1] << endl << endl;
> else
> cout << "Invalid product package" << endl << endl;
> //end if
> //get ID to search for, then convert to uppercase
> getline(cin, carPackage);
> transform(carPackage.begin(), carPackage.end(),
> carPackage.begin(), toupper);


The last three lines are not part of the else.

>}


Vertical blank space has its uses but its use should be tempered by
the desire to see as much of the program as possible on a single
screen.
chadrobot (12-08-18, 11:55 PM)
On Saturday, December 8, 2018 at 9:01:03 AM UTC-6, Barry Schwarz wrote:
[..]
> screen.
> --
> Remove del for email

Okay I need to be able to put any random base price for a car with a package code and add them together. Then do the taxes. And display the total.
Similar Threads