Hi, I'm trying to read from a file and execute a while statement to analyze the data, but I'm having some issues. The initial user input and display is working, but the rest of the code is not executing. What might I be missing here?
This is the code in its entirety. I'm trying to let the user name the integers file prior to executing the calculations.
////////////////////////////////////////////////////////////////////////////////////////////////////////
//CSC 134, Lab 3A
//Author: Kevin Miller
//Date: June 4, 2015
//This class reads data from a file and outputs calculations and information from the data.
////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
usingnamespace std;
int main()
{
//Variables
string inputfile;
int num = 0;
int even = 0;
int totaleven = 0;
int odd = 0;
int totalodd = 0;
int sumodd = 0;
int sumeven = 0;
int sumall = 0;
int smallest = 1000;
int largest = 0;
double average = 0.0;
int count = 0;
int value = 0;
ifstream inFile ("integers.txt");
cout << "Please enter the name of your file: " << endl;
cin >> inputfile;
cout << "This program processes the numbers in " << inputfile << "." << endl;
inFile.open(inputfile.c_str());
while (inFile.eof() == false)
{
inFile >> num;
count++;
if (num % 2 == 0)
{
num = even;
sumeven = sumeven + even;
totaleven++;
}
if (num % 2 != 0)
{
num = odd;
sumodd = sumodd + odd;
totalodd++;
}
sumall = sumeven + sumodd;
if (num > largest)
{
largest = value;
}
if (num < value)
{
smallest = value;
}
}
average = (largest + smallest) / 2;
//Printouts
cout << "The total amount of numbers is: " << count << endl;
cout << "The total amount of even numbers is: " << totaleven << endl;
cout << "The sum of the even numbers is " << sumeven << endl;
cout << "The total amount of odd numbers is: " << totalodd << endl;
cout << "The sum of the odd numbers is " << sumodd << endl;
cout << "The sum of all the numbers is" << sumall << endl;
cout << "The largest number is:" << largest << endl;
cout << "the smallest number is: " << smallest << endl;
cout << "The average of the largest and smallest numbers is: " << average << endl;
inFile.close();
system("pause");
return 0;
}
Please tidy your code up (by editing orig. post!) so it is correctly formatted.
Thanks, Andy
(I do hope you code has ended up "mashed" here due to problems cutting and pasting it, and that it doesn't look that bad in your editor or IDE. Note that some editors use a tab size of 4 whereas the code display here uses 8. For that reason I usually convert all tabs to spaces before pasting code here, so it looks the same I see it in Visual Studio or CodeLite.)
I edited it to include the entire code. I've been playing around with different setups, but nothing seems to execute the while statement to determine the calculations and display them. It just stops after the second cout statement.
////////////////////////////////////////////////////////////////////////////////////////////////////////
//CSC 134, Lab 3A
//Author: Kevin Miller
//Date: June 4, 2015
//This class reads data from a file and outputs calculations and information from the data.
////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<cstdlib> // for system() when building GCC
usingnamespace std;
int main()
{
//Variables
string inputfile;
int num = 0;
int even = 0;
int totaleven = 0;
int odd = 0;
int totalodd = 0;
int sumodd = 0;
int sumeven = 0;
int sumall = 0;
int smallest = 1000;
int largest = 0;
double average = 0.0;
int count = 0;
int value = 0;
//ifstream inFile ("integers.txt");
ifstream inFile; // don't open inFile twice
cout << "Please enter the name of your file: " << endl;
cin >> inputfile;
cout << "This program processes the numbers in " << inputfile << "." << endl;
inFile.open(inputfile.c_str());
// debug code
int max = 200;
while (inFile.eof() == false)
{
inFile >> num;
if(inFile.bad())
cout << "file is bad\n";
count++;
// debug code
cout << count << " : " << num << "\n";
if (num % 2 == 0)
{
num = even; // what does this line do?
sumeven = sumeven + even;
totaleven++;
}
if (num % 2 != 0) // do you know about 'else'?
{
num = odd; // what does this line do?
sumodd = sumodd + odd;
totalodd++;
}
sumall = sumeven + sumodd; // do you need to do this so often?
if (num > largest)
{
largest = value;
}
if (num < value) // ???
{
smallest = value;
}
// debug code
--max;
if(0 == max)
break;
}
average = (largest + smallest) / 2; // is this how to calc an average??
//Printouts
cout << "The total amount of numbers is: " << count << endl;
cout << "The total amount of even numbers is: " << totaleven << endl;
cout << "The sum of the even numbers is " << sumeven << endl;
cout << "The total amount of odd numbers is: " << totalodd << endl;
cout << "The sum of the odd numbers is " << sumodd << endl;
cout << "The sum of all the numbers is" << sumall << endl;
cout << "The largest number is:" << largest << endl;
cout << "the smallest number is: " << smallest << endl;
cout << "The average of the largest and smallest numbers is: " << average << endl;
inFile.close();
system("pause");
return 0;
}
#include <iostream>
#include <fstream>
usingnamespace std;
int main() {
ifstream inFile("integers.txt");
if(!inFile.is_open()) {
cout << "file failed to open\n";
return 1;
}
int num = 0;
while(inFile >> num) { // this checks for errors as well as eof
cout << num << "\n";
}
return 0;
}
Thank for the help. It still doesn't seem to do what I want it to. I've added notes about what the functions are supposed to do. Also, with the current code, it's not reading the file I have in the resources folder (integers.txt). It's actually compiling but it's printing all 0s and reading the default variables that I assigned...
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
usingnamespace std;
int main()
{
//Variables
string inputfile;
int num = 0;
int even = 0;
int totaleven = 0;
int odd = 0;
int totalodd = 0;
int sumodd = 0;
int sumeven = 0;
int sumall = 0;
int smallest = 1000;
int largest = 0;
double average = 0.0;
int count = 0;
int value = 0;
ifstream inFile;
cout << "Please enter the name of your file: " << endl;
cin >> inputfile;
cout << "This program processes the numbers in " << inputfile << "." << endl;
inFile.open(inputfile.c_str());
int max = 200;
while (inFile.eof() == false)
{
inFile >> num;
if (inFile.bad())
cout << "file is bad\n";
count++; //find the total number of integers in integer.txt
cout << count << " : " << num << "\n";
if (num % 2 == 0)
{
num = even; //determine which numbers are even
sumeven = sumeven + even; //find the sum of all the even numbers
totaleven++; //find the total number of even integers
}
if (num % 2 != 0)
{
num = odd; //determine which numbers are odd
sumodd = sumodd + odd; //find the sum of all the odd numbers
totalodd++; //find the total number of odd integers
}
sumall = sumeven + sumodd;
if (num > largest) //find the largest number
{
largest = value;
}
if (num < value) //find the smallest number
{
smallest = value;
}
--max;
if (0 == max)
break;
}
average = (largest + smallest) / 2; //This averages the largest and smallest number
//Printouts
cout << "The total amount of numbers is: " << count << endl;
cout << "The total amount of even numbers is: " << totaleven << endl;
cout << "The sum of the even numbers is " << sumeven << endl;
cout << "The total amount of odd numbers is: " << totalodd << endl;
cout << "The sum of the odd numbers is " << sumodd << endl;
cout << "The sum of all the numbers is" << sumall << endl;
cout << "The largest number is:" << largest << endl;
cout << "the smallest number is: " << smallest << endl;
cout << "The average of the largest and smallest numbers is: " << average << endl;
inFile.close();
system("pause");
return 0;
}
When I stopped inFile from opening twice the file reading worked fine for me. (Your file is either one number per line or at least space delimited?)
Is your program finding the right file??
And I flagged this line
num = odd;
as what it is actually doing is setting num to zero (I don't get what you mean by "determine which numbers are even")
Earlier on you set the variables even and odd to zero, and then set the value you've just read in -- num -- to their value (i.e. zero), so by the time you do you your summing and testing for smallest and largest num is always zero. So all your sums will be zero, your max will be zero, and your min 1000.
Also:
- the variable value looks totally spurious. On line 64 it should be smallest and on line 61 and 66 it should be num. I don't think value should exist at all.
- do you know how to calculate the mean? (You have the required data to do this.)
I am trying to calculate the mean but just for the largest and smallest numbers, so that's why I have largest + smallest / 2.
I got rid of the num = odd/even functions and left it at sumodd = sumodd + num after the if statements.
I think the only issue that's causing the program to fail to do its intended function is that it's not reading from the file. I'm getting all default numbers (200 lines of 0s). I'm using Visual Studio 2010, and I've tried putting integers.txt in the project folder in ever possible place under that fold as well as adding it as an existing item through Visual Studio. Any ideas why it's not reading it?