Public Group

# I need a little help

This topic is 2503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello peoples...
i am currently trying to impruve my skills with c++ and came to a small problem. what i am trying to create is a calculator that takes a two didget number , and for example 76, and turns it into 7 + 6 whitch is 13 and then add 1 + 3 wicth gives you 4. i have the basics planned out however i cant findout how to seperate the numbers. for example i want to take the number 76 and make it just 7 and 6. im not really sure how to do that so any help wouild be apreceated.

ps i know my spelling is bad sorry for that and this program is probably stupid and weird but whatever it looks like fun.

##### Share on other sites
It sounds like what your doing would benefit from the use of remainders and integer division. If you are working with integers, you can just divide by 10 to get the first digit. This is because the remainder gets dropped during integer division. You can get that second digit by taking the remainder of the number when divided by 10, which is represented by a % sign.

int number=76;
int firstDigit=number/10; //Integer division will ditch the remainder
int secondDigit=number%10; //The remainder operation will give you the remainder only

It is late, and I haven't tested the code, but it should work ok. Go ahead and give it a try: I'll just claim insanity if I'm wrong. =P

PS: Nice picture, I choose the icosahedron.

##### Share on other sites

hello peoples...
i am currently trying to impruve my skills with c++ and came to a small problem. what i am trying to create is a calculator that takes a two didget number , and for example 76, and turns it into 7 + 6 whitch is 13 and then add 1 + 3 wicth gives you 4. i have the basics planned out however i cant findout how to seperate the numbers. for example i want to take the number 76 and make it just 7 and 6. im not really sure how to do that so any help wouild be apreceated.

ps i know my spelling is bad sorry for that and this program is probably stupid and weird but whatever it looks like fun.

76%10 = 6
76/10 = 7

if you have a number x and want a vector of its digits you can do:
 int x = 54354; std:vector<int> digits; do { digits.push_back(x%10); x/=10; } while (x!=0); 

or, if you want to keep adding them up and repeat the process until you only got a single digit number left:

 std::vector<int> getDigits(int x) { int x = 54354; std:vector<int> digits; do { digits.push_back(x%10); x/=10; } while (x!=0); return digits; } ...main... int number = 456535; std::vector<int> digits = getDigits(number); while (digits.size()>1) { number = 0; for (int i=0;i<digits.size();i++) { number+=digits; } digits = getDigits(number); } assert(digits.size()==1); std::cout << digits[0]; 

##### Share on other sites
Thans for yout help... I just finished the program and it is late so im lazy and i probably looks ugly with all those variables so whatever... here is the code:

#include "stdafx.h"
#include <iostream>

int main()
{
int origonalNum;
std::cout << "Welcome to my Fluke calculator!\n";
std::cout << "Please type in a TWO diget number\n";
std::cout << ">";
std::cin >> origonalNum;

while (origonalNum > 99)
{
std::cout << "Invalid choice. I said a TWO diget number\n";
std::cout << ">";
std::cin >> origonalNum;
}
int X = origonalNum / 10; //gets the tens didget
int Y = origonalNum % 10; //gets the ones didget
int Z = X + Y; // adds the first and second didget together
if ( Z > 9 )
{
int A = Z / 10; // gets the tens didget of Z
int B = Z % 10; // gets the ones didget of Z
int I = A + B; // adds A and B
std::cout << origonalNum << " Really = " << I << "\n\n";
system("pause");
return 0;
}
std::cout << origonalNum << " Really = " << Z << "\n\n";
system("pause");
return 0;
}

For the most part it hasent come up with a two didget number the second time so i think its good but i will revise this in the morning... i am soooo tired

##### Share on other sites
Practically this works, but I might be able to give you some style tips;
I know you said you were tired and it's probably a hard task figuring out the problem, but there are some improvements which could make you code much clearer.

First of all using system("pause") is considered bad style. I know it helps a lot when testing these kinds of small programs, but try to do it without and run it from the command line or maybe even using debug mode. It's better to do it right in the first place than losing bad habits on the long run.

And it might be worth it looking into reusing lines, which not only helps clarity, but also lets you do less work. And aren't we all a bit lazy deep inside?

Let me give you one example:

if ( Z > 9 ) { int A = Z / 10; // gets the tens didget of Z int B = Z % 10; // gets the ones didget of Z int I = A + B; // adds A and B std::cout << origonalNum << " Really = " << I << "\n\n"; system("pause"); return 0; } std::cout << origonalNum << " Really = " << Z << "\n\n"; system("pause"); return 0;

Those last 3 statements in the if part and the final three statements are the same. If you see something like that happening you should think about a way to do that only once. This clears up the code and you get to type 3 less statements.
If Z is still bigger than 9 in this example you use Z to 'calculate' the new variables A and B (here you can use the old X and Y as well ;)), do you still need the value in Z? Probably not. So instead of the new variable I you could just put the result back in Z and then leave the if-statement. If you do that you can use the last three statements in the code above for both situations. So it will become like this:

if ( Z > 9 ) { X = Z / 10; // You don't use X anymore, so you can reuse this variable Y = Z % 10; // Same for Y Z= X + Y; // adds X and Y and puts it back into Z } //Now you only need this one instead of two that only differ in one variable std::cout << origonalNum << " Really = " << Z << "\n\n"; return 0;

You can even try to challenge yourself and only use X,Y and Z only once. Maybe a loop can help with that? ;)
Maybe you already understood all these things and this was just quick coding, or maybe this is new to you. Either way, I hope I helped you out a bit.

Oh, and you might try to use the code tags on the forum, which makes your post even more clear:
#include <iostream> int main() { blablabla; return 0; }

##### Share on other sites

...

First of all using system("pause") is considered bad style. I know it helps a lot when testing these kinds of small programs, but try to do it without and run it from the command line or maybe even using debug mode. It's better to do it right in the first place than losing bad habits on the long run.
...

While using system("pause") isn't the best practice, it is good to wait for some key before quitting. instead of using system() call, just use a standard C++ call. For simplicity's sake, you could just do something like this:
 std::string QuitInput; std::cout << "Press Enter to Quit."; std::cin >> QuitInput; return 0; 

There are other methods of pressing a key to end, but this is the easiest.

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633741
• Total Posts
3013624
×