Need some guidance/pointers/tips/etc regarding codes.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 49
Reputation: dexter1984 is an unknown quantity at this point 
Solved Threads: 2
dexter1984 dexter1984 is offline Offline
Light Poster

Need some guidance/pointers/tips/etc regarding codes.

 
0
  #1
Oct 10th, 2008
Hi,
I'm doing a project regarding converting roman numerals to decimals and vice versa. With some reference from the internet, I was able to come up with one. However, it isn't working very well. Can someone look at my codes and point out what/where's the mistakes?
Thanks in advance.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. int checkStatus(string);
  7. string toRoman(string);
  8.  
  9.  
  10. int main()
  11. {
  12. ifstream fin;
  13. string filename, data, roman;
  14. int status;
  15.  
  16. cout << "Please enter filename: ";
  17. getline(cin, filename);
  18.  
  19. fin.open(filename.c_str());
  20. if (!fin.good())
  21. {
  22. cout << "File not found" << endl;
  23. system("pause");
  24. return 1;
  25. }
  26.  
  27. while (!fin.eof())
  28. {
  29. getline(fin, data);
  30. status = checkStatus(data);
  31. if (status == 0)
  32. cout << data << "\t\t" << "Invalid" << endl;
  33. else if (status == 1)
  34. {
  35. roman = toRoman(data);
  36. cout << data << "\t\t";
  37. for (int c = 0; (isalpha(roman[c])) ; c++)
  38. cout << roman[c];
  39. cout << endl;
  40. }
  41. else if (status == 2)
  42. cout << data << "\t\t" << "Decimal" << endl;
  43.  
  44. }
  45. fin.close();
  46. system("pause");
  47. return 0;
  48. }
  49.  
  50.  
  51. int checkStatus(string data)
  52. {
  53. char temp;
  54. if (!isdigit(data[0]))
  55. {
  56. for (int b = 0; b < data.length(); b++)
  57. {
  58. temp = toupper(data[b]);
  59. if ((temp != 'I') && (temp != 'V') &&
  60. (temp != 'X') && (temp != 'L') &&
  61. (temp != 'C') && (temp != 'D') &&
  62. (temp != 'M'))
  63. return 0;
  64. }
  65. return 2;
  66. }
  67. else
  68. {
  69. for (int b = 0; b < data.length(); b++)
  70. {
  71. if (!isdigit(data[b]))
  72. return 0;
  73. }
  74. return 1;
  75. }
  76. }
  77.  
  78. string toRoman(string data)
  79. {
  80. int a;
  81. int i = 0;
  82. string romanF;
  83.  
  84. a = atoi(data.c_str());
  85. while(a > 0)
  86. {
  87. if((a >= 1000) && (a < 4000))
  88. {
  89. romanF[i] = 'M';
  90. a = a-1000;
  91. i ++;
  92. }
  93. else if((a >= 900) && (a < 1000))
  94. {
  95. romanF[i] = 'C';
  96. romanF[i+1] = 'M';
  97. a = a-900;
  98. i += 2;
  99. }
  100. else if((a >= 500) && (a < 900))
  101. {
  102. romanF[i] = 'D';
  103. a = a-500;
  104. i ++;
  105. }
  106. else if((a >= 400) && (a < 500))
  107. {
  108. romanF[i] = 'C';
  109. romanF[i+1] = 'D';
  110. a = a-400;
  111. i += 2;
  112. }
  113. else if((a >= 100) && (a < 400))
  114. {
  115. romanF[i] = 'C';
  116. a = a-100;
  117. i ++;
  118. }
  119. else if((a >= 90) && (a < 100))
  120. {
  121. romanF[i] = 'X';
  122. romanF[i+1] = 'C';
  123. a = a-90;
  124. i += 2;
  125. }
  126. else if((a >= 50) && (a < 90))
  127. {
  128. romanF[i] = 'L';
  129. a = a-50;
  130. i ++;
  131. }
  132. else if((a >= 40) && (a < 50))
  133. {
  134. romanF[i] = 'X';
  135. romanF[i+1] = 'L';
  136. a = a-40;
  137. i += 2;
  138. }
  139. else if((a >= 10) && (a < 40))
  140. {
  141. romanF[i] = 'X';
  142. a = a-10;
  143. i ++;
  144. }
  145. else if((a >= 9) && (a < 10))
  146. {
  147. romanF[i] = 'I';
  148. romanF[i+1] = 'X';
  149. a = a-9;
  150. i += 2;
  151. }
  152. else if ((a >= 5) && (a < 9))
  153. {
  154. romanF[i] = 'V';
  155. a = a-5;
  156. i ++;
  157. }
  158. else if ((a >= 4) && (a < 5))
  159. {
  160. romanF[i] = 'I';
  161. romanF[i+1] = 'V';
  162. a = a-4;
  163. i += 2;
  164. }
  165. else if ((a >= 1) && (a < 4))
  166. {
  167. romanF[i] = 'I';
  168. a = a-1;
  169. i ++;
  170. }
  171. }
  172. return romanF;
  173. }

The answers are suppose to be those on the textfile, instead I get some extra characters for some numbers.
I went through and followed the numbers but can't figure out why.
http://i126.photobucket.com/albums/p...untitled-2.jpg


So I tried to limit the codes and location of the mistakes.http://i126.photobucket.com/albums/p.../untitled2.jpg

Weird... The first few numbers and once in awhile it works fine.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Need some guidance/pointers/tips/etc regarding codes.

 
0
  #2
Oct 10th, 2008
Just had a quick look but something im gonna point out.

  1. else if ((a >= 4) && (a < 5))
  2. {
  3. romanF[i] = 'I';
  4. romanF[i+1] = 'V';
  5. a = a-4;
  6. i += 2;
  7. }
could become
  1. else if (a==4)
  2. {
  3. romanF[i++] = 'I';
  4. romanF[i++] = 'V';
  5. a -= 4;
  6. }

This has made it cleaner, but also it has reduded the time complexity by only having to perform 1 comparison.

Chris
Last edited by Freaky_Chris; Oct 10th, 2008 at 5:08 pm.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 49
Reputation: dexter1984 is an unknown quantity at this point 
Solved Threads: 2
dexter1984 dexter1984 is offline Offline
Light Poster

Re: Need some guidance/pointers/tips/etc regarding codes.

 
0
  #3
Oct 10th, 2008
Aaa, I haven't refine my codes yet so it's a little messy.
I only did the toRoman part, haven't touch on the fromRoman part yet. But thanks for the input
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 49
Reputation: dexter1984 is an unknown quantity at this point 
Solved Threads: 2
dexter1984 dexter1984 is offline Offline
Light Poster

Re: Need some guidance/pointers/tips/etc regarding codes.

 
0
  #4
Oct 10th, 2008
My friend just pointed out to me that the string looks like it wasn't cleared so there's extra I's at the end. Hmmm...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 49
Reputation: dexter1984 is an unknown quantity at this point 
Solved Threads: 2
dexter1984 dexter1984 is offline Offline
Light Poster

Re: Need some guidance/pointers/tips/etc regarding codes.

 
0
  #5
Oct 10th, 2008
Woot! I just solved it.
else if (status == 1)
{
roman = toRoman(data);
cout << data << "\t\t";
for (int c = 0; isalpha(roman[c]) ; c++)
{
cout << roman[c];
roman[c] = ' ';
}

But it's weird on how I have to clear the string. Shouldn't it be a new string everytime?
I tried using string.clear() but it doesn't work. Hmmm
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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