944,018 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2346
  • C++ RSS
Dec 5th, 2004
-1

Hello Everyone, I need Help with my C++ Code...

Expand Post »
Hello People,
I need help because i just need to figure out whats wrong with the code i wrote for my program.. i tried to figure it out but i'm stuck. So here it is and i would appreciate it if someone can set me strait. Thanx alot.
Well here it is..
I am writing a program that computes the number of days between two dates.
It gives me a couple of error messages.
I don't know what i'm not using write or what i'm doing wrong!!
Please help me... Thanx again.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <cctype>
  5. #include <cstring>
  6. using namespace std;
  7.  
  8. int main(int)
  9. {
  10. const int YEAR= 365;
  11.  
  12. int i;
  13. string inMonth1;
  14. int inMonth1Int;
  15. string inDay1;
  16. int inDay1Int;
  17. string inYear1;
  18. int inYear1Int;
  19. string inEra1;
  20. string inMonth2;
  21. int inMonth2Int;
  22. string inDay2;
  23. int inDay2Int;
  24. string inYear2;
  25. int inYear2Int;
  26. string inEra2;
  27. char inMonth1Char;
  28. char inMonth2Char;
  29. int date1;
  30. int date2;
  31. bool badInput;
  32. long double in1;
  33. long double in2;
  34.  
  35.  
  36. while (badInput==false)
  37. {
  38. while (inMonth1!="quit" &&inDay1!="quit" &&inYear1!="quit" && inEra1!="quit"
  39. &&inMonth2!="quit" &&inDay2!="quit" &&inYear2!="quit" && inEra2!="quit")
  40. {
  41.  
  42.  
  43. cout << "This program calculates the number of days between two dates."<< endl;
  44. cout << "Please enter the dates using only the following format:"<< endl;
  45. cout << "Month's abreviation, day, year, BC or AD" << endl;
  46. cout << "Then press enter, and the next date"<< endl;
  47. cout << "Example:"<< endl << "Jun 29 2500 BC (Enter)"<< endl<<"Jun 29 1238 AD (Enter)" << endl;
  48. cout << "To quit, type the word 'quit' and enter."<< endl;
  49. cout << "For the year of Jesus' death exactly, please type 0 for the year." << endl;
  50. cout << "We're sorry, no other format can be accepted."<< endl;
  51.  
  52. cin.ignore('\n') >> inMonth1 >> inDay1 >> inYear1 >> inEra1 >> inMonth2 >> inDay2 >> inYear2 >> inEra2;
  53.  
  54. i=inMonth1.length();
  55. while (i>0)
  56. {
  57. inMonth1[i]=tolower(inMonth1[i]);
  58. i--;
  59. }
  60.  
  61. i=0;
  62. i=inMonth2.length();
  63. while (i>0)
  64. {
  65. inMonth2[i]=tolower(inMonth2[i]);
  66. i--;
  67. }
  68.  
  69. if(inMonth1!="jan"&&inMonth1!="feb"&&inMonth1!="mar"&&inMonth1!="apr"&&inMonth1!="may"&&inMonth1!="jun"&&inMonth1!="jul"&&inMonth1!="aug"&&inMonth1!="sep"&&inMonth1!="oct"&&inMonth1!="nov"&&inMonth1!="dec")
  70. badInput=true;
  71. if (inMonth1=="jan")
  72. inMonth1Char='a';
  73. if (inMonth1=="feb")
  74. inMonth1Char='b';
  75. if (inMonth1=="mar")
  76. inMonth1Char='c';
  77. if (inMonth1=="apr")
  78. inMonth1Char='d';
  79. if (inMonth1=="may")
  80. inMonth1Char='e';
  81. if (inMonth1=="jun")
  82. inMonth1Char='f';
  83. if (inMonth1=="jul")
  84. inMonth1Char='g';
  85. if (inMonth1=="aug")
  86. inMonth1Char='h';
  87. if (inMonth1=="sep")
  88. inMonth1Char='i';
  89. if (inMonth1=="oct")
  90. inMonth1Char='j';
  91. if (inMonth1=="nov")
  92. inMonth1Char='k';
  93. if (inMonth1=="dec")
  94. inMonth1Char='l';
  95.  
  96. inDay1Int=atoi(inDay1);
  97. inYear1Int=atoi(inYear1);
  98. inDay2Int=atoi(inDay2);
  99. inYear2Int=atoi(inYear2);
  100.  
  101. switch (inMonth1Char)
  102. {
  103. case 'a':
  104. if(inDay1Int>0&&inDay1Int<32)
  105. {
  106. inMonth1Int=31;
  107. break;
  108. }
  109. else
  110. badInput=true;
  111.  
  112. case 'b':
  113. if(inYear1Int%4==0)
  114. {
  115. if(inDay1Int>0&&inDay1Int<30)
  116. {
  117. inMonth1Int=29;
  118. break;
  119. }
  120. else
  121. badInput=true;
  122. }
  123. else
  124. {
  125. if(inDay1Int>0&&inDay1Int<29)
  126. {
  127. inMonth1Int=28;
  128. break;
  129. }
  130. else
  131. badInput=true;
  132. }
  133.  
  134. case 'c':
  135. if(inDay1Int>0&&inDay1Int<32)
  136. {
  137. inMonth1Int=31;
  138. break;
  139. }
  140. else
  141. badInput=true;
  142. case 'd':
  143. if(inDay1Int>0&&inDay1Int<31)
  144. {
  145. inMonth1Int=30;
  146. break;
  147. }
  148. else
  149. badInput=true;
  150. case 'e':
  151. if(inDay1Int>0&&inDay1Int<32)
  152. {
  153. inMonth1Int=31;
  154. break;
  155. }
  156. else
  157. badInput=true;
  158. case 'f':
  159. if(inDay1Int>0&&inDay1Int<31)
  160. {
  161. inMonth1Int=30;
  162. break;
  163. }
  164. else
  165. badInput=true;
  166. case 'g':
  167. if(inDay1Int>0&&inDay1Int<32)
  168. {
  169. inMonth1Int=31;
  170. break;
  171. }
  172. else
  173. badInput=true;
  174. case 'h':
  175. if(inDay1Int>0&&inDay1Int<32)
  176. {
  177. inMonth1Int=31;
  178. break;
  179. }
  180. else
  181. badInput=true;
  182. case 'i':
  183. if(inDay1Int>0&&inDay1Int)
  184. {
  185. inMonth1Int=30;
  186. break;
  187. }
  188. else
  189. badInput=true;
  190. case 'j':
  191. if(inDay1Int>0&&inDay1Int<32)
  192. {
  193. inMonth1Int=31;
  194. break;
  195. }
  196. else
  197. badInput=true;
  198. case 'k':
  199. if(inDay1Int>0&&inDay1Int<31)
  200. {
  201. inMonth1Int=30;
  202. break;
  203. }
  204. else
  205. badInput=true;
  206. case 'l':
  207. if(inDay1Int>0&&inDay1Int<32)
  208. {
  209. inMonth1Int=31;
  210. break;
  211. }
  212. else
  213. badInput=true;
  214. badInput=true;
  215. }
  216.  
  217.  
  218.  
  219. switch (inMonth2Char)
  220. {
  221. case 'a':
  222. if(inDay2Int>0&&inDay2Int<32)
  223. {
  224. inMonth2Int=31;
  225. break;
  226. }
  227. else
  228. badInput=true;
  229.  
  230. case 'b':
  231. if(inYear2Int%4==0)
  232. {
  233. if(inDay2Int>0&&inDay2Int<30)
  234. {
  235. inMonth2Int=29;
  236. break;
  237. }
  238. else
  239. badInput=true;
  240. }
  241. else
  242. {
  243. if(inDay2Int>0&&inDay2Int<29)
  244. {
  245. inMonth2Int=28;
  246. break;
  247. }
  248. else
  249. badInput=true;
  250. }
  251.  
  252. case 'c':
  253. if(inDay2Int>0&&inDay2Int<32)
  254. {
  255. inMonth2Int=31;
  256. break;
  257. }
  258. else
  259. badInput=true;
  260. case 'd':
  261. if(inDay2Int>0&&inDay2Int<31)
  262. {
  263. inMonth2Int=30;
  264. break;
  265. }
  266. else
  267. badInput=true;
  268. case 'e':
  269. if(inDay2Int>0&&inDay2Int<32)
  270. {
  271. inMonth2Int=31;
  272. break;
  273. }
  274. else
  275. badInput=true;
  276. case 'f':
  277. if(inDay2Int>0&&inDay2Int<31)
  278. {
  279. inMonth2Int=30;
  280. break;
  281. }
  282. else
  283. badInput=true;
  284. case 'g':
  285. if(inDay2Int>0&&inDay2Int<32)
  286. {
  287. inMonth2Int=31;
  288. break;
  289. }
  290. else
  291. badInput=true;
  292. case 'h':
  293. if(inDay2Int>0&&inDay2Int<32)
  294. {
  295. inMonth2Int=31;
  296. break;
  297. }
  298. else
  299. badInput=true;
  300. case 'i':
  301. if(inDay2Int>0&&inDay2Int)
  302. {
  303. inMonth2Int=30;
  304. break;
  305. }
  306. else
  307. badInput=true;
  308. case 'j':
  309. if(inDay2Int>0&&inDay2Int<32)
  310. {
  311. inMonth2Int=31;
  312. break;
  313. }
  314. else
  315. badInput=true;
  316. case 'k':
  317. if(inDay2Int>0&&inDay2Int<31)
  318. {
  319. inMonth2Int=30;
  320. break;
  321. }
  322. else
  323. badInput=true;
  324. case 'l':
  325. if(inDay2Int>0&&inDay2Int<32)
  326. {
  327. inMonth2Int=31;
  328. break;
  329. }
  330. else
  331. badInput=true;
  332. badInput=true;
  333. }
  334.  
  335. if (inEra1!="AD"&&inEra1!="BC")
  336. badInput=true;
  337.  
  338. if (inEra1=="AD")
  339. {
  340. in1=(inYear1Int*365.24219)+inDay1Int+inMonth1Int;
  341. }
  342. if (inEra1=="BC")
  343. {
  344. in1=(inYear1Int*365.24219)+inDay1Int+inMonth1Int*(0-1);
  345. }
  346.  
  347. }
  348. }
  349. return 0;
  350. }
