Warning on Overloaded stream operators

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

Join Date: Mar 2008
Posts: 26
Reputation: TheFueley is an unknown quantity at this point 
Solved Threads: 0
TheFueley's Avatar
TheFueley TheFueley is offline Offline
Light Poster

Warning on Overloaded stream operators

 
0
  #1
Apr 23rd, 2008
This one of those typical Rational number programs. It takes a user-supplied fraction and then another. Then it does the 4 basic math operations on it. Finally it compares the fractions to each other. I have the program working fine. I just want to know how to fix this warning message. I don't quite know "what to return" from this block of code. TIA!
Wow, after all that I forgot to add the warning message:
  1. warning: control reaches end of non-void function

  1. //allows cin >> f; (f is a fraction)
  2. istream &operator>>(istream &input, Rational &f)
  3. {
  4. string stringy;
  5.  
  6. //stringify the input stream
  7. input >> stringy;
  8.  
  9. //find where the "/" is at in the string
  10. int marker = stringy.find_first_of("/");
  11.  
  12. //numerator is from start of number to "/"
  13. string top = stringy.substr(0, marker);
  14.  
  15. //denominator is from "/" to end
  16. string bottom = stringy.substr(marker + 1, stringy.size() - marker);
  17.  
  18. //convert strings to ints
  19. istringstream up(top);
  20. int topper;
  21. up >> topper;
  22. istringstream low(bottom);
  23. int lower;
  24. low >> lower;
  25.  
  26. //set fraction
  27. f.numer = topper;
  28. f.denom = lower;
  29. }

