Guys, here is the program I am attempting to write:
Write a function that returns how many numbers (doubles) in the file filename.txt are below a given number named level. The value of the variable level and the name of the file should be passed using the function's parameters. The function should do everything necessary to create an input stream for the file and should close the file before returning. (Don't forget to use the .data() function to convert the filename to a C-string, as we did in the lab.) For example, if a program needed to know how many numbers in the file Hwk5.txt were below 60, then it would call the function with the values 60. and "Hwk5.txt".
Before trying to complete the assignment, test your function by writing a simple main function that does nothing but call the function and display how many numbers are below, say 60.
Now complete the assignment by writing a main function that obtains the filename from the user and then displays how many of the numbers are in the following ranges:
< 60
In the 60's
In the 70's
In the 80's
90 through 100
Hint: Use your function to determine how many are less than 60. This is the first answer. Then use your function to determine how many are less than 70. Your second answer will be this number minus the first answer.
Use a protype for your function, and make sure you document both the main program and the function. As always, your output should be well-labeled and neat in appearance.
-So I understand the idea of what I'm trying to do...that is not a problem. My issue is with my text book and professor- neither explain things in a way I can understand.
I know to start i must prompt the user for the file they want to read, i then must input all of that data into an input stream (check the values for being 0-100) then close the file, from there I will run my function to determine how many numbers fall in what category given info from the stream. I will then output the function's findings.
My problem...is the basics. I've been reading on how to stream data from a .txt file, but nothing I have found helps me setup my scenario. Help anyone? I don't want anyone doing this 'for me'...but could sure as hell use some better explanation from an actual programmer.
I'm assuming your instructor is providing you the files with numbers which are separated either by newlines or whitespace..
first you need to include to correct header for file io
#include <fstream>
next you can make the prototype should look something like this
int howMany(char * fName, double level);
notice this function will return and int (containing how many numbers were less than the level in the file)
char * fName will be the filename as a c style string (meaning array of characters terminated by a null character)
double level will be the level that he gives (ie 60)
so now you can define the function... should look something like this..
int howMany(char * fName, double level)
{
double num;
int count = 0;
std::ifstream file; // this initializes the filestream
file.open(fName); // this opens the file with the name passed in
while (file >> num) // this will read the input into num seperating out newlines
{ // and whitespace over and over until the end of file is reached
if (num < level) //for each number check if its less than the level
count++; // if it is increase the count
}
file.close();
return count;
}
Got ya, that makes sense. So to find the other values I will repeat the func for val. 70, then 80, 90, and 100. For 70 I will take the count from the 60 version and subtract- thus if the 60func yields say 5, and the 70func yields 8- my answer for 70 would be 8-5= 3 for the range 61-70?
if so, how will i handle mult. counts? do I need to assign my first count val (for the 60 func) to some val. before my function runs the 70 version? and so on?
say: int 60Count = count
then run 70func version and 70count = count
then run 80func version and 80count = count
?
Sorry for being so un-knowledgable, but the only experience I have is one semester with visual basic, and now half a semester with C++
oh- and thanks for the previous info. For some reason picking up C++ is a bitch compared to VB for me...at leaset given my course materials and instructors style of teaching
here is a basic way of doing output/input for your prog
#include <iostream>
int main()
{
char str[50]; // new c stlye string
std::cout << "Please Enter the filename\n>>>";
std::cin >> str; //get filename
std::cout << howMany(str, 60) << std::endl; //print the first count for level = 60
for (int start = 7; start < 11; start++) // now print all the ones following that (60-70,70-80 etc)
{
std::cout << ( howMany(str, start*10) - howMany(str, (start-1)*10) ) << std::endl;
}
}