473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

inputting array from file

12
Have a .txt document as:

String
int
int
int
int
int
int
int
int
int
int
String
int
int
...

for 15 strings. Looking to input the names into a string array then the grades into a 2d array Then output with some other calculations. Here is what i have so far:

#include <conio.h>
#include <iostream> // required to perform c++ stream I/O
#include <fstream> // required
#include <cstdlib>
#include <cmath> // required to perfrom math functions in C++ library
#include <iomanip> // required for parameterized stream manipulators
#include <string> // required to access string functions

using namespace std; // for accessing c++ standard library members

// define constants
const int TESTS = 10; // number of tests per student
const int STUDENTS = 15; // number of students


// function main begins program execution
int main()
{
// define two dementional array to store student grades
int data[STUDENTS][TESTS] = {0};
string names[STUDENTS] = {0};

ifstream inputFile;
inputFile.open("GradeSpring06.txt"); // open file to read from
if (!inputFile)
{

cout << "Error opening file!" << endl;
exit( 1 );
}





for (int student=0; student< STUDENTS ; student++)
{
inputFile >> names[student];
for (int test=0; test<TESTS;test++)
{

inputFile >> data[student][test]; // set file


}

}


cout <<fixed <<setprecision(1);

double testTotal = 0.0;
double classTotal = 0.0;
double squared = 0.0;
double var = 0.0;
double sdev = 0.0;
int mxm = data[0][0];
int min = data[0][0];

// display a header
cout << "\nGrade summary" << endl;
cout << "--------------" << endl;
cout << left << setw(7) << "Student"
<< right << setw(5) << right << "Ex1"
<< setw(7) << "Ex2"
<<setw(6) << "Ex3"
<<setw(6) << "Qz1"
<<setw(5) << "Qz2"
<<setw(6) << "Qz3"
<<setw(6) << "Qz4"
<<setw(6) << "Qz5"
<<setw(6) << "Pr1"
<<setw(6) << "Pr2"
<<setw(8) <<"Averg" <<endl;

for ( int student = 0; student < STUDENTS; student ++)
{
cout << left <<setw(7) << student + 1;

for ( int test = 0; test < TESTS; test++)
{
// display student grades
int grade = data[student][test];
cout << right <<setw(5) << grade <<" ";


classTotal += grade;
}
// find average of students grades
double average =(((data[student][0]+data[student][1]+data[student][2])/3)*0.4)+(((data[student][8]+data[student][9])/2)*0.3)+(((data[student][3]+data[student][4]+data[student][5]+data[student][6]+data[student][7])/5)*0.3);
cout << right << setw(7) << average << endl;


}

cout << "\n" << left << setw(7) << "Test Avg";

for (int test = 0; test < TESTS; test++)
{
for (int student = 0; student < STUDENTS; student++)
{
testTotal += data[student][test];
}

cout << right << setw(6) << testTotal / STUDENTS;

testTotal = 0.0;
}

cout << endl;

cout << left << setw(7) << "Stan dev";

for (int test = 0; test < TESTS; test++)
{
for (int student = 0; student < STUDENTS; student++)
{
squared += pow(data[student][test], 2.0);
var = (squared - ((testTotal * testTotal) / 15) ) / 15;
sdev = sqrt(var);
}

cout << right << setw(6) << sdev;

sdev = 0.0;

}

cout << endl;

cout << left << setw(7) << "Max";

for (int test = 0; test < TESTS; test++)
{
for (int student = 0; student < STUDENTS; student++)
{

if (data[student][test] > mxm)
{

mxm = data[student][test];
}
}

cout << right <<setw(6) << mxm;

mxm = 0;
}

cout <<endl;

cout << left << setw(7) << "Min";

for (int test = 0; test < TESTS; test++)
{
for (int student = 0; student < STUDENTS; student++)
{

if (data[student][test] < min)
{

min = data[student][test];
}
}

cout << right <<setw(6) << min;

min = 100;
}

cout <<endl;

double classAverage = classTotal / (STUDENTS * TESTS);
cout << "\nClass average: " <<classAverage <<endl <<endl;
getch();

return 0; // indicates successful termination
} // end function main
Nov 30 '06 #1
1 2530
Ramper
12
help plz? when i try to compile and run it exits.
Nov 30 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: ASiF | last post by:
Hi i am completely novice at programming, would appreciate some help. The program needs to be able to take a set of up to a 100 numbers (between 1 to 9) "inputted" at the prompt and then assign...
8
by: Kruton | last post by:
What is the easiest way to take a string from a console while including whitespaces? This program's user will input a line of text that will consist of multiple tokens, but the number tokens and...
3
by: jerryalan | last post by:
I'm doing an assignment for class where I need to create a gradebook. I have it working well but I'm having a hard time getting student names inputted with spaces. I am using a pointer to create...
1
by: C G | last post by:
Dear All, I have a text file with data like: 1 2 3 4 5 6 7 8 9 i.e. so I have three columns of numbers. I wish to put this data into a table. However, I do not want it to take up three...
2
by: saltedcoffee | last post by:
hey, I am beginer to C++ programing. I am working on a project called the "maze Problem" First of all I am required to read a text file in( which is the maze) and store it into a 2 dimentional...
5
by: Chris | last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL. Meeting info and Meeting docs reside on 2 related tables in the db. Users may want to upload anywhere from 1 to 10 or...
2
by: UofFprogrammer | last post by:
Hello, Several Weeks ago I asked a question about testing for the end of an input file. I have been using this method pretty well for inputting information from an external file. I am using C++. ...
5
by: tim123 | last post by:
Hey. I'm having some issues reading in data from a tab delimited text file. I only want to input numbers, and ideally I'd like it to be able to cope with a line or 2 of text at the top in case...
1
by: shadowofanubis66 | last post by:
Basically, the project I'm working on this week is to make a school lunch menu with a GUI, when the person types in a day, the menu for that day shows up. We have two files, ones a day off text file...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.