-
1+4 = 14
I want to take a number ( exemple 14 ) than extract the 1 in X and 4 in y. Then I want to add x+y who will give me 5. Why this code dont work ?
Code:
int lucky(int x)
{
if (x > 10)
{
while(x < 10)
{
y= x % 10;
x= x / 10;
}
}
else
{
luckyt = x + y;
cout<<luckyt<<endl;
}
luckyt = x + y;
cout<<luckyt<<endl;
return 0;
}
-
I forget to tell you the declaration :
#include <iostream.h>
#include <string.h>
int lucky(int x);
int x;
int y;
int luckyt;
-
This is all the project:
Code:
#include <iostream.h>
#include <string.h>
int lucky(int x);
int x;
int y;
int luckyt;
int laststring;
int main()
{
char string[20]; //A nice long string
cout<<"Please enter your name: ";
// cin.getline(string, char(string), '\n'); //The user input goes into string
cin.getline(string, 20, '\n'); //The user input goes into string
cout<<"Your long string was:"<<endl<<string<<endl;
cout<<"You name have "<<(strlen(string))<<" letters"<<endl;
cout<<"Your lucky number is"<<endl;
laststring=(strlen(string));
lucky(laststring);
return 0;
}
int lucky(int x)
{
luckyt = 0;
if (x > 10)
{
while(luckyt < 10)
{
y= x % 10;
x= x / 10;
luckyt = x + y;
}
}
else
{
luckyt = x + y;
cout<<luckyt<<endl;
}
cout<<luckyt<<endl;
return 0;
}
I dont see what's wrong, I dont have any error but I have no answer :(
-
I'm not sure specifically what you're wanting to do here. If your name has, say, 24 letters, you want to return the value (2+4) or 6? Also, if the name has 3 letters (like "Jim"), do you want the lucky number to be 3 or 6? Your current code produces 6, so I wasn't sure about this. If you just want 3, here's a slight walkthrough:
set luckyt = 0
To get the last digit , you want to take the modulus/remainder of x. (luckyt = x % 10)
if x is greater than ten, there's still more to add. If it is, divide it by 10, and add the remainder to luckyt (luckyt = luckyt + (x%10)). Check it again, repeating BOTH math operations if needed.
Once x is less than 10, and you've added the remainders as needed, you can return (or output) the final answer.
ie: take 13928. (Just an example) Let's call luckyt "A" (for answer).
A = 0
A = 13928 % 10 = 8
is X > 10? Yes.
x = x/10 = 1392
A = A + x%10 = 8+2 = 10
is X > 10? Yes.
x = x/10 = 139
A = A + x%10 = 10 + (9) = 19
is X > 10? Yes.
x = x/10 = 13
A = A + x%10 = 19 + (3) = 22
is X > 10? Yes.
x = x/10 = 1
A = A + x%10 = 22 + (1) = 23
is X > 10? No. We're done.
Answer is 23. (If you want 5 instead of 23 (ie 2+3) then just call the function on your answer.)
Chances are no one's name is that long, but it helps for being an example. I hope this helps somewhat. If it doesn't, just say so. :D
Destined.
-
Thank you !!!! This code is better than mine :) Thank you a lots:)
-
and you used the name x two times (one time global and one time as parameter to lucky. This is why globals are dangerous.
-
x and X are the same or they are different variable ?
-
At the top of your program, in the variable declarations, you have "int x;" As well, you define the function "int lucky(int x)" It is this "x" that you pass to the lucky function that might get confused.
The two are indeed seperate, but when you type "if (x > 10)" you have to ask yourself if you're wanting the global variable or the one passed to the function. Although you might know which one you're referring to, it's a bad coding practice. This is because someone else, or even yourself, might become confused later on as to which one you wanted. (Not so nice to debug later on, either.)
Destined
-
-
you can always refer to the global x using the scope resollution operator like this:
PHP Code:
int luck(int x)
{
cout << "The global x: " << ::x << endl;
cout << "The Local x: " << x << endl;
return 0;
}
-
::x ? What is that I do not understand double :.
-
It is the scope resolution operator. and its the same thing u use for class members:
PHP Code:
className::member; // using class scope.
namespaceName::member; // using namespace scope.
::member; // using global scope.
// or
std::member; // using std namespace scope.
That is all that it means, hope that helped?>
for more:
http://library.thinkquest.org/~C0111...ual.php?tid=15
have fun:)
-
Thank you for the link man! It explained very well the :: :)
-
btw, i wrote that article :)
-
oops :rolleyes: heyhey Good job