You are here:

C++/C++ help my program wont run


Thank you for taking the time to look at my code.
my code is suposed to
1. Use sentinel, nested and EOF loops to do file I/O.
2. Create a program file to read a data file and find the section number(first four digits) in a sequence of data(each line).
3. Then find the maximum, minimum and average of each section.
4. Then store these results in an output file.
5. create an outter loop that uses an end of file loop to find the average of each section. an store it in the same file.
this is the input data file
0372 36 67 87 85 65 77 89 -1
0481 89 92 67 -1
0754 76 91 83 45 -1
0892 44 79 97 58 -1
0938 91 67 87 86 95 80 -1
this is the code I have written
there are no compiling errors but when I go to run the program nothing happens, I cannot figure out where my syntax is messed up. will you help me?
int main()
string Section;
ifstream infile; // input file
ofstream outfile; // output file
int count; // number of vlaues on the line
float sum; // sum of numbers on line
int number; // number on line
float average; // Average of line
int currvalue; //Current data value
int minvalue; //Minimum data value
int maxvalue; //Max data value
float sumo; //Sum of averages
float averageo; //average of averages
float numbero; //single average to total
int counto; //number of averages
string section; // Section number
bool positive;"scores.txt");

if (!infile) //Test the file
{cout<<"Cannot open inputfile."; //Failure
system ("pause");
return 1;

if (!outfile) //Test the file
cout<<"Cannot open outputfile."; //Failure
system ("pause");
return 1;

cin>>section; //Accuire section
cout<<section; //Display section

while (currvalue > 0)
counto = 0;
sumo = 0;
if (currvalue > maxvalue)
maxvalue = currvalue;
else if (currvalue < minvalue)
minvalue = currvalue;
currvalue = 0;
return 0;
while (number > 0)

count = 0; //Quantity of values
sum = 0; //Total of values
positive = true; //Loop control flag
while (positive) //Stop for end value
if (number < 0)
positive = false;
else sum = sum + number;
count++; //increase count
average = sum / count;
return 0;
sumo = sumo + numbero;
averageo = sumo / counto;
cout << "Average for all sections " << averageo << endl;

Ok Lee, some points:

1. Before


you need to set currval to something.  If it's 0 (not likely but it could be) the while won't run.  Your compiler should have warned about using a variable before it was defined.

2.  After


you have return 0; which ends the program.  I think you want to get out of the loop, so use a break; instead.

3.  while(number>0) - same problem - number isn't assigned.

4.  else sum = sum+number; needs a compound statement:

else {
 sum = sum+number;

otherwise you'll count the -1.  Note that sum += number; is easier to type.

I can't run the program due to tool compatibility issues, but this is what I see anyway.

Hope it helps,


All Answers

Answers by Expert:

Ask Experts


Bill A


I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.


I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

BS Computer Engineering

©2016 All rights reserved.