What is wrong with this while loop?

This is a discussion on What is wrong with this while loop? within the C++ Programming forums, part of the General Programming Boards category; Hey guys. I am new programmer and can't figure out this while loop. I am getting input from a .dat ...

  1. #1
    Unregistered
    Guest

    Lightbulb What is wrong with this while loop?

    Hey guys. I am new programmer and can't figure out this while loop. I am getting input from a .dat file that looks like this:

    Bryan 100000.75
    Elise 826
    James 12000.50
    Johnny 900000.75
    Roger 77000.50
    Belle 59576
    Bill 10.00
    Larry 96000.25
    Eric 100

    The first row is names, the second row is salaries. My program needs to use a loop to gather the info from this data file and determine who has the max salary, who has the min salary and what is the average of all the salaries in the data file. Here is my loop:

    in_data.open("emps.dat");

    decimals(cout,2);
    while(!in_data.eof())
    {
    in_data >> temp;

    for(i = i; i < 2; i++)
    {
    max.set_salary(temp.salary());
    max.set_name(temp.name());
    min.set_salary(temp.salary());
    min.set_name(temp.name());
    }
    if(temp.salary() > max.salary())
    {
    max.set_salary(temp.salary());
    max.set_name(temp.name());
    total_sal = total_sal + max.salary();
    counter++;
    }
    else if (temp.salary() < min.salary())
    {
    min.set_salary(temp.salary());
    min.set_name(temp.name());
    total_sal = total_sal + min.salary();
    counter++;
    }
    else
    {
    total_sal = total_sal + temp.salary();
    counter++;
    }
    }

    So my problem is, it keeps adding the last member in the data file twice to total_sal. What can I do to stop this from happening?
    So in this data files case, the total would be $100 too much.
    (PS the program must be able to handle any data file of any size.)

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    559

    Re: What is wrong with this while loop?

    Originally posted by Unregistered

    for(i = i; i < 2; i++)
    Real quick - the for statement is wrong. Also, use code tags, it helps in reading your code.

  3. #3
    Unregistered
    Guest
    Sorry, let me type that code so it is readable.

    [code]
    in_data.open("emps.dat");

    decimals(cout,2);
    while(!in_data.eof())
    {
    in_data >> temp;

    for(i = i; i < 2; i++)
    {
    max.set_salary(temp.salary());
    max.set_name(temp.name());
    min.set_salary(temp.salary());
    min.set_name(temp.name());
    }
    if(temp.salary() > max.salary())
    {
    max.set_salary(temp.salary());
    max.set_name(temp.name());
    total_sal = total_sal + max.salary();
    counter++;
    }
    else if (temp.salary() < min.salary())
    {
    min.set_salary(temp.salary());
    min.set_name(temp.name());
    total_sal = total_sal + min.salary();
    counter++;
    }
    else
    {
    total_sal = total_sal + temp.salary();
    counter++;
    }
    }

  4. #4
    Unregistered
    Guest
    Argggg. Stupid tags.... I need to register so I can edit my own posts, this is annoying. Sorry everyone!

    Code:
    in_data.open("emps.dat"); 
    
    decimals(cout,2); 
    while(!in_data.eof()) 
    { 
            in_data >> temp; 
    
            for(i = i; i < 2; i++) 
            { 
                    max.set_salary(temp.salary()); 
                    max.set_name(temp.name()); 
                    min.set_salary(temp.salary()); 
                    min.set_name(temp.name()); 
            } 
            if(temp.salary() > max.salary()) 
            { 
                    max.set_salary(temp.salary()); 
                    max.set_name(temp.name()); 
                    total_sal = total_sal + max.salary(); 
                    counter++; 
            } 
            else if (temp.salary() < min.salary()) 
            { 
                    min.set_salary(temp.salary()); 
                    min.set_name(temp.name()); 
                    total_sal = total_sal + min.salary(); 
                    counter++; 
            } 
            else 
            { 
                    total_sal = total_sal + temp.salary(); 
                    counter++; 
            } 
    }

    Also, about the for loop. Above in my program I declare i = 1. I found typing the for loop like that was the only way to make it run only 1 time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 03:45 AM
  2. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  4. What is wrong with my while loop?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-19-2002, 01:07 PM
  5. Whats wrong w/ my loop?? Please help?
    By aspand in forum C Programming
    Replies: 6
    Last Post: 05-30-2002, 04:42 AM

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