| | |
convert int to string
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I am having to write a program that has a user-defined class. In this program I need to convert an INT to a STRING. For example, if the program reads in the date "7/17/2009" it will need to be converted to "July 17, 2009", where all it does is take the month's number value and change it to the string equivalant.
Can somebody help me with the INT to STRING conversion? I can't quite grasp it.
Here is what I have thus far for the implementation of my class:
I know this code is full of errors. I just need assistance with the 'convertMonthNumberToString' function.
Can somebody help me with the INT to STRING conversion? I can't quite grasp it.
Here is what I have thus far for the implementation of my class:
C++ Syntax (Toggle Plain Text)
#include "testerResult.h" #include <iostream> #include <string> using namespace std; Date::Date() { month = 1; day = 1; year = 1800; } Date::Date(int m, int d, int y) { } Date::Date(string m, int d, int y) { Date::convertMonthNumberToString(month); month = m; day = d; year = y; } int Date::compareDates() { return 0; } string Date::convertMonthNumberToString(int month) { switch (month) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; } } int Date::dayNumber() { return 0; } void Date::displayDate() { } void Date::increment() { } void Date::isLeapYear() { if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) { cout << "This is a Leap Year." << endl; } else { cout << "This is not a Leap Year." << endl; } } void Date::isValidDate() { int d = 0; int m = 0; int y = 0; if (d < 1 || d > 31) { cout << "Invalid Date." << endl; } else if (m < 1 || m > 12) { cout << "Invalid Date." << endl; } else if (y < 1800 || y > 2100) { cout << "Invalid Date." << endl; } else { cout << "Valid Date." << endl; } } void Date::setDate(int m, int d, int y) { if(m < 1 || m >12) return; month = m; cout << m; if(d < 1 || d > 31) return; day = d; cout << d; if(y < 1800 || y > 2100) return; year = y; cout << y; } int Date::getDay() const { return day; } int Date::getMonth() const { return month; } int Date::getYear() const { return year; } int Date::getDate() const { return month; return day; return year; }
I know this code is full of errors. I just need assistance with the 'convertMonthNumberToString' function.
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
Don't use class name along with member if that member is an instance (object) member. Static members are qualified with class name only.
You have to define member function convertMonthStringToNumber.
You have to define member function convertMonthStringToNumber.
C++ Syntax (Toggle Plain Text)
Date::Date(string m, int d, int y) { month = convertMonthStringToNumber(m); day = d; year = y; }
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: Jan 2008
Posts: 3,814
Reputation:
Solved Threads: 501
Well, yes, it is full of errors, and keep in mind that sometimes when something is full of errors, the part you are having a hard time with may NOT be the error. The function you are worried about seems fine to me save for one thing:
I personally try to NEVER name my function parameters the same as my class variables. Leave one
[EDIT]
I see someone has beaten me to it and I'm a couple of posts behind. I'll post anyway.
[/EDIT]
string Date::convertMonthNumberToString(int month) { switch (month) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; } }
I personally try to NEVER name my function parameters the same as my class variables. Leave one
this off and you're in a lot of trouble. Since month is a class variable, I'd strongly advise you change the variable in red to m , as you do elsewhere. It gets too confusing otherwise, for me at least. Or perhaps you don't want month passed to this function (i.e. the function should take no parameters)? I don't know. Your function CALL in your constructor is wrong. Since I'm not sure what you're doing here, I won't hazard a guess as to how to fix it.Date::Date(string m, int d, int y)
{
Date::convertMonthNumberToString(month);
month = m;
day = d;
year = y;
}[EDIT]
I see someone has beaten me to it and I'm a couple of posts behind. I'll post anyway.
[/EDIT]
•
•
Join Date: Jan 2008
Posts: 3,814
Reputation:
Solved Threads: 501
Regarding adatapost's suggestion, this isn't going to work:
convertMonthToString takes an int and returns a string. Here, m is a string. Is month an int or a string? Look at your other constructor:
month is an int here. One or both of these constructors is therefore incorrect. Since you didn't post the .h file, we don't know what all the types are.
[EDIT]
Also, the function name got messed up (I did it too). I think you're going to have to repost with more info. Post the .h file at the very least. We need to know the types and what everything represents.
[/EDIT]
C++ Syntax (Toggle Plain Text)
Date::Date(string m, int d, int y) { month = convertMonthStringToNumber(m); day = d; year = y; }
convertMonthToString takes an int and returns a string. Here, m is a string. Is month an int or a string? Look at your other constructor:
C++ Syntax (Toggle Plain Text)
Date::Date() { month = 1; day = 1; year = 1800; }
month is an int here. One or both of these constructors is therefore incorrect. Since you didn't post the .h file, we don't know what all the types are.
[EDIT]
Also, the function name got messed up (I did it too). I think you're going to have to repost with more info. Post the .h file at the very least. We need to know the types and what everything represents.
[/EDIT]
Last edited by VernonDozier; Jul 17th, 2009 at 9:52 pm.
•
•
Join Date: Jan 2008
Posts: 3,814
Reputation:
Solved Threads: 501
•
•
•
•
OP hasn't defined a convertMonthStringToNumber(m) method yet. A class has convertMonthNumberToString method.
C++ Syntax (Toggle Plain Text)
convertMonthNumberToString
which matches the OP's English description:
•
•
•
•
In this program I need to convert an INT to a STRING.
•
•
•
•
You have to define member function convertMonthStringToNumber.
Last edited by VernonDozier; Jul 17th, 2009 at 10:15 pm.
I actually had to change part of it around to get the program to like what adatapost's post was. Since my program accepted it without giving errors I assumed it was correct. I will post the .h program as well as the implementation program. I will try to clean it up as best as possible. It runs, but I haven't quite gotten it to do what I want it to. What I am mainly trying to do is to get all the functions to be able to work, at least partially. From there I will be able to refine my work a little bit and make it work the way it should. But for now I am wanting to focus on converting an INT into a STRING.
I'm sure I could revise that section too. Anyhow, I am required to have at least 13 items in this .h section. They are as follows:
3 Constructors
6 regular member functions as listed in my book
3 data items
1 regular member function named isLeapYear
Here is the implementation portion:
So, at this current time I am just trying to work on converting an INT to a STRING... I will have to remark this post unsolved...
C++ Syntax (Toggle Plain Text)
// Here is the .h section #ifndef DATE_H #define DATE_H #include <string> using namespace std; class Date { public: //Member Functions //Constructors Date(); Date(int,int,int); Date(string,int,int); //is the date valid void isValidDate(); //convert int to string string convertMonthNumberToString(int); //dsplays entire date using convertMonthNum... if necessary void displayDate(); //compare the dates int compareDates(); //display day number int dayNumber(); //increment void increment(); //is it a leap year void isLeapYear(); //setter void setDate(int,int,int); //getter int getMonth() const; int getDay() const; int getYear() const; private: //Data members int month; int day; int year; }; #endif //DATE_H
I'm sure I could revise that section too. Anyhow, I am required to have at least 13 items in this .h section. They are as follows:
3 Constructors
6 regular member functions as listed in my book
3 data items
1 regular member function named isLeapYear
Here is the implementation portion:
C++ Syntax (Toggle Plain Text)
#include "testerResult.h" #include <iostream> #include <string> using namespace std; Date::Date() { month = 1; day = 1; year = 1800; } Date::Date(int m, int d, int y) { month = m; day = d; year = y; } Date::Date(string m, int d, int y) { m = convertMonthNumberToString(month); day = d; year = y; } int Date::compareDates() { return 0; } string Date::convertMonthNumberToString(int month) { switch (month) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; } } int Date::dayNumber() { return 0; } void Date::displayDate() { } void Date::increment() { } void Date::isLeapYear() { if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) { cout << "This is a Leap Year." << endl; } else { cout << "This is not a Leap Year." << endl; } } void Date::isValidDate() { int d = 0; int m = 0; int y = 0; if (d < 1 || d > 31) { cout << "Invalid Date." << endl; } else if (m < 1 || m > 12) { cout << "Invalid Date." << endl; } else if (y < 1800 || y > 2100) { cout << "Invalid Date." << endl; } else { cout << "Valid Date." << endl; } } void Date::setDate(int m, int d, int y) { if(m < 1 || m >12) return; month = m; cout << m; if(d < 1 || d > 31) return; day = d; cout << d; if(y < 1800 || y > 2100) return; year = y; cout << y; } int Date::getMonth() const { return month; } int Date::getDay() const { return day; } int Date::getYear() const { return year; }
So, at this current time I am just trying to work on converting an INT to a STRING... I will have to remark this post unsolved...
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
•
•
Join Date: Jan 2008
Posts: 3,814
Reputation:
Solved Threads: 501
Read my earlier post regarding naming a parameter the same name as your class variable. Here is your function:
Here is what I assume your function should be (note intentional lack of function parameters) :
Having a local variable with the same name as a class variable always makes me screw everything up, so I avoid them like the plague. Others may be able to keep them straight better. At any rate, you have NOT declared this a static function, which you could do if month in this case was intended to not be the class variable. That's why I am guessing that the function takes no parameters. I don't know whether you have learned static versus non-static functions and variables yet. If not, probably don't worry about it and keep everything non-static.
This constructor doesn't make any sense:
month isn't initialized, so you shouldn't be passing it to a function. m is immediately overwritten, and nothing is ever done with m, so it's a local variable that goes out of scope after the constructor is done executing.
Question: Are you supposed to STORE any strings anywhere? Right now, you have no class variables of type string.
C++ Syntax (Toggle Plain Text)
string Date::convertMonthNumberToString(int month) { switch (month) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; } }
Here is what I assume your function should be (note intentional lack of function parameters) :
C++ Syntax (Toggle Plain Text)
string Date::convertMonthNumberToString() { switch (month) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; } }
Having a local variable with the same name as a class variable always makes me screw everything up, so I avoid them like the plague. Others may be able to keep them straight better. At any rate, you have NOT declared this a static function, which you could do if month in this case was intended to not be the class variable. That's why I am guessing that the function takes no parameters. I don't know whether you have learned static versus non-static functions and variables yet. If not, probably don't worry about it and keep everything non-static.
This constructor doesn't make any sense:
C++ Syntax (Toggle Plain Text)
Date::Date(string m, int d, int y) { m = convertMonthNumberToString(month); day = d; year = y; }
month isn't initialized, so you shouldn't be passing it to a function. m is immediately overwritten, and nothing is ever done with m, so it's a local variable that goes out of scope after the constructor is done executing.
Question: Are you supposed to STORE any strings anywhere? Right now, you have no class variables of type string.
Last edited by VernonDozier; Jul 18th, 2009 at 12:36 am.
![]() |
Similar Threads
- int to string problem, need help quick (Java)
- Int to String converstion breaking in function (C++)
- convert int to string (C++)
- Convert Int to string classic asp (ASP)
- convert int to string (C)
Other Threads in the C++ Forum
- Previous Thread: Question about Winsock2
- Next Thread: how to compile and execute a program
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






