phatazzmoose Posted February 15, 2005 Posted February 15, 2005 hey guys heres my question... i have to make a program for my lab that will input an integer like say... 15658 then the output has to be like this... same order: 1 5 6 5 8 reverse order: 8 5 6 5 1 how can i get the digits out of the integer???? Share this post Link to post Share on other sites More sharing options...
CNUco2007 Posted February 15, 2005 Posted February 15, 2005 i would type cast it to a string, then access the string like an array. i think you can do that. someone correct me if im wrong cuz i havent tried that in a while. i'm doing java right now in my cpsc425 class so my c++ skills are kinda slacking Share this post Link to post Share on other sites More sharing options...
phatazzmoose Posted February 15, 2005 Posted February 15, 2005 we havent got to strings yet tho we've done functions and we're starting on global and static variables... is there a function that you could #include to call and get the digits? Share this post Link to post Share on other sites More sharing options...
CNUco2007 Posted February 15, 2005 Posted February 15, 2005 hmmm, im not sure if there is a function. lemme think on this. i wish c++ had documentation like the online API Doc for Java lol. You can look up any class and find out how it works and its member functions. Share this post Link to post Share on other sites More sharing options...
phatazzmoose Posted February 15, 2005 Posted February 15, 2005 see we've only gone over functions and we're supposed to be able to code the labs only by what we've gone over in class there has to be a function call i could make or something... when you break a number up into its individual digits... dont you parse it... or something like that? Share this post Link to post Share on other sites More sharing options...
CNUco2007 Posted February 15, 2005 Posted February 15, 2005 well, even stuff im finding online is converting it to a string so thats not helping... since int is not a class there aren't any function associated w/ it. But i'm not sure if there is another class that can work w/ it or not to get those results. Share this post Link to post Share on other sites More sharing options...
phatazzmoose Posted February 15, 2005 Posted February 15, 2005 we have to use functions and variables to do it... surely someone can help me EDIT: incase you forgot.. all i need to do is take an integer like 152 and break it apart into 1, 5, and 2... any way to do this? Share this post Link to post Share on other sites More sharing options...
Nemo Posted February 15, 2005 Posted February 15, 2005 we have to use functions and variables to do it... surely someone can help me EDIT: incase you forgot.. all i need to do is take an integer like 152 and break it apart into 1, 5, and 2... any way to do this? 419136[/snapback] I can think of a brute force way to do it, but it's not very elegant: I'm assuming you have a function that can take the integer portion of a number (I'm not a C++ programmer). Take your number, divide it by ten and take the integer portion of the result and multiply it back by 10,. Using you example of 152, the result would be 150. Subtract it from the orignal number, 152, and the result is 2. Save the intermediate results and repeat with the number 15, then 1. Kinda kludgey, but it works. Share this post Link to post Share on other sites More sharing options...
Kamikaze_Badger Posted February 15, 2005 Posted February 15, 2005 I think you can use the stdlib.h library to do it... It's somewhere at cplusplus.com. Pseudo code would be like: typedef int[50] = BigNumbar; BigNumbar nInput = 0; cout << "Enter your number: "; cin >> nInput; (put each different integer into an array area) (output the array forwards and then backwards) Share this post Link to post Share on other sites More sharing options...
markiemrboo Posted February 15, 2005 Posted February 15, 2005 itoa() should be the one that converts integer to a 'string'. I think... Since I don't want to do my lab work i'll see what I can come up with. #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; int main() { int theNum = 154353; char toStr[20]; // num str base itoa(theNum, toStr, 10); for (unsigned int i = 0; i < strlen(toStr); i++) { printf("%c\n", toStr[i]); } system("pause"); } But... since programming classes seem to be a bit twisted these days I guess you're not allowed to use itoa() and you have to do it some other way.... Share this post Link to post Share on other sites More sharing options...
Kamikaze_Badger Posted February 15, 2005 Posted February 15, 2005 Well, he has to use a function... So it would look maybe like: #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; void numberFunc(); //Function prototype int main() { int theNum = 154353; char toStr[20]; // num str base itoa(theNum, toStr, 10); numberFunc; /* for (unsigned int i = 0; i < strlen(toStr); i++) { printf("%c\n", toStr[i]); } */ system("pause"); } void numberFunc() { for (unsigned int i = 0; i < strlen(toStr); i++) { printf("%c\n", toStr[i]); } I hanv't done any stuff with function prototypes since CPU Burninatar 3000, so... yea... Share this post Link to post Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now