Results 1 to 15 of 15

Thread: 1+4 = 14

  1. #1
    DaoK
    Guest

    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;
    }

  2. #2
    DaoK
    Guest
    I forget to tell you the declaration :

    #include <iostream.h>
    #include <string.h>

    int lucky(int x);
    int x;
    int y;
    int luckyt;

  3. #3
    DaoK
    Guest
    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

  4. #4
    Destined Soul
    Guest
    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.

  5. #5
    DaoK
    Guest
    Thank you !!!! This code is better than mine Thank you a lots

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7
    DaoK
    Guest
    x and X are the same or they are different variable ?

  8. #8
    Destined Soul
    Guest
    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

  9. #9
    DaoK
    Guest
    ok i will try that.

  10. #10
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    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: " << ::<< endl;
          
    cout << "The Local x: " << << endl;
          return 
    0;

    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  11. #11
    DaoK
    Guest
    ::x ? What is that I do not understand double :.

  12. #12
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    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
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  13. #13
    DaoK
    Guest
    Thank you for the link man! It explained very well the ::

  14. #14
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    btw, i wrote that article
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  15. #15
    DaoK
    Guest
    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
  •  



Click Here to Expand Forum to Full Width