File I/O and While Statement
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: File I/O and While Statement

Hybrid View

  1. #1
    Join Date
    Oct 2013
    Posts
    2

    File I/O and While Statement

    I have an assignment due at 8:00PM. I've had a ton of work to do this week and haven't really got to learn the topic before the assignment. I'm just trying to get the points for the assignment and learn the stuff after its due.

    Problem:

    You are to create a program that will read 50 integers from a file and sum them together. Output this sum to an output file. You will also output (to the file) how many numbers were odd, and how many were even (zeros may be considered even). Your program MUST employ a while loop to read the values from the file.

    The while loop should check for TWO things (what are they?). Hint: you know you need to read 50 numbers, but for correct file processing, what else should you check for? Your grade will be based on whether or not you correctly identify both conditions to be used in the while loop.

    here's my code so far:


    Code:
    #include <iostream> // Preprocessor directive for cin and cout
    #include <fstream>
    using namespace std;
    
    int main ()
    {
    // Declaring variables
    ifstream num_list;
    int number(0), even(0), odd(0), count(0);
    int sum, num;
    
    num_list.open("Lab_7_Input_files.txt");
    
    if (num_list.fail())
    {
    cout << "Error opening file\n";
    }
    
    
    while ( !num_list.eof())
    {
    count=0;
    while (count <= 50)
    {
    
    sum += num_list;
    }
    }
    
    
    
    num_list >> number;
    while (number >= 0 && number <= 50)
    {
    if (num % 2 == 0)
    {
    even = even + number;
    }
    else
    {
    odd = odd + number;
    }
    }
    
    cout << "The file contains " << even << "even numbers.\n";
    cout << "The file contains " << odd << "odd numbers.\n";
    
    return 0;
    }

    I am getting error: no match for 'operator+=' in 'sum += num_list'

    I have been trying for hours, but can't seem to get it just right. I hate to ask this, but if someone could fix this and post it that would be great, as I really need the points.

    Thanks in advance if anyone tries to help!

  2. #2
    Join Date
    Oct 2013
    Posts
    2

    Re: File I/O and While Statement

    I replaced my while loop with:

    while ( !num_list.eof())
    {
    count=0;
    while (count <= 50)
    {
    num_list >> number;
    sum += number;
    }
    count++;
    }

    Now the program runs now, however, nothing is being displayed (empty black box).

    Anyone know what's going on?

  3. #3
    Join Date
    Apr 1999
    Posts
    27,418

    Re: File I/O and While Statement

    Quote Originally Posted by thepox View Post
    Now the program runs now, however, nothing is being displayed (empty black box).

    Anyone know what's going on?
    In the code you posted, where do you output anything (except the initial prompt)?

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,028

    Re: File I/O and While Statement

    I've had a ton of work to do this week and haven't really got to learn the topic before the assignment. I'm just trying to get the points for the assignment and learn the stuff after its due.
    That's not how it works! The assignment is to test your understanding of the topic. Just because you have other things to do doesn't negate your requirement to complete the assignment. We all had a lot of stuff to do when we were studying and managed to get through it.

    However, as I'm feeling in a particularly generous mood today, have a look at the code below.

    Code:
    #include <iostream> // Preprocessor directive for cin and cout
    #include <fstream>
    using namespace std;
    
    int main ()
    {
    // Declaring variables
    ifstream num_list("Lab_7_Input_files.txt");
    
    int odd(0), count(0), sum(0);
    int num;
    
    	if (!num_list.is_open())
    	{
    		cout << "Error opening file\n";
    		return 1;
    	}
    
    	while ((num_list >> num) && count < 50)
    	{
    		count++;
    		odd += num % 2;
    		sum += num;
    	}
    
    	cout << count << " numbers were read from the file." << endl;
    	cout << "Their sum is " << sum << "." << endl;
    	cout << "There are " << count - odd << " even numbers." << endl;
    	cout << "There are " << odd << " odd numbers." << endl;
    
    	return 0;
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center