convert int to string

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 45
Reputation: _dragonwolf_ is an unknown quantity at this point 
Solved Threads: 0
_dragonwolf_'s Avatar
_dragonwolf_ _dragonwolf_ is offline Offline
Light Poster

convert int to string

 
0
  #1
Jul 17th, 2009
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:
  1. #include "testerResult.h"
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. Date::Date()
  8. {
  9. month = 1;
  10. day = 1;
  11. year = 1800;
  12. }
  13.  
  14. Date::Date(int m, int d, int y)
  15. {
  16. }
  17.  
  18. Date::Date(string m, int d, int y)
  19. {
  20. Date::convertMonthNumberToString(month);
  21.  
  22. month = m;
  23. day = d;
  24. year = y;
  25. }
  26.  
  27. int Date::compareDates()
  28. {
  29.  
  30. return 0;
  31. }
  32.  
  33. string Date::convertMonthNumberToString(int month)
  34. {
  35. switch (month)
  36. {
  37. case 1: return "January";
  38. case 2: return "February";
  39. case 3: return "March";
  40. case 4: return "April";
  41. case 5: return "May";
  42. case 6: return "June";
  43. case 7: return "July";
  44. case 8: return "August";
  45. case 9: return "September";
  46. case 10: return "October";
  47. case 11: return "November";
  48. case 12: return "December";
  49.  
  50. }
  51. }
  52.  
  53. int Date::dayNumber()
  54. {
  55.  
  56. return 0;
  57. }
  58.  
  59. void Date::displayDate()
  60. {
  61. }
  62.  
  63. void Date::increment()
  64. {
  65. }
  66.  
  67. void Date::isLeapYear()
  68. {
  69. if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
  70. {
  71. cout << "This is a Leap Year." << endl;
  72. }
  73. else
  74. {
  75. cout << "This is not a Leap Year." << endl;
  76. }
  77. }
  78.  
  79. void Date::isValidDate()
  80. {
  81. int d = 0;
  82. int m = 0;
  83. int y = 0;
  84.  
  85. if (d < 1 || d > 31)
  86. {
  87. cout << "Invalid Date." << endl;
  88. }
  89. else if (m < 1 || m > 12)
  90. {
  91. cout << "Invalid Date." << endl;
  92. }
  93. else if (y < 1800 || y > 2100)
  94. {
  95. cout << "Invalid Date." << endl;
  96. }
  97. else
  98. {
  99. cout << "Valid Date." << endl;
  100. }
  101. }
  102.  
  103. void Date::setDate(int m, int d, int y)
  104. {
  105. if(m < 1 || m >12) return;
  106. month = m;
  107. cout << m;
  108. if(d < 1 || d > 31) return;
  109. day = d;
  110. cout << d;
  111. if(y < 1800 || y > 2100) return;
  112. year = y;
  113. cout << y;
  114. }
  115.  
  116. int Date::getDay() const
  117. {
  118. return day;
  119. }
  120.  
  121. int Date::getMonth() const
  122. {
  123. return month;
  124. }
  125.  
  126. int Date::getYear() const
  127. {
  128. return year;
  129. }
  130.  
  131.  
  132. int Date::getDate() const
  133. {
  134. return month;
  135. return day;
  136. return year;
  137. }

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 464
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: convert int to string

 
1
  #2
Jul 17th, 2009
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.
  1. Date::Date(string m, int d, int y)
  2. {
  3. month = convertMonthStringToNumber(m);
  4. day = d;
  5. year = y;
  6. }
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 45
Reputation: _dragonwolf_ is an unknown quantity at this point 
Solved Threads: 0
_dragonwolf_'s Avatar
_dragonwolf_ _dragonwolf_ is offline Offline
Light Poster

Re: convert int to string

 
0
  #3
Jul 17th, 2009
This helped a lot. Thanks a lot. I appreciate it.
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,814
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: convert int to string

 
0
  #4
Jul 17th, 2009
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:

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]
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,814
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: convert int to string

 
0
  #5
Jul 17th, 2009
Regarding adatapost's suggestion, this isn't going to work:

  1. Date::Date(string m, int d, int y)
  2. {
  3. month = convertMonthStringToNumber(m);
  4. day = d;
  5. year = y;
  6. }

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:

  1. Date::Date()
  2. {
  3. month = 1;
  4. day = 1;
  5. year = 1800;
  6. }

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 464
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: convert int to string

 
0
  #6
Jul 17th, 2009
OP hasn't defined a convertMonthStringToNumber(m) method yet. A class has convertMonthNumberToString method.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,814
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: convert int to string

 
0
  #7
Jul 17th, 2009
Originally Posted by adatapost View Post
OP hasn't defined a convertMonthStringToNumber(m) method yet. A class has convertMonthNumberToString method.
Right, anyway, the OP marked it solved, so I guess somehow it is. it just seemed from the description that the OP actually named it correctly the first time:

  1. convertMonthNumberToString

which matches the OP's English description:

In this program I need to convert an INT to a STRING.
The function you suggested converts a STRING to an INT.

You have to define member function convertMonthStringToNumber.
Anyway, it's marked "Solved", so I guess the OP in fact needed what you suggested, so your reading of the problem was apparently the correct one.
Last edited by VernonDozier; Jul 17th, 2009 at 10:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 45
Reputation: _dragonwolf_ is an unknown quantity at this point 
Solved Threads: 0
_dragonwolf_'s Avatar
_dragonwolf_ _dragonwolf_ is offline Offline
Light Poster

Re: convert int to string

 
0
  #8
