float percentage (C++)

I am tring to make a flaot that can work both as a list and a indicater. The list is for each of the states (made floats form them), then to allow each state to have its own percent values within itself. I am confused of the part cause when i tried it just displayed 1 and not the displayed the number value and what the taxe rate for it is. What is the easist way that I can get a float to hold a number value so when the user plugs in that number. It then will take the price number and add a tax (percentage value with it) to it.

#include "pch.h"
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <math.h>
#include <string>
using namespace std;

char input;

float purchase; // float for user entering their purchase amount
float state_list, tax_rate, sales_taxe, total_price;
float maryland, virginia, NC, SC, deleware, DOC, pennsylvania;

int main () {

if (!cin) { // figure out the purchase == no number value so loop works correctly

cin.clear(); // reset failbit
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Please select a state from the following list:";
}

else {

cout << "The entered cost is invalid..." << endl;
cout << endl << "How much is the item you would like to purchase: $";
cin >> purchase;
cout << "Please select a state from the following list:" << endl;
}

cout << endl << "1.) Maryland" << endl;
cout << "2.) Virginia" << endl;
cout << "3.) North Carolina" << endl;
cout << "4.) South Carolina" << endl;
cout << "5.) Deleware" << endl;
cout << "6.) District of Columbia" << endl;
cout << "7.) Pennsylvania" << endl;
cout << "In which state is the purchase being made : ";
cin >> state_list;

cout << endl;
std::cout << std::setfill('-') << std::setw(46) << "-"; // line header

cout << endl << "Original Purchase Price : " << "$" << purchase;
cout << endl << "Tax Rate";
std::cout << std::setfill(' ') << std::setw(18) << ": ";
cout << endl << "Sales Tax";
std::cout << std::setfill(' ') << std::setw(17) << ": ";
cout << endl << "Total Price";
std::cout << std::setfill(' ') << std::setw(15) << ": ";

cout << endl;

}
Last edited on
1
2
3
4
5
6
7
8
9
std::string city[] = {"Maryland", "Virginia", /*...*/ };
double tax[] = {0.21, 0.31, 0.01, /*...*/};

cout << "Please select a state from the following list:" << endl;
for(size_t K=0; K<std::size(city); ++K)
   std::cout << K << "). " << city[K] << '\n';
int input;
std::cin >> input;
std::cout << "City " << city[input] << " has a tax rate of " << tax[input] << '\n';
I would add 1 to all the taxes if you are going to use it to make tax.

5 dollars and 10% tax?
5 * 1.1 = 5.50...

its<cmath> and <cstdio>. the C headers that you used (.h versions) work but can make trouble in larger projects due to namespaces.






Topic archived. No new replies allowed.