striderx240 103 Report post Posted February 23, 2010 So i just wrote this program right now and i quite figure out whats wrong with it =/ Any suggestions would be helpful. Thank you p.s sorry, im still new to this site, not sure how to copy the code for easy viewing. #include <iostream> using namespace std; //This program will test three functions capable of reading, adding, //and printing 100-digit numbers. // Do not change these function prototypes: void readBig(int[]); void printBig(int[]); void addBig(int[], int[], int[]); // This constant should be 100 when the program is finished. const int MAX_DIGITS = 100; //There should be no changes made to the main program when you turn it in. int main() { // Declare the three numbers, the first, second and the sum: int num1[MAX_DIGITS], num2[MAX_DIGITS], sum[MAX_DIGITS]; cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: "; readBig(num1); cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: "; readBig(num2); addBig(num1, num2, sum); printBig(num1); cout << "\n+\n"; printBig(num2); cout << "\n=\n"; printBig(sum); cout << "\n"; return 0; } void ReadBig(int num[MAX_DIGITS]) { int j, length, temp, f; string s; cin>>s; length=s.length(); f=(length-1)/2; for( j=0;j<=MAX_DIGITS-1;j++) { num[j]=0; } for(j=0;j<=length-1;j++) { num[j]=s.at(j)-'0'; } for(j=0;j<=f;j++) { temp=num[s.length()-1-1]; num[s.length()-1-1]=num[j]; num[j]-temp; } } //ReadBig will read a number as a string, //It then converts each element of the string to an integer and stores it in an integer array. //Finally, it reverses the elements of the array so that the ones digit is in element zero, //the tens digit is in element 1, the hundreds digit is in element 2, etc. void addBig(int num1[], int num2[], int sum[]) { int j, ones=0, carry=0; for(j=0;j<=MAX_DIGITS-1;j++) { sum[j]=num1[j]+num2[j]; } for(j=0;j<=MAX_DIGITS-2;j++) if(sum[j]>=10) { ones-sum[j]%10; carry-sum[j]/10; sum[j]=ones; sum[j+1]=sum[j+1]+carry; } } //AddBig adds the corresponding digits of the first two arrays and stores the answer in the third. //In a second loop, it performs the carry operation. void printBig(int num[MAX_DIGITS]) { int j=0; int skip=MAX_DIGITS-1; while(num[skip]==0 && skip>0) skip--; for(j=skip;j>=0;j--) cout<<num[j]; } //PrintBig uses a while loop to skip leading zeros and then uses a for loop to print the number. 0 Share this post Link to post Share on other sites
Thermodynamics 443 Report post Posted February 23, 2010 Well to help with the first issue look here. 0 Share this post Link to post Share on other sites
Palidine 1315 Report post Posted February 23, 2010 //There should be no changes made to the main program when you turn it in.This is homework so we won't answer it for you. Why don't you describe what you mean by "can't figure out what's wrong"? Is it crashing? not compiling? is the assignment to figure out what's wrong?The typical way to figure out what's wrong is to run the program through your debugger and step through the code making sure it's doing what you think it should be doing.-me 0 Share this post Link to post Share on other sites
striderx240 103 Report post Posted February 23, 2010 okay fixed one problem =], as for the program, yes its homework, i've tried debugging it and i cant quite understand whats wrong with it. I have commented the lines that error when debugged.#include <iostream>using namespace std;//This program will test three functions capable of reading, adding,//and printing 100-digit numbers.// Do not change these function prototypes:void readBig(int[]);void printBig(int[]);void addBig(int[], int[], int[]);// This constant should be 100 when the program is finished.const int MAX_DIGITS = 100;//There should be no changes made to the main program when you turn it in.int main() { // Declare the three numbers, the first, second and the sum: int num1[MAX_DIGITS], num2[MAX_DIGITS], sum[MAX_DIGITS]; cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: "; readBig(num1); cout << "Please enter a number up to "<<MAX_DIGITS<< " digits: "; readBig(num2); addBig(num1, num2, sum); printBig(num1); cout << "\n+\n"; printBig(num2); cout << "\n=\n"; printBig(sum); cout << "\n"; return 0; }void ReadBig(int num[MAX_DIGITS]){ int j, length, temp, f; string s; cin>>s; //error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) length=s.length(); f=(length-1)/2; for( j=0;j<=MAX_DIGITS-1;j++) { num[j]=0; } for(j=0;j<=length-1;j++) { num[j]=s.at(j)-'0'; } for(j=0;j<=f;j++) { temp=num[s.length()-1-1]; num[s.length()-1-1]=num[j]; num[j]-temp; //C4552: '-' : operator has no effect; expected operator with side-effect }}//ReadBig will read a number as a string,//It then converts each element of the string to an integer and stores it in an integer array.//Finally, it reverses the elements of the array so that the ones digit is in element zero,//the tens digit is in element 1, the hundreds digit is in element 2, etc.void addBig(int num1[], int num2[], int sum[]){ int j, ones=0, carry=0; for(j=0;j<=MAX_DIGITS-1;j++) { sum[j]=num1[j]+num2[j]; } for(j=0;j<=MAX_DIGITS-2;j++) if(sum[j]>=10) { ones-sum[j]%10; //C4552: '-' : operator has no effect; expected operator with side-effect carry-sum[j]/10; //C4552: '-' : operator has no effect; expected operator with side-effect sum[j]=ones; sum[j+1]=sum[j+1]+carry; }}//AddBig adds the corresponding digits of the first two arrays and stores the answer in the third.//In a second loop, it performs the carry operation.void printBig(int num[MAX_DIGITS]){ int j=0; int skip=MAX_DIGITS-1; while(num[skip]==0 && skip>0) skip--; for(j=skip;j>=0;j--) cout<<num[j];}//PrintBig uses a while loop to skip leading zeros and then uses a for loop to print the number. 0 Share this post Link to post Share on other sites
choffstein 1090 Report post Posted February 23, 2010 ones-sum[j]%10;carry-sum[j]/10;What do you expect those lines to be doing? 0 Share this post Link to post Share on other sites
striderx240 103 Report post Posted February 23, 2010 holy S*@#$ that totally flew over my head lol, i meant to put "=", thanks man, put still I have problem with the string s. unsure about that. 0 Share this post Link to post Share on other sites
fastcall22 10853 Report post Posted February 23, 2010 I see the use of std::string but I don't see <string> included anywhere... 0 Share this post Link to post Share on other sites
striderx240 103 Report post Posted February 23, 2010 wow im so noob, totally slipped my mind, thanks again guys, i'll try to thoroughly correct it next time, so i don't waste your guys time again =p 0 Share this post Link to post Share on other sites