Here is all the code, for reference. It is separated into 3 files. Header, implementation, and driver.

  1. //Rational.h
  2. #ifndef RATIONAL_H_
  3. #define RATIONAL_H_
  4.  
  5. #include <iostream>
  6. using std::istream;
  7. using std::ostream;
  8.  
  9. class Rational
  10. {
  11. friend istream &operator>>(istream &, Rational&);
  12. friend ostream &operator<<(ostream &, const Rational&);
  13. public:
  14. Rational(int = 0, int = 1);
  15.  
  16. Rational operator+(const Rational &);
  17. Rational operator-(const Rational &);
  18. Rational operator*(const Rational &);
  19. Rational operator/(const Rational &);
  20.  
  21. bool operator==(const Rational &) const;
  22. bool operator!=(const Rational &) const;
  23. bool operator>(const Rational &) const;
  24. bool operator<(const Rational &) const;
  25. bool operator>=(const Rational &) const;
  26. bool operator<=(const Rational &) const;
  27.  
  28. private:
  29. int numer;
  30. int denom;
  31. void simplify();
  32. };
  33. #endif /*RATIONAL_H_*/
  34.  
  35.  
  36. //Rational.cpp
  37. #include <iostream>
  38. using std::cout;
  39. using std::istream;
  40. using std::ostream;
  41.  
  42. #include <string>
  43. using std::string;
  44.  
  45. #include <sstream>
  46. using std::istringstream;
  47.  
  48. #include "Rational.h"
  49.  
  50. //No Denominators <= 0
  51. Rational::Rational(int n, int d)
  52. {
  53. if (d==0)
  54. {
  55. cout << "Illegal Denominator of 0, or negative Denominator\n";
  56. cout << "Setting Denominator to default 1\n";
  57. d = 1;
  58. }
  59. else if (d<0)
  60. {
  61. cout << "Denominator is Negative...\n";
  62. cout << "Reversing Sign of Numerator and Denominator\n";
  63. n = -n;
  64. d = -d;
  65. }
  66.  
  67. numer = n;
  68. denom = d;
  69. }
  70.  
  71. //allows cin >> f; (f is a fraction)
  72. istream &operator>>(istream &input, Rational &f)
  73. {
  74. string stringy;
  75.  
  76. //stringify the input stream
  77. input >> stringy;
  78.  
  79. //find where the "/" is at in the string
  80. int marker = stringy.find_first_of("/");
  81.  
  82. //numerator is from start of number to "/"
  83. string top = stringy.substr(0, marker);
  84.  
  85. //denominator is from "/" to end
  86. string bottom = stringy.substr(marker + 1, stringy.size() - marker);
  87.  
  88. //convert strings to ints
  89. istringstream up(top);
  90. int topper;
  91. up >> topper;
  92. istringstream low(bottom);
  93. int lower;
  94. low >> lower;
  95.  
  96. //set fraction
  97. f.numer = topper;
  98. f.denom = lower;
  99. }
  100.  
  101. //allows cout << f; (f is a fraction)
  102. ostream &operator<<(ostream &output, const Rational &f)
  103. {
  104. return output << f.numer << "/" << f.denom;
  105. }
  106.  
  107. //Add 2 Rationals
  108. Rational Rational::operator+(const Rational &add)
  109. {
  110. Rational temp;
  111.  
  112. temp.numer = add.numer * denom;
  113. temp.numer += add.denom * numer;
  114. temp.denom = add.denom * denom;
  115. temp.simplify();
  116. return temp;
  117. }
  118.  
  119. //Subtract 2 Rationals
  120. Rational Rational::operator-(const Rational &sub)
  121. {
  122. Rational temp;
  123.  
  124. temp.numer = sub.denom * numer;
  125. temp.numer -= denom * sub.numer;
  126. temp.denom = sub.denom * denom;
  127. temp.simplify();
  128. return temp;
  129. }
  130.  
  131. //Multiply 2 Rationals
  132. Rational Rational::operator*(const Rational &mult)
  133. {
  134. Rational temp;
  135.  
  136. temp.numer = mult.numer * numer;
  137. temp.denom = mult.denom * denom;
  138. temp.simplify();
  139. return temp;
  140. }
  141.  
  142. //Divide 2 Rationals
  143. Rational Rational::operator/(const Rational &div)
  144. {
  145. Rational temp;
  146. temp.numer = div.denom * numer;
  147. temp.denom = denom * div.numer;
  148. temp.simplify();
  149. return temp;
  150. }
  151.  
  152. //Test Rational == Rational
  153. bool Rational::operator==(const Rational &eq) const
  154. {
  155. return (numer == eq.numer && denom == eq.denom);
  156. }
  157.  
  158. //Test Rational != Rational
  159. bool Rational::operator!=(const Rational &dne) const
  160. {
  161. return (numer != dne.numer && denom != dne.denom);
  162. }
  163.  
  164. //Test Rational > Rational
  165. bool Rational::operator>(const Rational &gt) const
  166. {
  167. Rational temp;
  168. temp.numer = numer * gt.denom;
  169. temp.denom = denom * gt.numer;
  170. return (temp.numer > temp.denom);
  171. }
  172.  
  173. //Test Rational < Rational
  174. bool Rational::operator<(const Rational &lt) const
  175. {
  176. Rational temp;
  177. temp.numer = numer * lt.denom;
  178. temp.denom = denom * lt.numer;
  179. return (temp.numer < temp.denom);
  180. }
  181.  
  182. //Test Rational >= Rational
  183. bool Rational::operator>=(const Rational &gte) const
  184. {
  185. Rational temp;
  186. temp.numer = numer * gte.denom;
  187. temp.denom = denom * gte.numer;
  188. return (temp.numer >= temp.denom);
  189. }
  190.  
  191. //Test Rational <= Rational
  192. bool Rational::operator<=(const Rational &lte) const
  193. {
  194. Rational temp;
  195. temp.numer = numer * lte.denom;
  196. temp.denom = denom * lte.numer;
  197. return (temp.numer <= temp.denom);
  198. }
  199.  
  200. //simplify Rational
  201. void Rational::simplify()
  202. {
  203. int largest;
  204. largest = numer > denom ? numer : denom;
  205.  
  206. int gcd = 0;
  207.  
  208. for (int loop = 2; loop<=largest; loop++)
  209. if (numer % loop == 0 && denom % loop == 0)
  210. gcd = loop;
  211. if (gcd != 0)
  212. {
  213. numer /= gcd;
  214. denom /= gcd;
  215. }
  216. }
  217.  
  218. //driver.cpp
  219. #include <iostream>
  220. using std::cin;
  221. using std::cout;
  222. using std::endl;
  223.  
  224. #include <string>
  225. using std::string;
  226.  
  227. #include <iomanip>
  228. using std::ios_base;
  229.  
  230. #include "Rational.h"
  231.  
  232. int main()
  233. {
  234. Rational a, b;
  235.  
  236. cout << "Enter the first fraction:" << endl;
  237. cin >> a;
  238.  
  239. cout << "Enter the next fraction:" << endl;
  240. cin >> b;
  241.  
  242. cout << "Here are your fractions:" << endl;
  243. cout << a << endl;
  244. cout << b << endl;
  245.  
  246. cout << "Okay, let's do some math:" << endl;
  247. cout << a << " + " << b << " = " << a+b << endl;
  248. cout << a << " - " << b << " = " << a-b << endl;
  249. cout << a << " * " << b << " = " << a*b << endl;
  250. cout << a << " / " << b << " = " << a/b << endl;
  251.  
  252. //boolalpha used to display true or false
  253. cout << "Okay, let's compare them:" << endl;
  254. cout << a << " == " << b << " ? ";
  255. cout.setf(ios_base::boolalpha);
  256. cout << (a==b) << endl;
  257. cout << a << " != " << b << " ? ";
  258. cout.setf(ios_base::boolalpha);
  259. cout << (a!=b) << endl;
  260. cout << a << " > " << b << " ? ";
  261. cout.setf(ios_base::boolalpha);
  262. cout << (a>b) << endl;
  263. cout << a << " < " << b << " ? ";
  264. cout.setf(ios_base::boolalpha);
  265. cout << (a<b) << endl;
  266. cout << a << " >= " << b << " ? ";
  267. cout.setf(ios_base::boolalpha);
  268. cout << (a>=b) << endl;
  269. cout << a << " <= " << b << " ? ";
  270. cout.setf(ios_base::boolalpha);
  271. cout << (a<=b) << endl;
  272. return 0;
  273. }
Last edited by TheFueley; Apr 23rd, 2008 at 12:27 pm. Reason: Forgot to add warning message
Rivera
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,398
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Warning on Overloaded stream operators

 
0
  #2
Apr 23rd, 2008
>>I don't quite know "what to return" from this block of code
It means that function must return something. You need to add return input; on line 29 or the first code you posted.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 26
Reputation: TheFueley is an unknown quantity at this point 
Solved Threads: 0
TheFueley's Avatar
TheFueley TheFueley is offline Offline
Light Poster

Re: Warning on Overloaded stream operators

 
0
  #3
Apr 23rd, 2008
Oh my ... Wow, thank you!
Rivera
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC