Hi, I have this code written to analyze a list of 12 numbers. The numbers are stored in a file called numbersfinished.txt and the program is asking the user to input a file name, and if it matches that it should calculate the total numbers, average, highest number and lowest number and the array is supposed to match up with those numbers. No matter how hard I try, my program runs but it keeps outputting total gibberish numbers. Here is the code:
// Week 9 Labs
// Number Analysis Program
// Michael Orlando
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Variable Declarations
const int arraysize = 12; // defining array size
int numbers[arraysize]; // defining array
int count = 0; // loop counter variable
ifstream inputfile; // stream file object
string fileName; // variable for file user inputs
cout << "Please enter the file name: "; // asking user for the file name
cin >> fileName; // collecting file name
if (numbers[count] < lowest)
lowest = numbers[count];
else (numbers[count] > highest);
highest = numbers[count];
}
for (count = 0; count < arraysize; count++)
{
total += numbers[count];
}
average = total / arraysize;
cout << "The lowest number in the file is: " << lowest << endl;
cout << "The highest number in the file is: " << highest << endl;
cout << "The total of all numbers in the file is: " << total << endl;
cout << "The average of all numbers in the file is: " << average << endl;
}
You are trying to open a file named "fileName", as shown in inputfile.open("fileName");
Try removing the quotes around the word filename, and then the program will open the file inputted in the variable 'filename'
And regarding this line else (numbers[count] > highest);
That is not an if statement nor correct, One, you have a semicolon at the end of the line, signifying the end of the statement. Highest will then be whatever is in number[count], whether or not, it is in fact higher or lower. Remove the semicolon. Plus it should be elseif (numbers[count] > highest)
You could also insert total += numbers[count]; in the first for loop and get rid of the second for loop entirely.
Hi, thank you for your help... unfourtunatly, removing the quote from fileName when opening gives me an error :( which is worse than where i was at beforeC do you have any other thoughts?
// Week 9 Labs
// Number Analysis Program
// Michael Orlando
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
// Variable Declarations
constint arraysize = 12; // defining array size
int numbers[arraysize] = [0]; // defining array ( And initialize to 0 )
int count = 0; // loop counter variable
ifstream inputfile; // stream file object
string fileName; // variable for file user inputs
cout << "Please enter the file name: "; // asking user for the file name
cin >> fileName; // collecting file name
inputfile.open(fileName);
int highest;
int lowest;
float average = 0.0;
int total = 0;
while (inputfile >> numbers[count])
{
count++;
}
inputfile.close();
highest = numbers[0];
lowest = numbers [0];
for (count = 1; count < arraysize; count++)
{
total += numbers[count];
if (numbers[count] < lowest)
lowest = numbers[count];
if(numbers[count] > highest)
highest = numbers[count];
}
average = total / arraysize;
cout << "The lowest number in the file is: " << lowest << endl;
cout << "The highest number in the file is: " << highest << endl;
cout << "The total of all numbers in the file is: " << total << endl;
cout << "The average of all numbers in the file is: " << average << endl;
}
#include <iostream>
#include <fstream>
#include <string>
int main()
{
// Variable Declarations
constint arraysize { 12 }; // defining array size
int numbers[arraysize] { }; // defining array
int count { }; // loop counter variable
std::ifstream inputfile; // stream file object
std::string fileName; // variable for file user inputs
std::cout << "Please enter the file name: "; // asking user for the file name
std::cin >> fileName; // collecting file name
std::cout << '\n';
inputfile.open(fileName);
if (!inputfile.is_open()) // if the file was NOT opened
{
std::cout << "\n*** Unable to open file...aborting! ***\n";
return -1;
}
int highest;
int lowest;
double average;
int total = 0;
while (count < arraysize && inputfile >> numbers[count])
{
count++;
}
inputfile.close();
highest = numbers[0];
lowest = numbers[0];
for (count = 0; count < arraysize; count++)
{
if (numbers[count] < lowest)
lowest = numbers[count];
else (numbers[count] > highest);
highest = numbers[count];
}
for (count = 0; count < arraysize; count++)
{
total += numbers[count];
}
average = total / arraysize;
std::cout << "The lowest number in the file is: " << lowest << '\n';
std::cout << "The highest number in the file is: " << highest << '\n';
std::cout << "The total of all numbers in the file is: " << total << '\n';
std::cout << "The average of all numbers in the file is: " << average << '\n';
}
Please enter the file name: test
*** Unable to open file...aborting! ***
We don't know what your test file's contents are, so I created one with some sample numbers (named it "test.txt"):
1 2 3 30 4 5 6 20 7 8 9 10
Rerun the program, providing the correct file name:
Please enter the file name: test.txt
The lowest number in the file is: 1
The highest number in the file is: 10
The total of all numbers in the file is: 105
The average of all numbers in the file is: 8
How your are determining the highest number is borked, line 47 should be if (numbers[count] > highest).
Once fixed the output is:
Please enter the file name: test.txt
The lowest number in the file is: 1
The highest number in the file is: 30
The total of all numbers in the file is: 105
The average of all numbers in the file is: 8
Hi guys, thank you very much for your help and advice. Unfortunately, while I am not getting any error messages, I still keep getting random numbers that make no sense for my output. I am assuming this has something to do with my file that I am using but I typed it in exactly as it is called "numbersfinished.txt" and each and every time, even with the additions you guys gave, i remain getting gibberish output instead of the program reading from the actual list. numbersfinished.txt is already saved under the "others" folder and has it's own tab, so i am not sure why the program is not reading the numbers from it, only giving me a random output. Any suggestions?
So I did move my file into the same folder as my project,
But the issue I keep having is if I open the file as "fileName"
it runs but gives me BS numbers
If I open the file as fileName without the quotes I get this error:
C:\Users\Orlando\Documents\Lab1\NumberArrayProblemFinshed\main.cpp|24|error: no matching function for call to 'std::basic_ifstream<char>::open(std::__cxx11::string&)'|
C:\Program Files
So it actually does run with the text you said to try...
But again, I keep getting random numbers, it always says there are 0 numbers in the file and then the average and sum are negative random numbers with the letter e in and them and it's just so frustrating :(
Hi guys update: I finally got my code to work! Not 100% sure what I did wrong before but starting a new project and inputting a new file got things running smoothly! Thank you againf or all your help and advice, much appreciated!