What am I doing wrong.

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

Join Date: Sep 2007
Posts: 6
Reputation: Carwy has a little shameless behaviour in the past 
Solved Threads: 0
Carwy Carwy is offline Offline
Newbie Poster

What am I doing wrong.

 
0
  #1
Mar 31st, 2008
Okay so I have have been beatting my head againest the wall and I can not get this to work right. I have asked my insttor and she does not know why it will not work righ either.
I read in numbers as characters and then I place them in a dymanic array as integers. This works fine.
When ever I pull them out to add, subtract or multiply them it falls apart. It will only let me have two numbers in the second number for add and sutract and one for multiply and if I use my copy construtor it goes nuts.
Please help.
  1. class NumbersC
  2. {
  3. private:
  4. int size; //this is the size of the first set of numbers.
  5. int *num; //A pointer to the first number in a dynamic array.
  6.  
  7. public:
  8. NumbersC() //Default constructor. Setting all values to zero or NULL to start.
  9. {
  10. size = 0;
  11. num = NULL;
  12. }
  13. ~NumbersC()
  14. {
  15. delete[] num; //Deletes num1 to free up memory on the stack.
  16. }
  17. //The copy constructor will not let it have any numbers over two digits long.
  18. NumbersC(const NumbersC& copyNum) //Copy constructor.
  19. {
  20. size = copyNum.size;
  21. num = new int (size);
  22. for(int i = 0; i < size; i++)
  23. {
  24. num[i] = copyNum.num[i];
  25. }
  26. }
  27. NumbersC& operator = (const NumbersC& numbers2) //Overloading the = operator and making it a fried to the class
  28. {
  29. size = numbers2.size;
  30. delete[] num;
  31. num = new int (size);
  32. for(int i = 0; i < size; i++)
  33. {
  34. num[i] = numbers2.num[i];
  35. }
  36. return *this;
  37. }
  38. void setNumber(char numberString[MAX], int i) //creatting and filling num1
  39. {
  40. int x = 0;
  41. size = i; //sets size1 to i which is the count of character entered into numberString
  42. num = new int(size); //Creates the array num1 and sets it size to size1.
  43. //Takes the information out of numberString and put is in num1
  44. while (x < size)
  45. {
  46. num[x] = numberString[x] - '0';
  47. x++;
  48. }
  49. }
  50. //returns the size of each number
  51. int getSize()
  52. {
  53. return size;
  54. }
  55. //Returns each number.
  56. int getNumber()
  57. {
  58. return num[size];
  59. }
  60. //Prints each number to the screen.
  61. void print()
  62. {
  63. for (int c = 0; c < size; c++)
  64. {
  65. cout << num[c];
  66. }
  67. }
  68. //Increase the size of total.num if there is a remainder.
  69. void getIncrease(NumbersC &total, int &remainder)
  70. {
  71. int *tempNumber;
  72. total.size = total.size + 1;
  73. tempNumber = new int (total.size); //Creatting tempNumber to put total into to fix it.
  74. int j = 0;
  75. tempNumber[j] = remainder;
  76. remainder = 0; //setting remainder to zero.
  77. j++;
  78. //Putting the rest of the numbers into tempNumber.
  79. for ( int s = 0; s < total.size; s++)
  80. {
  81. tempNumber[j] = total.num[s]; //Error cannot convert 'NumbersC' to 'char' in assignment.
  82. j++;
  83. }
  84. delete[] total.num; //Deletes total to free up memory on the stack. Because it is the wrong size.
  85. total.num = new int(total.size); //Rebuilds total to the right size.
  86. //Puts the numbers back into total which is now the right size.
  87. for ( int s = 0; s < total.size+1; s++)
  88. {
  89. total.num[s] = tempNumber[s];
  90. }
  91. }
  92. //Subtracts number2 from number1. Called from the operator overload -.
  93. void getSubtract(NumbersC &total, NumbersC &numbers1, NumbersC &numbers2,
  94. int &a, int &b, int y, int x, int &remainder)
  95. {
  96. int number1;
  97. int number2;
  98. //Goes through the numbers one at a time.
  99. while (x < total.size)
  100. {
  101. //If b is less than than zero and the remainder
  102. // is zero set number2 to zero.
  103. if (b < 0 && remainder ==0)
  104. {
  105. number2 = 0;
  106. }
  107. //If the remainder is larger than zero set it to number2 and
  108. //than set remainder to zero.
  109. else if (remainder > 0)
  110. {
  111. number2 = remainder;
  112. remainder = 0;
  113. }
  114. else //Get the number from numbers2.num.
  115. {
  116. number2 = numbers2.num[b];
  117. }
  118. //If a is less than zero set number1 to zero.
  119. if(a < 0)
  120. {
  121. number1 = 0;
  122. }
  123. else //Get the number from numbers1.num.
  124. {
  125. number1 = numbers1.num[a];
  126. }
  127. //If number2 is smaller than number1. Subtract one from b to
  128. //go to the next location is numbers2.num. Take the number
  129. //in numbers2.num and subtract one from it and place it in
  130. //remainder. Then add ten to the number in number2.
  131. if(number2 < number1)
  132. {
  133. b = b - 1;
  134. remainder = numbers2.num[b] - 1;
  135. number2 = number2 + 10;
  136. }
  137. else
  138. {}
  139. total.num[y] = number2 - number1;
  140. cout << total.num[y] << " = " << number2 << " - " << number1 << endl;
  141. //total.num[y] = subTotal;
  142. x++;
  143. y--;
  144. a--;
  145. b--;
  146. }
  147. }
  148. //If subtotal equal to or bigger than 10. It must be reduced through division
  149. //to get sutotal to the last digit in the number and remainder to the
  150. //first digit.
  151. void getSubDivide(int &subTotal, int &remainder)
  152. {
  153. remainder = subTotal / 10;
  154. subTotal = subTotal % 10;
  155. }
  156.  
  157. friend NumbersC operator +(NumbersC& numbers1, NumbersC& number2);
  158. friend NumbersC operator -(NumbersC& numbers1, NumbersC& number2);
  159. friend NumbersC operator *(NumbersC numbers1, NumbersC number2);
  160. };
  161.  
  162. int main(int argc, char *argv[])
  163. {
  164. NumbersC number1 , number2;
  165. int i = 0; //Used to go through the loop to put the characters into numberString..
  166. char numberString[MAX]; //This is where the numbers are stored until they are put into the dynamic arrays. num1 and num2.
  167. char letter; //This is used to read the number in at first.
  168. ifstream fin; //The internal name for the file.
  169. char fileName[MAX]; //Is to get the name of the file you are using. The one on your computer in the same folder as the program.
  170. char math; //Math is used for two thngs only in the main functio. 1) To see if you are entring the numbers from the keyboard or file and two to get which math you are doing.
  171. int count = 0; //Used to get the number of numbers in each number. ex. the number 123 has 3 numbers. So it needs to read a 3 from your file first to get the number.
  172. cout << "How would you like to enter the numbers?\n(k)eyboard\n(f)ile: ";
  173. cin >> math;
  174. tolower(math);
  175. //If you are inputting the numbers from the keyboard then if goes in here.
  176. if (math =='k')
  177. {
  178. cin.get(letter); //Do not know why but without it it will skip the first number.
  179. //Start entering number 1.
  180. cout << "\nEnter a number: ";
  181. cin.get(letter);
  182. //Start for isdigit
  183. //Puts the numbers into numberString one at a time.
  184. while (isdigit(letter))
  185. {
  186. numberString[i] = letter;
  187. i++;
  188. cin.get(letter);
  189. }
  190. numberString[i] = '\0'; //Sets the last spot in the array to the NULL or end character.
  191. //end isdigit.
  192. number1.setNumber(numberString, i);
  193. //End entering number 1.
  194. //Start entering number 2.
  195. cout << "\nEnter a number: ";
  196. cin.get(letter);
  197. //Start for isdigit
  198. i = 0; //Reset i to zero to start over.
  199. //Puts the numbers into numberString one at a time.
  200. while (isdigit(letter))
  201. {
  202. numberString[i] = letter;
  203. i++;
  204. cin.get(letter);
  205. }
  206. numberString[i] = '\0'; //Sets the last spot in the array to the NULL or end character.
  207. //end isdigit.
  208. number2.setNumber(numberString, i);
  209. //END NUMBER 2
  210. }
  211. else //For filrs here.
  212. {
  213. cout <<"Please enter the name of the file including the .txt(numbers.txt):\n";
  214. cin >>fileName;
  215.  
  216. fin.open(fileName); // opens the file call clouds.txt
  217. //This loop is to make sure the file opend right and is working.
  218. if (fin.fail())
  219. {
  220. cout <<"Did not open.";
  221. }else{}
  222. fin >> count;
  223. while(i <count)
  224. {
  225. fin >> numberString[i];
  226. i++;
  227. }
  228. numberString[i] = '\0'; //Sets the last spot in the array to the NULL or end character.
  229. number1.setNumber(numberString, i);
  230. //END NUMBER 1
  231. fin >> count;
  232. i = 0;
  233. while(i <count)
  234. {
  235. fin >> numberString[i];
  236. i++;
  237. }
  238. numberString[i] = '\0'; //Sets the last spot in the array to the NULL or end character.
  239. number2.setNumber(numberString, i);
  240. //END NUMBER 2
  241. fin.close();
  242. }
  243. //Asking what type of math you wish to do.
  244. cout << "\nWhat math would you like to do?\n(a)dd.\n(s)ubtract\n(m)ultiply\n";
  245. cin >> math;
  246. tolower (math);
  247. switch (math)
  248. {
  249. case 'a': //Adding the numbers together.
  250. {
  251. NumbersC sum = number1 + number2; //This works to add numbers.
  252. number1.print();
  253. cout <<" + ";
  254. number2.print();
  255. cout << " = ";
  256. sum.print();
  257. }
  258. break;
  259. case 's': //Subtracting them.
  260. {
  261. NumbersC sum = number1 - number2;
  262. number1.print();
  263. cout << " - ";
  264. number2.print();
  265. cout << " = ";
  266. sum.print();
  267. }
  268. break;
  269. case 'm': //Multiplying them.
  270. {
  271. NumbersC sum = number1 * number2;
  272. cout << "\nIn main.\n";
  273. number1.print();
  274. cout <<" * ";
  275. number2.print();
  276. cout << " = ";
  277. sum.print();
  278. }
  279. break;
  280. default:
  281. NumbersC sum = number1 + number2; //This works to add numbers.
  282. break;
  283. }
  284. cout << endl;
  285. system("PAUSE");
  286. return EXIT_SUCCESS;
  287. }
  288. //Overloading the + operator and making it a fried to the class
  289. //Will not allow any number over two digits in either number
  290. // and if you take out system("PAUSE") it will crash. Strange.
  291. NumbersC operator + ( NumbersC& numbers1, NumbersC& numbers2)
  292. {
  293. NumbersC total;
  294. int number1;
  295. int number2;
  296. int remainder = 0;
  297. int subTotal = 0;
  298. int size1 = numbers1.getSize(); //Sets size1 to numbers1.size;
  299. int size2 = numbers2.getSize(); //Sets size2 to numbers2.size;
  300. int y = 0;
  301. int a = size1-1; //Sets a to size1 - 1. to go throught the array in reverse.
  302. int b = size2-1; //Sets b to size2 - 1. to go throught the array in reverse.
  303. int l = 0;
  304. //If size2 is equal to or grater than size1 set total.size to size2.
  305. if (size1 >= size2)
  306. {
  307. total.size = size1;
  308. }
  309. //If size2 is less than size1 we set total.size to size1.
  310. else
  311. {
  312. total.size = size2;
  313. }
  314. total.num = new int(total.size); //creatting total.num and makeing it the size of total.size.
  315. system("PAUSE"); //If you remove this it will give an error at the end of the program in main.
  316. y = total.size - 1; //sets y tot toal.size - 1 to go through total.num bacwards.
  317. //goes through the numbers and adds the together until l is the same number as total.size.
  318. while (l < total.size)
  319. {
  320. subTotal = 0;
  321. //If a is less than zero we set number1 to zero. So it does the math right if number two is larges in size.
  322. if (a < 0)
  323. {
  324. number1 = 0;
  325. }
  326. else
  327. {
  328. number1 = numbers1.num[a];
  329. }
  330. //If b is less than zero we set number2 to zero. So it does the math right if number one is larges in size.
  331. if ( b < 0)
  332. {
  333. number2 = 0;
  334. }
  335. else
  336. {
  337. number2 = numbers2.num[b];
  338. }
  339.  
  340. subTotal = number1 + number2 + remainder; //Adds the numbers together.
  341. remainder = 0;
  342. /*******************************************************************
  343. * If subtotal is equal to or greater than ten then we have to do both
  344. * modulas and division on it get the correct subTotal and the remainder to
  345. * be carried over.
  346. *******************************************************************/
  347. if(subTotal >= 10)
  348. {
  349. total.getSubDivide(subTotal, remainder);
  350. }
  351. else{}
  352.  
  353. total.num[y] = subTotal; //Putting subtotal into total.num at y.
  354. l++;
  355. a--;
  356. b--;
  357. y--;
  358. }
  359. //If remainder is bigger than zero when we are done we have to place it at the start of the array.
  360. if (remainder > 0)
  361. {
  362. total.getIncrease(total, remainder);
  363. }
  364. else{}
  365.  
  366. return total;
  367. }
  368. //Overloading the + operator and making it a fried to the class
  369. //Will not subtract numbers when number2 is larger than two digit;
  370. NumbersC operator - (NumbersC& numbers1, NumbersC& numbers2)
  371. {
  372. NumbersC total;
  373. int *tempNumber;
  374. int size1 = numbers1.getSize(); //Sets size1 to numbers1.size;
  375. int size2 = numbers2.getSize(); //Sets size2 to numbers2.size;
  376. int number1 = 0;
  377. int number2 = 0;
  378. int remainder = 0; //The number taht one is subtracted from if number1 or number2 is smaller than the other.
  379. int a = size1-1; //Sets a to size1 - 1. to go throught the array in reverse.
  380. int b = size2-1; //Sets b to size2 - 1. to go throught the array in reverse.
  381. int x = 0; //Used to ge through the while loop.
  382. int y = 0; //Goes through total.num
  383. bool done = true;
  384. //If size1 is equal to or greater than size2 set total.size to size1. Else set it to size2.
  385. if (size1 >= size2)
  386. {
  387. total.size = size1;
  388. }
  389. else
  390. {
  391. total.size = size2;
  392. }
  393. y = total.size -1; //Set y to total.size minus one.
  394. total.num = new int(total.size); //creatting total.num and makeing it the size of total.size.
  395. system("PAUSE"); //If I remove this it will fail when any number has more than two digits in it.
  396. //Subtracts number1 from number2.
  397. if (size1 > size2)
  398. {
  399. //While x is less than total.size we go through the numbers on at a time.
  400. while (x < total.size)
  401. {
  402. //If a is less than zero. There is no more numbers in numbers1.num and if remainder is
  403. //equal to zero then it also has no number to be subtracted in it.
  404. //So place a zero in number1.
  405. if (a < 0 && remainder ==0)
  406. {
  407. number1 = 0;
  408. }
  409. else if (remainder > 0) //If the remainder is bigger tan zero we want to use it the next time.
  410. {
  411. number1 = remainder; //Setting number1 to remainder.
  412. remainder = 0; //Resetting remainder to zero so we know it is empty.
  413. }
  414. else //Just get the number for number1 from numbers1.num.
  415. {
  416. number1 = numbers1.num[a];
  417. }
  418. //If b is less than zero that means there are no more numbers in numbers2.num.
  419. //Place a zero in number2.
  420. if(b < 0)
  421. {
  422. number2 = 0;
  423. }
  424. else //Else get the number from numbers2.num
  425. {
  426. number2 = numbers2.num[b];
  427. }
  428. //If number1 is less than number2 we need to add ten to number1 and subtract
  429. //One from the digit on the left and dincreament a by one.
  430. if(number1 < number2)
  431. {
  432. //If a is bigger than or equal to zero we can increase the size of the number1 by ten.
  433. if (a >=0)
  434. {
  435. a--; ////Move a one to the left (dincreament it by one.
  436. remainder = numbers1.num[a] - 1; //Set remainder to equal the number in numbers1.num minus one.
  437. number1 = number1 + 10; //Add ten to the number in number1.
  438. }else{} //Do nothing.
  439. }
  440. if(a<0)
  441. { //This is done to make the first digit zero to be removed.
  442. total.num[y] = number1 - number2;
  443. break;
  444. }
  445. else //Do the subtraction and place the results in total.num[y]
  446. {
  447. total.num[y] = number1 - number2;
  448. cout << total.num[y] << " = " << number1 << " - " << number2 << endl;
  449. x++;
  450. y--;
  451. a--;
  452. b--;
  453. }
  454. }
  455. }
  456. //If size1 is equal to size2.
  457. else if (size1 == size2)
  458. {
  459. //If the first number in numbers1.num is bigger than the first number in numbers2.num we subtract.
  460. if (numbers1.num[0] >=numbers2.num[0])
  461. {
  462. //While x is less than total.size we go through the numbers on at a time.
  463. while (x < total.size)
  464. {
  465. //If a is less than zero. There is no more numbers in numbers1.num and if remainder is
  466. //equal to zero then it also has no number to be subtracted in it.
  467. //So place a zero in number1.
  468. if (a < 0 && remainder ==0)
  469. {
  470. number1 = 0;
  471. }
  472. else if (remainder > 0) //If the remainder is bigger tan zero we want to use it the next time.
  473. {
  474. number1 = remainder; //Setting number1 to remainder.
  475. remainder = 0; //Resetting remainder to zero so we know it is empty.
  476. }
  477. else //Just get the number for number1 from numbers1.num.
  478. {
  479. number1 = numbers1.num[a];
  480. }
  481. //If b is less than zero that means there are no more numbers in numbers2.num.
  482. //Place a zero in number2.
  483. if(b < 0)
  484. {
  485. number2 = 0;
  486. }
  487. else //Else get the number from numbers2.num
  488. {
  489. number2 = numbers2.num[b];
  490. }
  491. //If number1 is smaller than number2 and a is bigger than or equal to
  492. //zero we get the next number from numbers1.num and subtract one
  493. //from it and set it to remainder add ten to number one.
  494. if(number1 < number2 && a >=0)
  495. {
  496. a = a - 1;
  497. remainder = numbers1.num[a] - 1;
  498. number1 = number1 + 10;
  499. }
  500. else if ((number1 == number2) ||(number1> number2))
  501. {
  502. //With out this empty else if statement it give a strange output.
  503. }
  504. //If we have no numbers left in numbers1.num and remainder is to
  505. //small to subtract number2 from then stop and try a different way.
  506. else
  507. {
  508. cout <<"\nCan not subtract " << number1 << " from " << number2 << endl;
  509. done = false;
  510. }
  511. //If the numbers can be subtracted subtract them and put the answer in total.num[y].
  512. if ( done == true)
  513. {
  514. total.num[y] = number1 - number2;
  515. cout << total.num[y] << " = " << number1 << " - " << number2 << endl;
  516. }
  517. else
  518. {}
  519. x++;
  520. y--;
  521. a--;
  522. b--;
  523. }
  524.  
  525. //If done is false then try to subtract the numbers the other way.
  526. // by calling the constructor getSubtract
  527. //Reset a, b, x and y back to thier start values.
  528. if (done == false)
  529. {
  530. a = size1-1;
  531. b = size2-1;
  532. y = total.size - 1;
  533. x = 0;
  534. total.getSubtract(total, numbers1, numbers2, a, b, y, x, remainder);
  535. done = true; //set done back to true.
  536. }
  537. }
  538. //Subtract number2 from number1.
  539. // by calling the constructor getSubtract
  540. else
  541. {
  542. total.getSubtract(total, numbers1, numbers2, a, b, y, x, remainder);
  543. }
  544. }
  545. //Switchs the numbers to subtract number2 from number1
  546. // by calling the constructor getSubtract
  547. else
  548. {
  549. total.getSubtract(total, numbers1, numbers2, a, b, y, x, remainder);
  550.  
  551. }
  552. //This little if statement decreases the size of total if the first number is a zero.
  553. //But it always keeps one digit even if it is a zero.
  554. if (total.num[0] == 0 && total.size > 1)
  555. {
  556. int tempSize = 0; //Just a temp holding place for the answer.
  557. int t = 1; //t is set to one to skip the zero at the beginning of total.num
  558. tempSize = total.size; //tempSize is set to toal.size to get all the places.
  559. total.size = total.size - 1; //Total.size is then reduced by one to drop the zero.
  560. tempNumber = new int(total.size); //Where total is stored while the zero is being dropped.
  561. //This loop drops the zero from the beginning and stores the number in tempNumber.
  562. for (int i = 0; i < total.size; i++)
  563. {
  564. tempNumber[i] = total.num[t];
  565. t++;
  566. }
  567. delete[]total.num; //Delete total.num.
  568. total.num = new int (total.size); //Making a nuw tota.num the size of the new total.
  569. //Puttng the numbers back in total.num
  570. for ( int i = 0; i < total.size; i++)
  571. {
  572. total.num[i] = tempNumber[i];
  573. }
  574. delete[]tempNumber; //Deleteing tempNumber because it is not needed anymore.
  575. }
  576. return total;
  577. }
  578. ////Overloading the * operator and making it a fried to the class
  579. //Now can multiply a two digit number by a one digit number.
  580. NumbersC operator * ( NumbersC numbers1, NumbersC numbers2)
  581. {
  582. NumbersC total;
  583. int *tempNumber;
  584. int size1 = numbers1.getSize(); //Sets size1 to numbers1.size;
  585. int size2 = numbers2.getSize(); //Sets size2 to numbers2.size;
  586. int number1 = 0;
  587. int number2 = 0;
  588. int subTotal = 0;
  589. int remainder = 0;
  590. int a = size1-1; //Sets a to size1 - 1. to go throught the array in reverse.
  591. int b = size2-1; //Sets b to size2 - 1. to go throught the array in reverse.
  592. int t = 0; //Used to change the place in total if number 2 has more than one digit.
  593. int y = 0; //Used to keep our place in total.
  594.  
  595. // If size1 is greater than or equal to size2 then we set tsize (total size) to size1.
  596. if (size1 >= size2)
  597. {
  598. total.size = size1;
  599. }
  600. else //If size2 in bigger than size1 we set tsize (total size) to size2.
  601. {
  602. total.size = size2;
  603. }
  604. total.num = new int(total.size); //creatting total.num and makeing it the size of total.size
  605. if ( size1 > size2)
  606. { //Going through the for loop in reverse order to have the math come out right.
  607. for (b = size2 - 1; b >=0; b--)
  608. {
  609. y = total.size - 1; //For each loop we reset y to toal.size. to keep the right place in total.num
  610. //t is greater than zero that means we are on the next number in numbers2.num
  611. //We need to leave the last digit in total.num alone and add the subTotal
  612. //To total.num[y] and place it back into total.num[y]
  613. //this is done by subtracting y from t and putting the result back in y.
  614. if (t > 0)
  615. {
  616. y = y - t;
  617. }
  618. number2 = numbers2.num[b]; //sets number2 to the value of numbers2.num[b].
  619. //This loop goes through the first number one digit at a time and multiplies
  620. //them to number2.
  621. for (a = size1 - 1; a >=0; a--)
  622. {
  623. subTotal = 0; //Reset subTotal to zero so we know it's empty.
  624. number1 = numbers1.num[a];
  625. //If t is zero it means we are still on the first number in numbers2.num
  626. //So total.num does not needed to be add to subTotal at this time.
  627. if (t == 0)
  628. {
  629. subTotal = (number1 * number2) + remainder;
  630. cout << subTotal << " = (" << number1 <<" * " << number2 << ") + " << remainder << endl;
  631. remainder = 0;
  632. }
  633. //If t is larger than zero we need to add total.num[y] to subTotal.
  634. else
  635. {
  636. subTotal = (number1 * number2) + remainder + total.num[y];
  637. cout << subTotal << " = (" << number1 <<" * " << number2 << ") + " << remainder << " + " << total.num[y]<< endl;
  638. remainder = 0;
  639. /*******************************************************************
  640. * If subtotal is equal to or greater than ten then we have to do both
  641. * modulas and division on it get the correct subTotal and the remainder to
  642. * be carried over.
  643. *******************************************************************/
  644. if(subTotal >=10)
  645. {
  646. total.getSubDivide(subTotal, remainder);
  647. }
  648. //If remainder is bigger than zero when we are done we have to place it at the start of the array.
  649. if (remainder > 0 && y < 0)
  650. {
  651. total.getIncrease(total, remainder);
  652. }
  653. else{}
  654. }
  655. /*******************************************************************
  656. * If subtotal is equal to or greater than ten then we have to do both
  657. * modulas and division on it get the correct subTotal and the remainder to
  658. * be carried over.
  659. *******************************************************************/
  660. if(subTotal >=10)
  661. {
  662. total.getSubDivide(subTotal, remainder);
  663. }
  664. //If subTotal is bigger than zero when we are done we have to place it at the start of the array.
  665. //Set remainder to subTotal, set subTotal to zero and call getIncrease.
  666. if (subTotal > 0 && y < 0)
  667. {
  668. remainder = subTotal;
  669. subTotal = 0;
  670. total.getIncrease(total, remainder);
  671. }
  672. else //Just put subTotal in toal.num[y].
  673. {
  674. total.num[y] = subTotal;
  675. subTotal = 0; //Reset subtotal to zero to make sure it's empty.
  676. y--;
  677. }
  678. }
  679. t++; //If this is increamented then we change the start point total.num and add what is in total.num into the equation.
  680. }
  681. }
  682. else //We multiply one by two. Just to make life easy and I'm lazy.
  683. {
  684. for (a = size1 - 1; a >=0; a--)
  685. {
  686. y = total.size - 1; //For each loop we reset y to toal.size. to keep the right place in total.num
  687. //t is greater than zero that means we are on the next number in numbers2.num
  688. //We need to leave the last digit in total.num alone and add the subTotal
  689. //To total.num[y] and place it back into total.num[y]
  690. //this is done by subtracting y from t and putting the result back in y.
  691. if (t > 0)
  692. {
  693. y = y - t;
  694. }
  695. number1 = numbers1.num[a]; //sets number1 to the value of numbers1.num[a].
  696. //This loop goes through the first number one digit at a time and multiplies
  697. //them to number1.
  698. for (b = size2 - 1; b >=0; b--)
  699. {
  700. subTotal = 0; //Reset subTotal to zero so we know it's empty.
  701. number2 = numbers2.num[b];
  702. //If t is zero it means we are still on the first number in numbers2.num
  703. //So total.num does not needed to be add to subTotal at this time.
  704. if (t == 0)
  705. {
  706. subTotal = (number2 * number1) + remainder;
  707. cout << subTotal << " = (" << number2 <<" * " << number1 << ") + " << remainder << endl;
  708. remainder = 0;
  709. }
  710. //If t is larger than zero we need to add total.num[y] to subTotal.
  711. else
  712. {
  713. subTotal = (number2 * number1) + remainder + total.num[y];
  714. cout << subTotal << " = (" << number2 <<" * " << number1 << ") + " << remainder << " + " << total.num[y]<< endl;
  715. remainder = 0;
  716.  
  717. /*******************************************************************
  718. * If subtotal is equal to or greater than ten then we have to do both
  719. * modulas and division on it get the correct subTotal and the remainder to
  720. * be carried over.
  721. *******************************************************************/
  722. if(subTotal >=10)
  723. {
  724. total.getSubDivide(subTotal, remainder);
  725. }
  726. //If remainder is bigger than zero when we are done we have to place it at the start of the array.
  727. if (remainder > 0 && y < 0)
  728. {
  729. total.getIncrease(total, remainder);
  730. }
  731. else{}
  732. }
  733. /*******************************************************************
  734. * If subtotal is equal to or greater than ten then we have to do both
  735. * modulas and division on it get the correct subTotal and the remainder to
  736. * be carried over.
  737. *******************************************************************/
  738. if(subTotal >=10)
  739. {
  740. total.getSubDivide(subTotal, remainder);
  741. }
  742. //If subTotal is bigger than zero when we are done we have to place it at the start of the array.
  743. //Set remainder to subTotal, set subTotal to zero and call getIncrease.
  744. if (subTotal > 0 && y < 0)
  745. {
  746. remainder = subTotal;
  747. subTotal = 0;
  748. total.getIncrease(total, remainder);
  749. }
  750. else //Just put subTotal in toal.num[y].
  751. {
  752. total.num[y] = subTotal;
  753. subTotal = 0; //Reset subtotal to zero to make sure it's empty.
  754. y--;
  755. }
  756. }
  757. t++;
  758. }
  759. }
  760. system("PAUSE"); //If removed will not show answer at end of program. (ie 5 * 5)
  761. //If remainder is bigger than zero when we are done we have to place it at the start of the array.
  762. if (remainder > 0)
  763. {
  764. total.getIncrease(total, remainder);
  765. }
  766. else
  767. {}
  768. // system("PAUSE"); //If removed will not show answer at end of program. (ie 11 * 11) if both left in then 11 * 11 does not work.
  769. return total;
  770. }
