Trying to learn C++ and I can't figure out how to fix the errors in this code. It's supposed to convert the int into a string and display the result.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//declare prototypes
void DisplayApplicationInformation();
void DisplayDivider();
int GetInput();
void terminateApplication();
int main(){
//declare variables
string input;
string name;
int age;
double mileage;
DisplayApplicationInformation();
DisplayDivider("Start Program");
DisplayDivider("Get Name");
name = GetInput("Your Name");
cout << "Your name is " << name << endl;
DisplayDivider("Get Age");
input = GetInput("Your Age");
age = input;
cout << "Your age is " << age << endl;
DisplayDivider("Get Mileage");
input = GetInput("Gas Mileage");
mileage = input;
cout << "Your car MPG is " << mileage << endl;
TerminateApplication();
}
void DisplayApplicationInformation(){
cout << "Welcome to the Basic User Interface Program" << endl;
cout << "This program accepts user input as a string, then makes the appropriate data conversion" << endl;
}
You declare the function prototype displaydivider like this void DisplayDivider();
indicating that it takes no input parameters, but then you try to use it like this DisplayDivider("Start Program");
which is an attempt to pass in an input parameter. Your prototype must match the use and the definition.
Likewise for int GetInput()
age = input;age is an int, input is a string. It makes no sense to say that an integer is equal to a string and the compiler rightfully objects because it knows of no way to convert a string to an int.
mileage = input;mileage is an double, input is a string. It makes no sense to say that a double is equal to a string and the compiler rightfully objects because it knows of no way to convert a string to a double.
TerminateApplication();
You're trying to call this function before the compiler knows about it. You must put the prototype or definition before you try to use it.
return input;input is a stringstream in this case but your function GetInput() is supposed to return an int.
All these errors can be deduced simply by reading the error list the compiler gives you. Here's an example of such an error list, giving line numbers and a description of the error. The compiler error list should be what you look at first when code doesn't compile.
: In function ‘int main()’:
:8: error: too many arguments to function ‘void DisplayDivider()’
:22: error: at this point in file
:8: error: too many arguments to function ‘void DisplayDivider()’
:24: error: at this point in file
:9: error: too many arguments to function ‘int GetInput()’
:25: error: at this point in file
:8: error: too many arguments to function ‘void DisplayDivider()’
:28: error: at this point in file
:9: error: too many arguments to function ‘int GetInput()’
:29: error: at this point in file
:30: error: cannot convert ‘std::string’ to ‘int’ in assignment
:8: error: too many arguments to function ‘void DisplayDivider()’
:33: error: at this point in file
:9: error: too many arguments to function ‘int GetInput()’
:34: error: at this point in file
:35: error: cannot convert ‘std::string’ to ‘double’ in assignment
:38: error: ‘TerminateApplication’ was not declared in this scope
: In function ‘int GetInput()’:
:57: error: invalid conversion from ‘void*’ to ‘int’
Here is the error listing I am receiving for the program. I attempted to decalre the function prototype with the parameters but it wouldn't work either.
1>test.cpp(28): error C2660: 'DisplayDivider' : function does not take 1 arguments
1>test.cpp(30): error C2660: 'DisplayDivider' : function does not take 1 arguments
1>test.cpp(31): error C2660: 'GetInput' : function does not take 1 arguments
1>test.cpp(34): error C2660: 'DisplayDivider' : function does not take 1 arguments
1>test.cpp(35): error C2660: 'GetInput' : function does not take 1 arguments
1>test.cpp(36): error C2440: '=' : cannot convert from 'std::string' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>test.cpp(39): error C2660: 'DisplayDivider' : function does not take 1 arguments
1>test.cpp(40): error C2660: 'GetInput' : function does not take 1 arguments
1>test.cpp(41): error C2440: '=' : cannot convert from 'std::string' to 'double'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>test.cpp(44): error C3861: 'TerminateApplication': identifier not found
1>test.cpp(65): error C2440: 'return' : cannot convert from 'std::stringstream' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
I don't know where to begin. You need to really sit down and go through your code. The errors tell you the lines which need to be fixed so you should start by going from the top error (since other errors could occur from other errors, the top is the best place to start).
Everyone covered the first two major errors in your code. You have two functions that are declared as passing void parameters BUT you passed parameters so you need to either not pass anything or declare them properly. That covers your first two errors which are actually the first five compiler errors on that list and seven through eight.
the sixth error, C2440 on line 36 is stating you are trying to convert a string to an int. Well, lets take a look at the line.
input = GetInput("Gas Mileage");
GetInput returns a type int while input is declared as a string, that won't work at all! You can't do this. If you want to convert an int to a string, you need ostringstream (stringstream works but ostringstream is better for the one way conversion.
1 2 3 4 5
int x = 500;
string s;
ostringstream o;
o << x;
s = o.str();
That is how ostringstream works. istringstream is the reverse, converting a string into other variables.
1 2 3 4
int x;
string s = "500";
istringstream i(s);
i >> x;
That should help you tweak your program since you didn't have it converting right anyways.
Thank everyone with the help so far. I'm taking a class to learn C++ and I'm trying to stay sharp on the subject before continuing onto the next class.
I've fixed the function prototype errors and moved the variables from the main function and made them global variables. I tried implementing the ostringstream and I can't seem to figure out how to do it correctly, whether in the main function for each one of the error places or the GetInput function.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//declare prototypes
void DisplayApplicationInformation();
void DisplayDivider(string);
int GetInput(string);
void TerminateApplication();
//declare global variables
string input;
string name;
int age;
double mileage;
int main(){
DisplayApplicationInformation();
DisplayDivider("Start Program");
DisplayDivider("Get Name");
name = GetInput("Your Name");
cout << "Your name is " << name << endl;
DisplayDivider("Get Age");
input = GetInput("Your Age");
age = input;
cout << "Your age is " << age << endl;
DisplayDivider("Get Mileage");
input = GetInput("Gas Mileage");
mileage = input;
cout << "Your car MPG is " << mileage << endl;
TerminateApplication();
}
void DisplayApplicationInformation(){
cout << "Welcome to the Basic User Interface Program" << endl;
cout << "This program accepts user input as a string, then makes the appropriate data conversion" << endl;
}
void TerminateApplication(){
cout << "Thank you for using the Basic User Interface Program" << endl;
}
1>test.cpp(36): error C2440: '=' : cannot convert from 'std::string' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>test.cpp(41): error C2440: '=' : cannot convert from 'std::string' to 'double'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>test.cpp(65): error C2440: 'return' : cannot convert from 'std::stringstream' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called