Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    variable question

    I just barely started learning C++ and I wrote a program that takes information from a .txt file and arranges it into certain categories. Pretty simple. It deals primarily with numbers between 0 - 100. However I got curious and was wondering if there is a way to pick out the numbers that do not fall into a category and display them separately on the console. The only way I could think to do this was to have a variable that gets created every time a number is not accounted for.

    If that doesn't make any sense I'll just make an example, 5 numbers are produced and it is looking for numbers between 1 - 100. The numbers entered are:

    105.3
    55.4
    -12.1
    87.6
    56.4

    How could I edit my program to display the numbers not between 1 - 100 separately?

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Store the numbers in an ArrayList. You can enumerate it and check values any time you want.
    Or, you can use 2 ArrayLists. When you read the input, if the number is in range, store it in the 1st. If it is not, store it in the 2nd.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have no idea what that is, but thanks for the help.
    Time to do some research.

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Standard C++:

    Code:
    // Helper function
    static bool not_between_one_and_one_hundred(const int i)
    {
    	return (i < 1 || i > 100);
    }
    
    
    // Elsewhere, in main() or another method
    std::vector<int> nums;
    nums.push_back(1);
    nums.push_back(-20);
    nums.push_back(40);
    nums.push_back(50);
    nums.push_back(110);
    
    for (std::vector<int>::iterator res = std::find_if(nums.begin(), nums.end(), not_between_one_and_one_hundred); 
         res != nums.end(); 
         res = std::find_if(++res, nums.end(), not_between_one_and_one_hundred))
    {
    	std::cout << *res << std::endl;
    }
    edit: missed that you're using decimals there, so s/int/double/
    Last edited by ralph l mayo; 10-04-2007 at 01:59 AM.

  • #6
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by nikkiH View Post
    Store the numbers in an ArrayList. You can enumerate it and check values any time you want.
    Or, you can use 2 ArrayLists. When you read the input, if the number is in range, store it in the 1st. If it is not, store it in the 2nd.
    If you are using C++ .NET, it'd be better to use a List class since it is more strongly typed. I've found it can be more of a pain if you use an ArrayList.

    If you are using standard C++, ralph's solution is the way to go.
    OracleGuy

  • #7
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Sorry, you're right, I forget about List generics. FWIU, they aren't available in 1.1 and we just switched to 2.0 recently.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •