:o When I start the program it just spits out a continuous line of numbers; Can anyone give me any suggestions, OR better yet, just tell me how so I can start my Thanksgiving break early? :o

/* Program Description: The program will read a file named Scores.dat, and
will show the scores of a student and their average
Author: Eric Martin
Date: 24 November 2004 */

#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
using namespace std;

int main () {
// Declare
    ifstream InFile;
	int NumScores;
	int Weights [5];
	int Scores;
	int TotalScore;
	int StudentName;
	float AverageScore;
// Initialize
	InFile.open("c:/Scores.dat");
// Solve
	// Get Number of score
		InFile >> NumScores;
	// Get wight of scores
		for (int I = 0; I < NumScores; I++){
			InFile >> Weights [I];}
	// Get student name and scores
		while (!InFile.eof()){ InFile >> StudentName;
		TotalScore = 0;
		for (I = 0; I << NumScores; I++) {InFile >> Scores;
	// Apply weights and compute final scores
		TotalScore = TotalScore + Scores * Weights [I];}
	// Figure out letter grade
		AverageScore = (float) TotalScore / 100;
		cout << StudentName << ' ' << AverageScore << endl;}
	// Wrapup and close
		InFile.close();
		cout << endl;
		getch();
		return 0;
}

Dani AI

Generated

The continuous numbers come from the program reading the file incorrectly and printing partial/garbage records repeatedly. The likely causes are: the file never checked after open, the weights array not sized to the actual count, a typo in loop conditions (using bitshift instead of comparison), reading a student name into an integer variable, and using eof() or broken extraction checks. needs to validate every extraction and use proper types for names and dynamic storage for weights.

Follow this checklist (ties to and suggestions):

  • Verify the file opened successfully; abort with a clear error if not.
  • Read and validate the header (number of scores). Reject nonpositive or absurd values.
  • Allocate a container for weights sized to that header, read each weight and build a sum-of-weights.
  • For each student record read the name (string) and then exactly N scores; check each extraction and handle malformed lines.
  • Avoid while(!file.eof()); prefer while(file >> token) or getline+istringstream when names include spaces.
  • Remove nonstandard calls like getch()/conio.h; rely on standard I/O and compiler warnings (-Wall/-Wextra).

Example pattern to follow (robust I/O and checks):

std::ifstream in("Scores.dat");
if (!in) { std::cerr << "Cannot open Scores.dat\n"; return 1; }

int numScores;
if (!(in >> numScores) || numScores <= 0) { std::cerr << "Bad header\n"; return 1; }

std::vector<int> weights(numScores);
int sumWeights = 0;
for (int i = 0; i < numScores; ++i) {
    if (!(in >> weights[i])) { std::cerr << "Missing weight\n"; return 1; }
    sumWeights += weights[i];
}

std::string name;
while (in >> name) {
    int total = 0;
    for (int i = 0; i < numScores; ++i) {
        int score;
        if (!(in >> score)) { std::cerr << "Malformed line for " << name << '\n'; break; }
        total += score * weights[i];
    }
    float avg = static_cast<float>(total) / (sumWeights ? sumWeights : 100);
    std::cout << name << ' ' << avg << '\n';
}

Quick debugging tips: print read header and weights to confirm parsing, try a minimal test file, and enable compiler warnings. Also consider renaming the thread title to something descriptive next time — was right that it helps others find and prioritize the question.

Recommended Answers

All 4 Replies

I'm not real sure where to indent.. How does this look?

/* Program Description: The program will read a file named Scores.dat, and
will show the scores of a student and their average
Author: Eric Martin
Date: 24 November 2004 */


#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
using namespace std;


int main () {
// Declare
ifstream InFile;
int NumScores;
int Weights [5];
int Scores;
int TotalScore;
int StudentName;
float AverageScore;


// Initialize
InFile.open("c:/Scores.dat");


// Solve
// Get Number of score
InFile >> NumScores;
// Get wight of scores
for (int I = 0; I < NumScores; I++){
InFile >> Weights ;}
// Get student name and scores
while (InFile >> StudentName){
TotalScore = 0;
for (I = 0; I << NumScores; I++) {InFile >> Scores;
// Apply weights and compute final scores
TotalScore = TotalScore + Scores * Weights ;}
// Figure out Average
AverageScore = (float) TotalScore / 100;
cout << StudentName << ' ' << AverageScore << endl;}


// Wrapup and close
InFile.close();
cout << endl;
getch();
return 0;
}

I << NumScores?????

And its better if u let us know how the data in the file are arranged.

it's also better to use a descriptive topic and not demand instant attention as that's extremely rude.

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.