473,748 Members | 8,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inputting array from file

12 New Member
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: " <<classAverag e <<endl <<endl;
getch();

return 0; // indicates successful termination
} // end function main
Nov 30 '06 #1
1 2549
Ramper
12 New Member
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
2167
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 those number indvidually to seperate variable and then pass on to another function which will perform other operations on the numbers. char line
8
4029
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 the length of each line will vary at each use. Is there a way to get scanf to take a string that contains whitespace? Or is there another function that would work? -Thank you
3
1315
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 a new object (to dynamically size the student array to the size specified by the teacher). Here is my code: string * ps = new string; for(int i = 0; i < students; i++)
1
1271
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 rows, rather only a single row and a single column. I was think
2
2384
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 vector. This is what I have till now: #include <iostream> #include "maze.h" #include <vector> #include <fstream>
5
2727
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 more documents to share/use during a meeting presentation. What would be the most efficient way to approach this? This is the logic I'm currently considering: Page 1: Meeting Information input with link to a document upload page (this page...
2
1728
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++. char line; while(file>>line){ Where line is a c-string and file is an input stream. But this only reads until the first space it encounters. I now want to try to take in an entire line, regardless of what it contains (such as whitespace) as a...
5
1664
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 someone leaves the file headings on when exporting the data from the spreadsheet. My code is dim data(,) as double dim lines, i as integer dim s as string fileopen(1,"c:\mydatafile.txt", openmode.input)
1
2224
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 which just has the days off in it, and the other is a lunch menu file, which each line being an entry. Each day inside that lunch menu file ends with the word Milk. I've been trying to get the days off from the file and store it into an array of int,...
0
8987
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.