Jul 17th, 2009
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.

  1. // Here is the .h section
  2. #ifndef DATE_H
  3. #define DATE_H
  4.  
  5. #include <string>
  6. using namespace std;
  7.  
  8. class Date
  9. {
  10. public:
  11. //Member Functions
  12. //Constructors
  13. Date();
  14. Date(int,int,int);
  15. Date(string,int,int);
  16.  
  17. //is the date valid
  18. void isValidDate();
  19.  
  20. //convert int to string
  21. string convertMonthNumberToString(int);
  22.  
  23. //dsplays entire date using convertMonthNum... if necessary
  24. void displayDate();
  25.  
  26. //compare the dates
  27. int compareDates();
  28.  
  29. //display day number
  30. int dayNumber();
  31.  
  32. //increment
  33. void increment();
  34.  
  35. //is it a leap year
  36. void isLeapYear();
  37.  
  38. //setter
  39. void setDate(int,int,int);
  40.  
  41. //getter
  42. int getMonth() const;
  43. int getDay() const;
  44. int getYear() const;
  45.  
  46. private:
  47. //Data members
  48. int month;
  49. int day;
  50. int year;
  51. };
  52. #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:
  1. #include "testerResult.h"
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. Date::Date()
  8. {
  9. month = 1;
  10. day = 1;
  11. year = 1800;
  12. }
  13.  
  14. Date::Date(int m, int d, int y)
  15. {
  16. month = m;
  17. day = d;
  18. year = y;
  19. }
  20.  
  21. Date::Date(string m, int d, int y)
  22. {
  23. m = convertMonthNumberToString(month);
  24. day = d;
  25. year = y;
  26. }
  27.  
  28. int Date::compareDates()
  29. {
  30.  
  31. return 0;
  32. }
  33.  
  34. string Date::convertMonthNumberToString(int month)
  35. {
  36. switch (month)
  37. {
  38. case 1: return "January";
  39. case 2: return "February";
  40. case 3: return "March";
  41. case 4: return "April";
  42. case 5: return "May";
  43. case 6: return "June";
  44. case 7: return "July";
  45. case 8: return "August";
  46. case 9: return "September";
  47. case 10: return "October";
  48. case 11: return "November";
  49. case 12: return "December";
  50.  
  51. }
  52. }
  53.  
  54. int Date::dayNumber()
  55. {
  56.  
  57. return 0;
  58. }
  59.  
  60. void Date::displayDate()
  61. {
  62. }
  63.  
  64. void Date::increment()
  65. {
  66. }
  67.  
  68. void Date::isLeapYear()
  69. {
  70. if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
  71. {
  72. cout << "This is a Leap Year." << endl;
  73. }
  74. else
  75. {
  76. cout << "This is not a Leap Year." << endl;
  77. }
  78. }
  79.  
  80. void Date::isValidDate()
  81. {
  82. int d = 0;
  83. int m = 0;
  84. int y = 0;
  85.  
  86. if (d < 1 || d > 31)
  87. {
  88. cout << "Invalid Date." << endl;
  89. }
  90. else if (m < 1 || m > 12)
  91. {
  92. cout << "Invalid Date." << endl;
  93. }
  94. else if (y < 1800 || y > 2100)
  95. {
  96. cout << "Invalid Date." << endl;
  97. }
  98. else
  99. {
  100. cout << "Valid Date." << endl;
  101. }
  102. }
  103.  
  104. void Date::setDate(int m, int d, int y)
  105. {
  106. if(m < 1 || m >12) return;
  107. month = m;
  108. cout << m;
  109. if(d < 1 || d > 31) return;
  110. day = d;
  111. cout << d;
  112. if(y < 1800 || y > 2100) return;
  113. year = y;
  114. cout << y;
  115. }
  116.  
  117.  
  118. int Date::getMonth() const
  119. {
  120. return month;
  121. }
  122.  
  123. int Date::getDay() const
  124. {
  125. return day;
  126. }
  127.  
  128. int Date::getYear() const
  129. {
  130. return year;
  131. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,814
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: convert int to string

 
0
  #9
Jul 18th, 2009
Read my earlier post regarding naming a parameter the same name as your class variable. Here is your function:

  1. string Date::convertMonthNumberToString(int month)
  2. {
  3. switch (month)
  4. {
  5. case 1: return "January";
  6. case 2: return "February";
  7. case 3: return "March";
  8. case 4: return "April";
  9. case 5: return "May";
  10. case 6: return "June";
  11. case 7: return "July";
  12. case 8: return "August";
  13. case 9: return "September";
  14. case 10: return "October";
  15. case 11: return "November";
  16. case 12: return "December";
  17. }
  18. }

Here is what I assume your function should be (note intentional lack of function parameters) :

  1. string Date::convertMonthNumberToString()
  2. {
  3. switch (month)
  4. {
  5. case 1: return "January";
  6. case 2: return "February";
  7. case 3: return "March";
  8. case 4: return "April";
  9. case 5: return "May";
  10. case 6: return "June";
  11. case 7: return "July";
  12. case 8: return "August";
  13. case 9: return "September";
  14. case 10: return "October";
  15. case 11: return "November";
  16. case 12: return "December";
  17. }
  18. }

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:

  1. Date::Date(string m, int d, int y)
  2. {
  3. m = convertMonthNumberToString(month);
  4. day = d;
  5. year = y;
  6. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 45
Reputation: _dragonwolf_ is an unknown quantity at this point 
Solved Threads: 0
_dragonwolf_'s Avatar
_dragonwolf_ _dragonwolf_ is offline Offline
Light Poster

Re: convert int to string

 
0
  #10
Jul 18th, 2009
Not quite sure about storing any strings anywhere. However, the alterations that you said to make worked perfectly. Thanks. That solves that issue. I can call this issue solved now. I appreciate it.
With the dragons I fly and with wolves I run through the lands of myth and worlds unknown.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC