Extracting and comparing numbers

This is a discussion on Extracting and comparing numbers within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, I'm new to C++ and know how to extract numbers (for example, to extract 1 from 1289371 I ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    79

    Extracting and comparing numbers

    Hey everyone, I'm new to C++ and know how to extract numbers (for example, to extract 1 from 1289371 I need to do modulo 10 [% 10] and I can do it in a while loop with dividing to keep using modulo on the numbers after) but I don't know how to extract a number and then extract the one after and compare to see which one is larger and then do the same thing again and find out which one is smaller and print them both out.

    i.e. Input : 783214
    prints out: largest number is 8
    smallest number is 1


    Can anyone help me?
    Thanks.

    My code so far:
    Code:
    int largeNum;
    int smallNum;
    int current1;
    int current2;
    
    while (num != 0){
    		
    		current1 = num % 10;
    		num /= 10;
    		current2 = num % 10;
    		if( current1 > current2 ){
    			largeNum == current1;
    		}
    		if( current1 < current2 ){
    			largeNum == current2;
    		}
            
        }
    	
    	cout << "The largest digit : " << largeNum << endl;
    	cout << "The smallest digit : " << smallNum << endl;
    Last edited by .C-Man.; 02-28-2011 at 05:21 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    Begin with this:
    To do what you describe, what do you need to do? Break it down into steps.
    Then take it a step further. Break it into smaller pieces. How can you make a computer understand how to do it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > largeNum == current1
    1. You need =, not ==

    2. One of the if statements should be testing, and then assigning smallNum

    Also, you need to initialise say largeNum = 0, and smallNum = 10;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Quote Originally Posted by Salem View Post
    > largeNum == current1
    1. You need =, not ==

    2. One of the if statements should be testing, and then assigning smallNum

    Also, you need to initialise say largeNum = 0, and smallNum = 10;
    I realized that current2 was the same thing as current1... is there a way I can compare current1 to itself every time the loop restarts? because I can't just say
    Code:
    if( current1 > current1)
    right?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    Of course you can do that, but what's the point?
    Again, what you have is small logic errors. Simple errors.
    So either a) translate it to logic, fix it, then translate back, or
    b) use a debugger.

    You shouldn't get stuck on such trivial errors.
    We'll help you learn the knowledge to squash them like bugs in the future, but you have to do it yourself!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    I'm trying but I don't know what to do in order to fix it. That's why I'm on here trying to ask for at least tips or something because I'm new to C++ and I'm having hard time fixing these kinds of issues.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    That's why I'm asking you to formulate a flowchart or pseudo code from your code. Something that describes the logic in the code. And something that describes the logic that you would need to do it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    With this:
    Code:
    while (num != 0){
    		
    		current1 = num % 10;
    		num /= 10;
    		current2 = num % 10;
    		cout <<"curr:" << current1 << endl;
    		cout <<"curr2:" << current2 << endl;
    		if( current1 > current2 ){
    			largeNum = current1;
    		}
    		if( current1 < current2 ){
    			smallNum = current1;
    		}
            
        }
    
    	cout << "The largest digit : " << largeNum << endl;
    	cout << "The smallest digit : " << smallNum << endl;
    I got this:

    Enter an integer: 9372
    curr:2
    curr2:7
    curr:7
    curr2:3
    curr:3
    curr2:9
    curr:9
    curr2:0
    The largest digit : 9
    The smallest digit : 3

    So I know what curr and curr2 are doing each time the while loop is looped...

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    So you do. Yet the answer is not correct. Now why is that?
    If you would just compare your current logic to the logic that is needed, you would have your answer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Well in this case it is correct(for largest digit) but usually isn't because I'm just comparing the curr1 and curr2 from the same loop only and not saving it but I don't know how to do that... and I don't know how to incorporate smallNum into this either...

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    Your confusion lies from the fact that you don't have a proper logic in mind.
    Formulate how you would do this first. Then it will all become clear.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Thanks, I guess...

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    I don't mean this in a bad way. Formulate your logic and check back if you get stuck.
    We'll guide you on this path. We will walk it together and in the end, you will be smarter and be able to tackle these problems on your own in the future.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    I've been working on this all week and It's due tomorrow. I know my logic is correct in normal situations but I don't know how to make it logical for the computer. Can someone please help me?

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    What is the logic that you use for normal situations?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

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