teaching myself C++ prob.

This is a discussion on teaching myself C++ prob. within the C++ Programming forums, part of the General Programming Boards category; Hi, I hope I got this code tab thing right? anyway, I want to make this programme, which i am ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    teaching myself C++ prob.

    Hi, I hope I got this code tab thing right?
    anyway, I want to make this programme, which i am following an exercise out of a book! If the first number is a multiple of the second. Sometimes it works sometimes it dont, what am I doing wrong? In easy terms please

    Code:
    #include <iostream>
    
    using namespace std;
    
    int num1;
    int num2;
    int answer;
    
    int main()
    {
    	cout << "Enter two numbers to find the multiple of each other" << endl;
    	cout << "Enter number 1" << endl;
    	cin >> num1;
    	cout << "Enter number 2" << endl;
    	cin >> num2;
    
    	answer = num1 % num2;
    
    	if( answer == 1 )
    		cout << num1 <<" and " << num2 << " are NOT multiples of each other" << endl;
    
    	if( answer == 0 )
    	cout << num1 <<" and " << num2 << " ARE multiples of each other" << endl;
    
    	return 0;
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    your problem is that the answer can be something other than 1, and it still could not be a multiple....for instance, 11/4...its 2 with a modulus of 3....it wont display anything because of that...

    you can just have it like this.

    Code:
    if (answer == 0)
        cout << num1 <<" and " << num2 << " ARE multiples of each other" << endl;
    
    else
         cout << num1 <<" and " << num2 << " are NOT multiples of each other" << endl;
    edit: as far as your code thing goes, you have the backslash the wrong way, it should be /

    edited for reverse outputs
    Last edited by Jamsan; 02-27-2003 at 06:25 PM.

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    code needs to be CODE in the tags.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by RoD
    code needs to be CODE in the tags.
    It was, just the wrong slash being used though. I've fixed it now.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    when i was looking a sec ago he had [code] not [CODE]

  6. #6
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by RoD
    when i was looking a sec ago he had [ code ] not [ CODE ]
    capitilization doesn't matter on code tags

    Code:
    this was done with a lowercase code and /code

  7. #7
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i honestly didn't know that, cool thnx for the info.

  8. #8
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Might want to declare your variables inside of main(). Although in this program it really doesn't matter, but it is better to stay away from global variables.

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Your code may not produce expected results. What happens when i enter 6 for the first number and 100 for the second. It divides 6/100 in integer division thats 0. This will make it happily report that 6 and 100 are multiples of each other. How you gonna fix it? I could tell you but its simple enough to see how.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Use a float data type instead...

  11. #11
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    It divides 6/100 in integer division thats 0.
    Use a float data type instead...
    I may be wrong, but I didn't see any division anywhere in the code. The only thing that comes close is the modulus. There's no reason to use a float.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You dont see the division then u cant understand what operator % does then. It performs a division, throws away the quotient and return the remainder.

    6/100 = 0.06 or 0 as integer division.

    6 and 100 are multiples.

    Are they? I dont think so. Cmon use ur head. Its not there just to give u something to look at in the mirror.

    edit...

    Also using a float is the WRONG answer..... Its even simpler. You just need to think about it a little. why does the calculation go wrong if you enter 6 then 100 but go right if you enter 100 then 6.
    Thats a big clue!
    Last edited by Stoned_Coder; 02-28-2003 at 06:56 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  13. #13
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Originally posted by Stoned_Coder
    You dont see the division then u cant understand what operator % does then. It performs a division, throws away the quotient and return the remainder.
    Your knowledge of how the modulus operator works is correct, and 6 / 100 will return 0, but 6 % 100 will not return 0. It will return 6.
    why does the calculation go wrong if you enter 6 then 100 but go right if you enter 100 then 6.
    The calculation doesn't go wrong. 6 % 100 is a totally different question than 100 % 6.

    However, part of the program is a bit misleading. For example, take 32 and 64. 32 % 64 will return 32, but 64 % 32 will return 0. This is because 64 is a multiple of 32, but 32 is not a multiple of 64.
    Code:
    	if( answer == 0 )
    	cout << num1 <<" and " << num2 << " ARE multiples of each other" << endl;
    The only time two numbers will be multiples of each other is when the two numbers are the same.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  14. #14
    Rog
    Rog is offline
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    Originally posted by Stoned_Coder
    Are they? I dont think so. Cmon use ur head. Its not there just to give u something to look at in the mirror.
    ...possibly smoke'n too much cheap ganja has damaged it.

  15. #15
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    lol my turn for a brainfart day! $$$$ happens then u smoke it!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deal or No Deal listbox prob
    By kryptkat in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2009, 07:53 PM
  2. prob with multiple forms accessing header file
    By silkyt in forum Windows Programming
    Replies: 7
    Last Post: 11-03-2007, 05:14 PM
  3. Directx D3DXMatrixLookAtLH prob?
    By Frog22 in forum Game Programming
    Replies: 4
    Last Post: 12-10-2002, 02:20 PM
  4. teaching "C" to beginning programmers (Grade 11's)
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 06-12-2002, 07:06 AM
  5. Replies: 14
    Last Post: 06-11-2002, 07:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21