d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ File Help
Add Reply New Topic New Poll
Member
Posts: 1,129
Joined: May 25 2013
Gold: 0.11
Oct 11 2013 04:26pm
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 is my code so far:

#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);


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;
}


How does the eof function look? Does the counter go in the eof function? How do I calculate the total sum?
Member
Posts: 976
Joined: Apr 5 2010
Gold: 1,080.50
Oct 14 2013 01:16am
Code
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
ifstream inputFile; //inputFile object to be attached

string userFile; //File to be opened is stored in this string

int totalValue = 0, //Accumulator for the values read
evenCount = 0, //Used to count amount of even numbers
oddCount = 0, //Used to count amount of odd numbers
dataRead; //Stores the value read of the opened file

//Prompts for file name to open
cout << "Enter the file name you wish to open: ";
cin >> userFile;

//Attempts to open file
inputFile.open(userFile.c_str());

//Will run if the file has opened
if(inputFile)
{
//Will run if data has been read
while(inputFile >> dataRead)
{
//Test if it is even, if so add a count to the evenCount
if(dataRead % 2 == 0)
{
evenCount++;
}

//If it isn't even, then add a count to the oddCount
else
{
oddCount++;
}

//Displays the current value read
cout << "Value read was: " << dataRead << endl;

//Accumulates the values read
totalValue += dataRead;
}

//Displays the following values
cout << "Total Value: " << totalValue << endl
<< "Amount of Odd: " << oddCount << endl
<< "Amount of Even: " << evenCount << endl
<< "Program Complete. " << endl;
}

//Error message if the file was unable to open
else
{
cout << "Program Failed. " << endl;
}


return 0;
}


Use code braces when posting your code! :)

If you need help post here or send me a PM.

Book to read: C++ Early Objects
Last years PDF: http://eembdersler.files.***/2011/07/c_early.pdf
I recommend buying the most recent version hard copy. It comes with answers to checkpoints and what not.

This post was edited by cssa on Oct 14 2013 01:18am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll