Error in casting
Apr 26, 2010 at 7:19am UTC
I was trying to cast the string to a double in the following program. Unfortunately, I get the following error
========================================================================
1>------ Build started: Project: TimeArb_Simple_1, Configuration: Debug Win32 ------
1>Compiling...
1>Main_1.cpp
1>h:\simple_1\main_1.cpp(22) : error C2065: 'rate1' : undeclared identifier
1>h:\simple_1\main_1.cpp(22) : error C2440: 'type cast' : 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>h:\simple_1\main_1.cpp(23) : error C2065: 'rate2' : undeclared identifier
1>h:\simple_1\main_1.cpp(23) : error C2440: 'type cast' : 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>h:\simple_1\main_1.cpp(24) : error C2065: 'rate1' : undeclared identifier
1>h:\simple_1\main_1.cpp(24) : error C2065: 'rate2' : undeclared identifier
1>h:\simple_1\main_1.cpp(26) : error C2065: 'rate1' : undeclared identifier
1>h:\simple_1\main_1.cpp(26) : error C2065: 'rate2' : undeclared identifier
1>Build log was saved at "file://h:\Simple_1\Debug\BuildLog.htm"
1>TimeArb_Simple_1 - 8 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========================================================================
My code is below. I tried to cast the sting to a double but it didn't work. Can anyone please help me with this?
======================================================================
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
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;
string MaxNum(string r1, string r2);
string irate1;
string irate2;
void main()
{ cout << "Please input the first number" << endl;
cin >> irate1;
cout << "Please input the second number" << endl;
cin >> irate2;
cout << irate1 << MaxNum(irate1, irate2) << irate2;
}
string MaxNum(string r1, string r2)
{
rate1 = (double )r1;
rate2 = (double )r2;
if (rate1 > rate2)
return " is greater than " ;
else if (rate1 < rate2)
return " is less than " ;
else
return " is equal to " ;
}
======================================================================
Apr 26, 2010 at 7:19am UTC
BTw, in the above code, I tried using atof as well but it didn't work.
Apr 26, 2010 at 8:10am UTC
You need to initialize variables rate1 and rate2 in MaxNum(). Also, when atof() takes a const char* as parameter, so you need to extract it from the string object. For example:
1 2
double rate1 = atof(r1.c_str());
double rate2 = atof(r1.c_str());
Also, the return type of main() must be int.
Apr 26, 2010 at 4:53pm UTC
Thank you so much for your help.
Apr 26, 2010 at 7:32pm UTC
Why are you storing double numbers in a string anyway? You do realize that just because your function is of type string, that doesn't mean the parameters have to be strings, too... if you're passing in a value that's of type double, use DOUBLE parameters... and if you want to return a value, make sure that value is of the same type as the function!
Here's a better way to do what you're doing...
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 34 35 36 37 38
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void biggerNum(double first, double second);
int main()
{
double first, second;
cout << "What are the two numbers? " ;
cin >> first >> second;
biggerNum(first, second);
return 0;
}
void biggerNum(double first, double second)
{
if (first > second)
{
cout << first << " is greater than " << second << "." << endl;
}
else if (first < second)
{
cout << first << " is less than " << second << "." << endl;
}
else if (first == second)
{
cout << first << " is equal to " << second << "." << endl;
}
else
{
cout << "Invalid input." << endl;
}
}
Also, don't use global variables :(
Last edited on Apr 26, 2010 at 7:41pm UTC
Topic archived. No new replies allowed.