| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I'm trying to make a digital root calculator where the user enters a positive integer value,
and the digital root is the product of the non-zero digits of the value. The program is supposed to find the digital root of the digital root and so on until a single digit is obtained. I have stored the value the user enters in an apstring so that I can access the individual digits of that value. I later convert them to digits so that I can do calculations, but at a certain point, I need to convert from a digit back to an apstring again so that I can access the individual digits of the next digital root. Is there a way to convert from an int to an apstring? Here is my program thus far: #include <iostream.h> #include <stdlib.h> #include "apstring.h" #include <iomanip.h> void printHeading(apstring title); //Postcondition: Centers the title on the screen apstring getInt(); //Postcondition: A valid integer entered by the user is returned to the main program int main() { printHeading("Digital Product Root Calculator"); apstring integer=getInt(); //The user-entered integer value int len=integer.length(); //The length of the entered value int product=1; //One successive digital root int intDigit=0; //The integer value of digit for(int counter;counter=7;counter--) integer=product; //<-----That's where I'm stuck at, the int value in product needs to be stored in apstring integer for(len;len>=1;len--) { apstring digit=integer.substr(len-1,1);//stores a digit from the integer in digit intDigit=atoi(digit.c_str()); //converts digit from apstring to integer cout<<"IntDigit: "<<intDigit<<endl; if (intDigit!=0) //multiplies an integer into the product if it is not zero {product=intDigit*product; cout<<"Inside Product: "<<product<<endl; } else product; } cout<<"Digital Root: "<<product<<endl; system("PAUSE"); return 0; } //Finds the length of the title, then centers the title on the screen according to its length void printHeading(apstring title) { int len; int tab; len=title.length(); tab=len/2 +40; cout<<setw(tab)<<title<<endl<<endl; } apstring getInt() { apstring num; //the entered integer //Prompts user to enter a value cout<<"Please enter a positive integer value 1-8 digits in length: "; getline(cin,num); cout<<endl<<endl; //Determines whether the entered value is negative int neg=num.find('-'); //Determines the length of the entered value int len=num.length(); //If entered value is not between 0 and 99,999,999, prompt user to reenter the value while (len>8||neg!=-1) { cout<<"Please reenter an positive integer value 1-8 digits in length: "; getline(cin,num); cout<<endl<<endl; neg=num.find('-'); len=num.length(); } return num; } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > How to convert int to apstring? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|