I have to write a program that does this
Write a complete C++ program to solve the following problem.
PROGRAM DESCRIPTION: An unknown number of integers, but no
more than 25, are to be read from the file exam.dat.
(Yes, this means you have to open that file.) Calculate
and output their sum. Next output, one entry per line, each
number and its percentage of the sum. Line these up in columns.
The first column should be 10 characters wide; the second column
should be 7 characters wide, not including the percent sign.
The percentages should be rounded to the nearest tenth. See
sample output below.

PROGRAM INTERNALS: Since the program is so short, you do not
need to define any functions other than main(). You may
assume the input data is error-free and need not be checked for
correctness. The input data file is to be read only once; you
are NOT allowed to open the input file a second time NOR in some
way rewind the input file.

SAMPLE OUTPUT:
Sum Equals: 200

45 22.5%
5 2.5%
30 15.0%
20 10.0%
100 50.0%
and I just need help with one thing and I think i can figure the rest out . . . when I run my program the second for loop won't work (that's why there's a stupid cout to see if it even works) and I don't understand or have a clue why. I would appreciate any help

#include <iostream>
#include <fstream>
using namespace std;
const int size = 25;
int main ()
{
	ifstream inFile;
	float numbers[size];
	float percent[size];
	float sum =0;
	int counter;
	inFile.open("exam.dat");
	for (counter =0; numbers[counter] <=size; ++counter)
	{	
		inFile >> numbers[counter];
		cout << numbers[counter] << endl;
		sum += numbers[counter];
	}
	for (counter=0; numbers[counter] <=size; ++counter)
	{	
		inFile >> numbers[counter];
		percent[counter] = (numbers[counter] / sum) * 100;
		cout << percent[counter] << endl;
		cout << "bob"<< endl;
	}
	cout << sum << endl;
	return 0;
	
}

Did you somehow fail to read all of the information posted all over this site about CODE tags, like
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used, even in responses to your posts
6) even on the background of the box you actually typed your message in

The problem says you will read up to 25 values, possibly fewer. A for loop to control the reading is not a good choice. Use a while loop that stops when you run out of data.

Your second loop can be a for loop, assuming you kept count of how many numbers were actually read in. You don't need to be reading from the file - you stored the data in your array! In fact, since you read through the file, your attempts at further reading are in fact getting nothing. numbers[counter] <=size; is not a condition you want to use in any case. Don't stop when the value in the array is larger than size, stop when counter itself is. Your second loop is not executing because you probably have a value larger than 25 in the first element of the array.

Ok, first off, code tags are helpful. Most people won't look at your post if it's not formatted properly. Secondly, I'm not sure what you're trying to do with your code, but it's not going to create output like was in the example.

Your for loops are odd. for (counter = 0; numbers[counter] <=size; ++counter) means set counter to zero; if whatever is in array number at position counter is less then or equal to size; increment and do whatever is in the loop. It's not just your second loop that has the issue. Both your for loops only run once.

Now, say your loops did run correctly. Your program would output all the numbers found within the file on their own line, then it would output all the percentages on their own line, and then it outputs the sum. If you used the same input as in the example, this is what your program would output.

45
5
30
20
100
22.5
2.5
15
10
50
200

My suggestion, use a while to retrieve the numbers from inFile and store them within the array numbers using counter to store the quantity of numbers it read from the file. When it dies, output sum. Then have a for loop with a variable that starts at zero and goes to counter. Have the loop output a number from the array and then output it's percentage. That should be it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.