Last edited by alc6379; Dec 6th, 2004 at 11:34 am. Reason: added [code] tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
badchick is offline Offline
5 posts
since Dec 2004
Dec 5th, 2004
0

Re: Hello Everyone, I need Help with my C++ Code...

split it up into smaller, easier to comprehend chunks (iow, use what you learned about methods, classes, etc.).

That should unstick you probably.

If not think of how you'd do it without a computer. Write that down, then translate it into computer code one bit at a time (again, taking care to split it up into small chunks you can comprehend as a single entity).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 5th, 2004
0

Re: Hello Everyone, I need Help with my C++ Code...

Okay so i should shorten my code? Well i did as best as i could on it and i only get three errors that say something about the std::basisstring thingy. i am using atoi to do the numbering and i'm not sure if i need to change that particular thing.. But i'll see if i can shorten it ..Thanx..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
badchick is offline Offline
5 posts
since Dec 2004
Dec 6th, 2004
0

Re: Hello Everyone, I need Help with my C++ Code...

atoi requires const char*as an argument. string is a specialization of basic_string and is not a const char*. Call method c_str() on the string to get const char*. string owns the memory for the char buffer, so make sure your string goes out of scope after you have finished with the cons char*. The lines

inDay1Int=atoi(inDay1);
inYear1Int=atoi(inYear1);
inDay2Int=atoi(inDay2);
inYear2Int=atoi(inYear2);

should be

inDay1Int=atoi(inDay1.c_str());
inYear1Int=atoi(inYear1.c_Str());
inDay2Int=atoi(inDay2.c_str());
inYear2Int=atoi(inYear2.c_str());

Also, variable inMonth2Char is used bit not set anywhere.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
britt_boy is offline Offline
13 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: OnInitUpdate
Next Thread in C++ Forum Timeline: une tiny help plz!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC