...

View Full Version : variable question



XxANONxX
10-03-2007, 08:22 AM
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?

nikkiH
10-03-2007, 02:27 PM
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.

XxANONxX
10-03-2007, 05:59 PM
I have no idea what that is, but thanks for the help.
Time to do some research.

nikkiH
10-03-2007, 06:53 PM
Are you using c++ .NET?
http://www.functionx.com/vcnet/collections/arraylist.htm

ralph l mayo
10-03-2007, 08:09 PM
Standard C++:



// 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/

oracleguy
10-04-2007, 03:44 AM
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.

nikkiH
10-04-2007, 02:34 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum