Jump to content

C++


phatazzmoose

Recommended Posts

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

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

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

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

:angry:

 

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

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

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

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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...