Well, I thought I had this program working but now I'm getting the above referenced compiler error. The program is just a basic user interface. It is for a classwork assignment.
The program is to accept user information as a string, convert it (if needed) to either the int or double variable, and then display the result. I'm using stringstream convert to make the change between types, but I'm not sure if I'm using it right (that might be what's causing the error, I'm not sure). Line 36-37 generates the error.
Can anyone recommend a way to fix this?
Thanks.
Code:
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
string input, name;
int age;
double mileage;
void DisplayApplicationInformation();
void DisplayDivider(string outputTitle);
string GetInput(string inputType);
int TerminateApplication();
int main()
{
DisplayApplicationInformation();
cout << endl;
DisplayDivider("Start Program");
cout << endl;
DisplayDivider("Get Name");
name = GetInput("Your Name");
cout << "Your name is: " << name << endl;
cout << endl;
DisplayDivider("Get Age");
input = GetInput("Your Age");
stringstream convert(input);
if ( ! (convert >> age) ) age = 0;
cout << "Your age is " << age << endl;
cout << endl;
cout << fixed << showpoint << setprecision(2) << endl;
DisplayDivider("Get Mileage");
input = GetInput("Your Gas Mileage");
stringstream convert(input);
if ( ! (convert >> mileage) ) mileage = 0;
cout << "Your car's MPG is: " << mileage << endl;
cout << endl;
TerminateApplication();
}
void DisplayApplicationInformation() //provides the user with basic program information
{
cout << "Welcome to the Basic User Interface Program" << endl;
cout << "CIS247C, Week 1 Lab" << endl;
cout << "Name: John Worley" << endl;
cout << "This program accepts user input as a string," << endl;
cout << "then makes the appropriate data conversion." << endl;
cout << "Finally the program will display the user input," << endl;
cout << "as a string, integer, or double number as needed." << endl;
}
void DisplayDivider(string outputTitle) //output separator
{
cout <<"**********" << outputTitle << "**********" << endl;
}
string GetInput(string inputType) //general function that prompts user for information then returns info as string
{
string strInput;
cout << "Enter " << inputType << endl;
cin >> strInput;
return strInput;
}
int TerminateApplication() //program termination message and end program
{
cout << "Thank you for using the Basic User Interface program." << endl;
system("pause");
return 0;
}