Monday, July 27, 2009

C++ program?

I am supposed to write a program that asks the user for a file name. The program should read the contents of the file and display the following data:


the lowest number, the highest, the total of the numbers, and the average of the numbers.


My program will compile but it never reads the numbers from the file. It just gives me random numbers. My teacher wants us to use functions for everything, but I don't get what to do in the function readNumbers. My teacher completely left us on our own to do this. He just walked out of class so can anyone please help me with this?

C++ program?
You've opened the file, but you haven't read from it. Look up ifstream in your book, or look in the documentation of the class. You need to read the contents of the file and put it into your integer variables.





The random numbers you are seeing are just garbage values for uninitialized integers in your array.





When you pass your array into ReadNumbers, you should pass it by reference. Otherwise even if you do populate the array with the correct numbers, it will only be in a copy of the array that ReadNumbers has, not the copy the caller has.
Reply:I am assuming that you have fopen() inside of readNumbers(), however that leaves your problem as this: You do not pass any parameters to readNumbers()!


No comments:

Post a Comment