thanks
Cary
Last edited by Narue; Mar 31st, 2008 at 12:01 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: What am I doing wrong.

 
0
  #2
Mar 31st, 2008
> num = new int(size);
This only allocates 1 (that's ONE) int, and initialises it to size.

Compare with
num = new int [ size ];

Oh, and next time you want to dump 700+ lines of code on a forum, consider producing a simplified example which demonstrates the problem more succinctly.
For starters, this may in fact help you figure out the problem for yourself.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 6
Reputation: Carwy has a little shameless behaviour in the past 
Solved Threads: 0
Carwy Carwy is offline Offline
Newbie Poster

Re: What am I doing wrong.

 
0
  #3
Mar 31st, 2008
Originally Posted by Salem View Post
>
For starters, this may in fact help you figure out the problem for yourself.
For starts thanks for the info. I hope it works.
Second I will not be asking anymore question on this forum. To many rude people.
Mod please close this thread and delete my user name and account.
Oh and by the way it did not fix it. Thanks annyway.
Last edited by Carwy; Mar 31st, 2008 at 4:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: What am I doing wrong.

 
0
  #4
Mar 31st, 2008
> Oh and by the way it did not fix it.
I never suggested that would be the single and only fix.

But as it seems you expect people to wade through hundreds of lines of code to fix ALL your problems, then get all huffy when people tell you to otherwise, then I expect your programming career will be mercifully short.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 6
Reputation: Carwy has a little shameless behaviour in the past 
Solved Threads: 0
Carwy Carwy is offline Offline
Newbie Poster

Re: What am I doing wrong.

 
-2
  #5
Mar 31st, 2008
Originally Posted by Salem View Post
> Oh and by the way it did not fix it.
I never suggested that would be the single and only fix.

But as it seems you expect people to wade through hundreds of lines of code to fix ALL your problems, then get all huffy when people tell you to otherwise, then I expect your programming career will be mercifully short.
Well lets see this is my second semester of school. How long did you have to become an ass?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
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: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: What am I doing wrong.

 
0
  #6
Mar 31st, 2008
Originally Posted by Carwy View Post
Well lets see this is my second semester of school. How long did you have to become an ass?
If you cant' take the heat then get out of the kitchen. Second semester student is very good, but after you graduate and get your first job you will only begin learning. You will always have supervisors and peers who will criticize you and your work. So get over it and get used to it.

The people at DaniWeb are here to help you and donate their time to do so free of charge. So don't bit the hand that feeds you.
Last edited by Ancient Dragon; Mar 31st, 2008 at 11:13 pm.
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: Sep 2007
Posts: 6
Reputation: Carwy has a little shameless behaviour in the past 
Solved Threads: 0
Carwy Carwy is offline Offline
Newbie Poster

Re: What am I doing wrong.

 
0
  #7
Apr 1st, 2008
Originally Posted by Ancient Dragon View Post
If you cant' take the heat then get out of the kitchen. Second semester student is very good, but after you graduate and get your first job you will only begin learning. You will always have supervisors and peers who will criticize you and your work. So get over it and get used to it.

The people at DaniWeb are here to help you and donate their time to do so free of charge. So don't bit the hand that feeds you.
You boy's setting at desk all day have no clue what heat is.
That is why I picked this job. That and the fact I can not do my first love any more. Because I have cancer.
So if you want to talk about heat why not leave that nice chair and join the your men in Iraq or Afganestain and then tell me about heat.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
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: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: What am I doing wrong.

 
1
  #8
Apr 1st, 2008
Originally Posted by Carwy View Post
So if you want to talk about heat why not leave that nice chair and join the your men in Iraq or Afganestain and then tell me about heat.
Been there, and done that (VietNam). I serverd 23 years in USAF (1962-1985) but I admit I was never shot or a POW. We have no greater respect than for our military and civilians now serving in Iraq and elsewhere around the world. That was not the kind of heat I was talking about.

But this comment will never be seen by you because you turned tail and ran at the first sign of criticism. Oh well.
Last edited by Ancient Dragon; Apr 3rd, 2008 at 12:30 am.
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: Jan 2008
Posts: 3,829
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: What am I doing wrong.

 
0
  #9
Apr 1st, 2008
Sorry to hear you have cancer. I wish you the best on that. That doesn't change the fact that you are acting like a complete jerk. You have no idea who sits at a desk all day, who doesn't, and who has been to Iraq and Afghanistan.

That's all irrelevant to the fact that you dumped hundreds of lines of code onto a thread with little to no explanation of what the problem was and expected other people to figure the entire thing out. Someone gives you some constructive criticism on that and you get angry and call him rude and threaten to never come back. When he calls you on it, you insult him again.

Get a better attitude or please fulfill your promise and don't come back.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: What am I doing wrong.

 
1
  #10
Apr 1st, 2008
Maths lessons for new posters.

Before you post some massive post, consider the following.

1. There is a small number of regular helpers who end up answering the vast majority of posts (say 5),
2. Each such helper has a finite amount of time to spend on the forum (say 1 hour per day),
3. The number of posts in any 24 hour period across the C and C++ forums is say 60.

So assuming a perfect distribution of helpers to posters (which is of course false), the ideal scenario is that each helper can affort to spend an average of 5 minutes on a post.

And that's 5 minutes to
- read it,
- understand it,
- research answers, test possible solutions,
- compose an answer.

In practice, that means your initial post needs to be read and understood within a couple of minutes to be reasonably sure of attracting the attention you want.

In the first instance, you need to post one specific question, and a small section of nicely formatted code. You can then post more code if asked to do so later in the thread.

You're in competition with all the other posters for attention; Posts which
- are poorly formatted,
- overly long,
- don't get to the point
are going to sink to the bottom of the helpers' "to do" list, and probably fall off the end if time runs out.

I could (along with everyone else) just ignored your post and you would have moved on silently (although none the wiser about your netiquette errors).



Now you can play around with the numbers, but you're not going to get someone to spend that whole hour on your massive 500+ lines of code just because you ask nicely.


Even at 1 line a second scanning the code, it would take well over 10 minutes just to get through your post. To actually read and comprehend it might take half an hour or more. Then there's the problem of constructing the exact circumstances of the problem (which you didn't post), just to get to the point of being able to test your code.

So tell me, why should anyone invest that amount of time helping an obvious ingrate who loses it at the first sign of trouble?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
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