Visual Studio Error Help

I have changed the doubles to char's cause on visual studio it said it didn't complie the code. I have tried changing the doubles and changing it to floats and char's. All i am tryin get to work is the user enter loop that i created. The error I keep getting is LNK2019. Is there anything i need to change or do to get it to compile right.

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

char input;

double getInput(const std::string&);

float purchase, query; // float for user entering their purchase amount

int main() {

std::cout << " +----------------------+" << endl; // sales tax calculator header
std::cout << " | SALES TAX CALCULATOR |" << endl; // sales tax calculator header
std::cout << " +----------------------+" << endl; // sales tax calculator header
std::cout << " Instructions: Please enter an item cost" << endl; // sales tax calculator header
std::cout << " and select a state from the list." << endl; // sales tax calculator header

double purchase = getInput("How much is the item you would like to purchase: $");

double getInput(const std::string& query);


purchase = 0.0;
std::string str_input = "";

// ask the question
std::cout << query;

while (true)
{
// retrieve the entire input and stuff it into a string
std::getline(std::cin, str_input);

try
{
// try to extract a double from the retrieved line of input
// an error is thrown if not a double
purchase = std::stod(str_input);

// the double was successfully retrieved, let's get out of here
break;
}

catch (...)
{
std::cout << "The entered cost is invalid..." << endl << endl << "How much is the item you would like to purchase : $";
continue;
}
}


std::string city[] = { "Maryland", "Virginia", "North Carolina", "South Carolina", "Deleware", "District of Columbia", "Pennsylvania" };
double tax[] = { 6.00, 4.30, 4.75, 6.00, 0.00, 6.00, 6.00 }; // the tax rates I used are from Avalara.com for validation

std::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';
std::cout << "In which state is the purchase being made: ";
int input;
std::cin >> input;

std::cout << endl;
std::cout << std::setfill('-') << std::setw(46) << "-"; // line header
std::cout << endl << "Original Purchase Price : " << "$"; // original purchase price header
std::cout << std::fixed;
std::cout << std::setprecision(2); // adds 2 decimal placing for number variables
std::cout << purchase; // float value from the users price that was entered
std::cout << endl << "Tax Rate"; // tax rate header
std::cout << std::setfill(' ') << std::setw(18) << ": "; // spacing for :
std::cout << std::fixed;
std::cout << std::setprecision(2); // adds 2 decimal placing for number variables
std::cout << tax[input] << "%"; // displays the states tax rate
std::cout << endl << "Sales Tax"; // sales tax header
std::cout << std::setfill(' ') << std::setw(17) << ": "; // spacing for :
std::cout << std::fixed;
std::cout << std::setprecision(2); // adds 2 decimal placing for number variables
std::cout << "$" << tax[input]; // displays what the states tax rate in dollars/ or amount that it is
std::cout << endl << "Total Price"; // total price header
std::cout << std::setfill(' ') << std::setw(15) << ": "; // spacing for :
std::cout << "$" << purchase + tax[input]; // addds both purchase and what the tax input value, that then adds up both total values and displays it as total price

std::cout << endl << endl << "In " << city[input] << ", an item that costs $" << purchase << " will cost $" << purchase + tax[input] << " with sales tax included.";
std::cout << endl;

return 0;
}
Last edited on
Where have you implemented your function double getInput(const std::string&);?
I believe that helps classify the "double getIput(const std::string& query);" that then helps take the user data and display/ and or loop there answer
Last edited on
Okay, but where is it? Have you written it yet?
jlb wrote:
Where have you implemented your function double getInput(const std::string&);?

It's badly implemented within the body of main().

I recognize it as a cut'n'paste of code I've posted previously. No code tags help hide the function body.

Once my code is extracted from main() and positioned properly (with opening and closing braces and adding back the return statement) the link error goes away.

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
double getInput(const std::string& query)
{
   double      purchase  = 0.0;
   std::string str_input = "";

   // ask the question
   std::cout << query;

   while (true)
   {
      std::cout << ": ";

      // retrieve the entire input and stuff it into a string
      std::getline(std::cin, str_input);

      try
      {
         // try to extract a double from the retrieved line of input
         // an error is thrown if not a double
         purchase = std::stod(str_input);

         // the double was successfully retrieved, let's get out of here
         break;
      }

      catch (...)
      {
         std::cout << "The input was invalid...\n\n";
         continue;
      }
   }
   return purchase;
}


How do I know this is my code cut'n'pasted without fully understanding what it does? The variables names and comments. Plus the exception handling.
Topic archived. No new replies allowed.