Thread: Sorting Even/Odd numbers and Max/min from a file using while loops

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Sorting Even/Odd numbers and Max/min from a file using while loops

    Hello I am having a problem with my program. Everytime I try to start the program, I keep getting a continuously infinite stream of numbers and sentences. The purpose of this program is to take 50 values from a file and sort them into even and odd columns and to also find the max and min of the values, by only useing one while loop. I have tried everything am keep getting the infinite stream, I do not know what is wrong with my program and would appriciate a little help. Also there is the return 0; and closed brackets at the end, couldn't fit it all into the picture. Thank you in advance!
    Sorting Even/Odd numbers and Max/min from a file using while loops-capture-jpg

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Paste your code, not a picture of it.

    Perhaps you should begin by replacing your while condition with something like
    Code:
    while ( infile >> number ) {
      // do stuff
    }
    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.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    That fixed the infinite problem, but now only the even numbers are showing up and not the odd or the max/min. Also the numbers are not including the negative integers in the file. Sorry if this seems like a simple fix, but I'm new to c++ and am having a difficult time grasping it.

    Code:
    #include<iostream>
    #include<fstream>
    #include<cmath>
    usingnamespace std;
    int main()
    {
    ifstream infile;
    ofstream outfile;
    infile.open("Data_Values.txt");
    int number=-10;
    infile>>number;
     
     
    int max=-10000;
    int min=10000;
    cout<<"\n Your even numbers are:"<<endl;
    while (infile>>number){
    if (number%2==0){
    cout<<" "<<number;
    number++;
    }}
    cout<<"\n\n Your odd numbers are:"<<endl;
    while (infile>>number){
    if (number%2==1){
    cout<<" "<<number;
    number++;
    }}
    cout<<"\n\n Your minimum value is:"<<endl;
    while (infile>>number){
    if (number>max){
    cout<<" "<<number;
    }}
    cout<<"\n\n Your maximum value is:"<<endl;
    while (infile>>number){
    if (number<min){
    cout<<" "<<number;
    }}
    infile.close();
    return 0;
    } 


  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I thought you were supposed to only use one while loop, you've got several there.

    The reason you are only seeing the even numbers is that the first loop runs through until it reaches the end of the file at which point that "even" loop stops. The other loops all fail because you're already at the end of the file... there is no more data to be read so they don't show anything. You need to be using one loop with all your various testing of the number read done inside that single loop.

    You should also remove the first file read op you're doing before the first loop. That is basically throwing away the first number from the file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Ok so I was able to get some extra time with a TA and she helped me work out a algorithm to solve this and this is the final product for other viewers. We had to find the sum of odd and even numbers, not list them out. Also I don't know if this is a problem with the code or with the text file, but for some reason the program is stating that their is one extra odd number.
    Code:
    #include<iostream>
    #include<fstream>
    #include<cmath>
    usingnamespace std;
    int main()
    {
    ifstream infile;
    ofstream outfile;
    infile.open("Data_Values.txt");
    int number=-10;
    int sum_even=0;
    int sum_odd=0;
    int counter;
    int max=-10000;
    int min=10000;
    while (!infile.fail() && !infile.eof())
    {
    infile>>number;
    if (number>max)
    {
    max=number;
    }
    if (number<min)
    {
    min=number;
    }
    if (number%2==0)
    {
    counter=0;
    counter++;
    sum_even=sum_even+counter;
    }
    if (number%2!=0)
    {
    counter=0; 
    counter++;
    sum_odd=sum_odd+counter;
    }
    }
    cout<<"The number of even numbers is:"<<sum_even<<endl;
    cout<<"The number of odd numbers is:"<<sum_odd<<endl;
    cout<<"The maximum value is:"<<max<<endl;
    cout<<"The minimum value is:"<<min<<endl;
    infile.close();
    return 0
    }
    


  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This:
    Code:
    while (!infile.fail() && !infile.eof())
    {
       infile>>number;
    ... is wrong. You need to test the read operation for success and continue if things are OK. You should avoid using end-of-file tests to control your loops wherever possible. In your case, reading the last number from the file puts you at the end of the file but does not trigger the setting of the end-of-file flag. This means that at the top of your loop when you go back to test if you're at the end of the file (!infile.eof()), the test returns false - which gets turned into true due to the negation (!) - even though you are physically at the end of the file. You therefore attempt to read one more time (infile>>number) which then fails, it is only then that the end-of-file flag gets set. But, it's already too later since you're already inside the loop acting as if everything went fine. Since the read operation failed, number remains unchanged still holding whatever value it had during the last successful read operation and this messes up your counts since because you are then processing that last result twice.

    Like I said, you need to test the result of the read operation and only continue if that read is successful. You do that by replacing what you wrote above with this:
    Code:
    while(infile >> number)
    {
    If the read operation fails, the stream (infile) will go into an error state after which no more reads can be made from it. The extraction operation on that stream returns the stream object itself (infile >> number actually returns the stream infile). In the context of the above modified while loop, the returned stream can then be tested directly as either true or false.



    This:
    Code:
    if (number%2==0)
    {
    counter=0;
    counter++;
    sum_even=sum_even+counter;
    }
    if (number%2!=0)
    {
    counter=0; 
    counter++;
    sum_odd=sum_odd+counter;
    }
    Can be written better as:
    Code:
    if (number%2==0)
    {
        ++sum_even;
    }
    if (number%2!=0)
    {
        ++sum_odd;
    }
    And actually since a number is either even or odd, if it's one it is certainly not the other so we really only need to make one single test to decide which one it is:
    Code:
    if(number%2)
      ++sum_odd;
    else
      ++sum_even;
    We can shorten this further using a single line statement involving what's known as the ternary operator (?:) as such:
    Code:
    (number%2) ? ++sum_odd : ++sum_even;
    ...where ++sum_odd only gets executed if number%2 is true (number is odd) and ++sum_even only gets executed if number%2 is false (number is even).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubble sorting an array using NO loops.
    By Kristyy_mariee in forum C++ Programming
    Replies: 20
    Last Post: 03-28-2012, 01:23 PM
  2. Reading a text file and sorting a column of numbers
    By txmusic in forum C Programming
    Replies: 9
    Last Post: 02-26-2012, 09:49 PM
  3. Replies: 5
    Last Post: 01-10-2012, 01:14 AM
  4. Why does a sorting algoritm require nested loops ?
    By dantheman4 in forum Tech Board
    Replies: 2
    Last Post: 09-29-2011, 11:35 AM
  5. Sorting numbers in a file
    By pxleyes in forum C Programming
    Replies: 19
    Last Post: 04-15-2004, 06:17 AM