|
-
Sep 25th, 2001, 08:19 PM
#1
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;
}
-
Sep 25th, 2001, 08:22 PM
#2
I forget to tell you the declaration :
#include <iostream.h>
#include <string.h>
int lucky(int x);
int x;
int y;
int luckyt;
-
Sep 25th, 2001, 08:40 PM
#3
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
-
Sep 25th, 2001, 11:00 PM
#4
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. 
Destined.
-
Sep 26th, 2001, 05:26 AM
#5
Thank you !!!! This code is better than mine Thank you a lots
-
Sep 26th, 2001, 07:15 AM
#6
and you used the name x two times (one time global and one time as parameter to lucky. This is why globals are dangerous.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 26th, 2001, 06:01 PM
#7
x and X are the same or they are different variable ?
-
Sep 26th, 2001, 06:23 PM
#8
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
-
Sep 26th, 2001, 06:53 PM
#9
-
Sep 28th, 2001, 05:15 PM
#10
Fanatic Member
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;
}
-
Sep 28th, 2001, 06:21 PM
#11
::x ? What is that I do not understand double :.
-
Sep 28th, 2001, 09:17 PM
#12
Fanatic Member
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
-
Sep 29th, 2001, 10:38 AM
#13
Thank you for the link man! It explained very well the ::
-
Sep 29th, 2001, 12:28 PM
#14
Fanatic Member
btw, i wrote that article
-
Sep 29th, 2001, 03:06 PM
#15
oops heyhey Good job
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|