C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 20th, 2004, 09:59 PM
smartnsweetgirl smartnsweetgirl is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 1 smartnsweetgirl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Arrow How to convert int to apstring?

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;


}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > How to convert int to apstring?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT