0

Does this loop look at all familiar?? (create a names 2D array, change the rows from 4 to 7)

for (int row =0; row<4; row++){
    for(int col=0; col<2; col++)
    fin>>names[row][col];
    for(int col=0; col<6; col++)
    fin>>scores[row][col];
    }// end of outer for

Also you had two ifstreams declared when you meant to have 1 ifstream and 1 ofstream. Make those changes, and add names[row][0] and names[row][1] to your cout, add a matching fout statement and you are good to go.

0

Does this loop look at all familiar?? (create a names 2D array, change the rows from 4 to 7)

for (int row =0; row<4; row++){
    for(int col=0; col<2; col++)
    fin>>names[row][col];
    for(int col=0; col<6; col++)
    fin>>scores[row][col];
    }// end of outer for

Also you had two ifstreams declared when you meant to have 1 ifstream and 1 ofstream. Make those changes, and add names[row][0] and names[row][1] to your cout, add a matching fout statement and you are good to go.

Jon, I tried that earlier but I must be doing something wrong because the results are coming all jumbled up. See if you can spot where the issue is please?

string names[7][2]; 

    for(int row =0; row<7; row++){
    for(int col=0; col<2; col++)
    fin>>names[row][col];
    }// end of outer for

cout<<fixed<<showpoint<<setprecision(2);
    for(int row=0;row<7;row++)
    cout<<setw(20)<<names[row][0]<<names[row][1]<<setw(25)<<rowtotals[row]<<setw(10)<<progavg[row]<<setw(10)<<testavg[row]<<setw(10)<<courseavg[row]<<endl;
0

Don't just read the names, read the names and the data all at once. Take the loop from the prior post and make it rows to 7. It works.

First0 Last0 54 34 65 34 43 23
First1 Last1 76 54 34 54 23 87
First2 Last2 87 67 45 65 87 67
First3 Last3 45 76 87 65 45 65
First4 Last4 34 65 76 87 56 87
First5 Last5 76 56 78 98 78 67
First6 Last6 87 67 76 87 78 67

was the file I made to test it

1234567890123456789012345678901234567890123456789012345678901234567890
----------------------------------------------------------------------
Student Name        Total     Program   Test      Course    Grade
                    Points    Average   Average   Average
----------------------------------------------------------------------
First0 Last0       253     51.00     33.33     42.17
First1 Last1       328     54.67     54.67     54.67
First2 Last2       418     66.33     73.00     69.67
First3 Last3       383     69.33     58.33     63.83
First4 Last4       405     58.33     76.67     67.50
First5 Last5       453     70.00     81.00     75.50
First6 Last6       462     76.67     77.33     77.00

output is off by a couple of columns but I didn't put a setw in for name

Edited by jonsca: n/a

0

I have been working all day to get result like that!! lol
So I tried putting names and scores under the same loop.. but now even the order of arrangement for numbers are screwed up.. grr
I don't mean to act lazy.. but if you can please correct it for me. My brain is getting thicker by minute working on this all day :(

0

nevermind actually I kinda got it. Now I need to work on letter grade and such. Thanks. I'll let you know if I get stuck (again) :)

0

It's about time! I got everything figured out. Now just minor tweaks needed. If I were to add entry on the beginning asking user to press any key with cin.get(); How do I make it to clear the screen, so it does not say press any key on the top?
Alternatively, I would prefer to use something like

cout<<"Please enter the name path of input file <e.g.scores.txt>: ";
    string filename;
    cin>>filename;
    fin.open(filename.c_str());
    cout<<endl;

but when the filename scores.txt is typed in, instead of going to the next screen, I get input error.
Program overall works fine, but I wanted to see if knew the issue.

0

I don't know what to tell you about the screen clearing aspect except to direct you to the pluses and minuses of doing such a thing in this thread (in it you can find stuff you can use but also arguments against doing so, so I don't want to recommend any method to you per se)

For the second issue (and it seems like an insulting question but it happens) are you putting in a filename that exists in the current directory. If you put in a path with a space it definitely won't like it.
I did put that section into your code and it does work for me...

0

Thanks for referring to that link. I found my answer!
system("cls"); did the trick ;)
Where as for the second part, I am typing in source.txt since that is the name of the input file which is stored on the same directory as the program. Am I missing something?

Edited by amishraa: n/a

0

Yeah, it shouldn't be a problem. You are typing the full name with the .txt? I can't think of anything else it could be. Try making a second file with the information in it.

0

Yep tried that.. but same thing. Blah.. it's not a big deal. I will just use cin.get() and system("cls") instead.
I was just trying to fix issue with fileoutput where I corrected one of the istream to ostream but receiving output error.
Do you see issue anywhere else on the code that may be causing it?

0
ifstream fin;
ofstream fout;

//function definitions
int main(){
    //system("color f0");
cout<<"Please enter the name path of input file <e.g.scores.txt>: ";
    string filename;
    cin>>filename;
    //fin.open(filename.c_str());
    cout<<endl;
    
    headerfn(); //function call
        
    fin.open(filename.c_str());
    fout.open("results_scores.txt");

There is that whole section of code...

0

I'm not sure if it's doing the same thing on yours. But on mine even if the wrong file name is entered, it goes to the next screen.

Edited by amishraa: n/a

0

okay.. here we go ;)

//preprocessor directive
#include<iostream>
#include<iomanip>
#include<fstream>
#include<conio.h>

using namespace std;

//global variables/constants, function prototypes
void headerfn(); //void function without parameters

ifstream fin;
ofstream fout;

//function definitions
int main(){
    //system("color f0");
    /*
    cout<<"Please enter the name path of input file <e.g. input4.txt>: ";
    string filename;
    cin>>filename;
    cout<<endl;
    */
    headerfn(); //function call
        
    fin.open("input4.txt");
    fout.open("results.txt");
    if (!fin){
              cout<<"Input failure";
              system("pause");
              return 1;
              }
    if(!fout){
              cout<<"Output Failure"<<endl;
              system("pause");
              return 1;
              } //end of error check
    
     string names[7][2];       
     int scores[7][6];
           
    //read the data
   for (int row =0; row<7; row++){
    for(int col=0; col<2; col++)
    fin>>names[row][col];
    for(int col=0; col<6; col++)
    fin>>scores[row][col];
    }// end of outer for
   
   
    for(int row=0;row<7;row++){
        for(int col=0;col<6;col++)
        fin>>scores[row][col];
        }//end of outer for
   
      
    int rowtotals[7];
    int coltotals[6];
    
    for(int row=0;row<7;row++){
    rowtotals[row]=0;
    for(int col=0;col<6;col++)
    rowtotals[row]=rowtotals[row]+scores[row][col];
    }//end of outer for

    for(int col=0;col<6;col++){
            coltotals[col]=0;
            for(int row=0;row<7;row++)
            coltotals[col]+= scores[row][col]; //+= is same as saying coltotals[col]+
            }//end of outer for

//calculates the programs sum and tests sum
float progavg[7]={0}, testavg[7]={0}, progsum[7]={0}, testsum[7]={0}; 
for (int row = 0;row<7;row++)
{ 
        for(int col = 0;col<3;col++)
            progsum[row] += scores[row][col];
        progavg[row] = (float)progsum[row]/3;
        for(int col = 3;col<6;col++)
            testsum[row] += scores[row][col];
        testavg[row] = (float)testsum[row]/3;
}
    
//calculates the course average    
    float courseavg[2];
    for(int col=0;col<7;col++){
        courseavg[col]=(progavg[col]+testavg[col])/2;
        }

    char grade[7];
    for(int row=0;row<7;row++){
    for(int col=0;col<6;col++)
    if (courseavg[row] >=90 )grade[row]='A';
    else if (courseavg[row] >=80 )grade[row]='B';
    else grade[row]='F';
}


    cout<<fixed<<showpoint<<setprecision(2);
    for(int row=0;row<7;row++)
    cout<<left<<setw(20)<<names[row][0]+" "+names[row][1]<<setw(10)<<rowtotals[row]<<setw(10)<<progavg[row]<<setw(10)<<testavg[row]<<setw(10)<<courseavg[row]<<grade[row]<<endl;
    if(fin.peek()=='\n')fin.ignore();
    
    cout<<endl;
    cout<<"Press any key to exit\n";
    cin.get();                     

    return 0;
}//end of main

//***********************************************
//void function without parameters
void headerfn(){
    cout<<"**********************************************************************\n"<<endl;
	cout<<"        Programmer:  James Bond\n"<<endl;
    cout<<"        Program: Arrays \n"<<endl;
	cout<<"        This program uses function to read student information from\n";
	cout<<"        an input text file into string and integer arrays.\n";
	cout<<"        It then calculates and output averages and\n";
	cout<<"        grades to the monitor as well as to an output text file\n"       <<endl;
    cout<<"**********************************************************************\n";
    cout<<endl;
    cout<<"Welcome to the IT210 Grade Calculator\n"<<endl;
    cout<<"Please enter the name path of input file <e.g. input4.txt>: ";
    string filename;
    cin>>filename;
    cout<<"Thank you. You entered input4.txt\n";
    cout<<"Press any key to continue\n"<<endl;
    system("cls");
    cin.get();
    //system("cls");
    cout<<endl;
    cout<<endl;
    cout<<"                          STUDENT GRADES SHEET\n\n                        "<<endl;       
    //cout<<"1234567890123456789012345678901234567890123456789012345678901234567890"<<endl;
    cout<<"----------------------------------------------------------------------"<<endl;          
    cout<<left<<setw(20)<<"Student Name"<<setw(10)<<"Total"<<setw(10)<<"Program"<<setw(10)<<"Test"<<setw(10)<<"Course"<<setw(10)<<"Grade"<<endl;
    cout<<right<<setw(26)<<"Points"<<setw(11)<<"Average"<<setw(10)<<"Average"<<setw(10)<<"Average"<<endl;
    cout<<"----------------------------------------------------------------------"<<endl;
     }//end of headerfn
1

Scope. Your filename variable is out of scope by the time you're back up to main(). So either return it from headerfn() or pass it in by reference, or just put it back up in main(). Technically since your fin and fout are global (which they probably shouldn't be) you can open the file in headerfn() and it will be available elsewhere. But, I would recommend roping fin and fout into main and pass in the filename string by reference).

Votes + Comments
Rep for helping in this monster-thread
0

Guess I can move the first part of the header to the main and one followed after entering the file name could go under the header function.
But the streams were set to global because I am eventually going to be using functions for calculations and outputs. I just wanted to ensure everything works flawlessly before messing with functions.
Thank you. Let me give that a try.

0

You can pass around the ifstream and ofstream objects to your functions. Gosh, I can't believe there's still more to go on this assignment for you.

0

I know! I don't think I have ever spent so much hours in doing a single assignment....
I really owe you like big time! like Dominoes pizza? lol
ohh yah... let me add. I am supposed to do this whole thing over again with 1D.
Now... that I have no clue of. Hopefully, it's not so much different.

Edited by amishraa: n/a

0

Same input, same program but using 1D
How am I doing so far?

//preprocessor directive
#include<iostream>
#include<iomanip>
#include<fstream>
#include<conio.h>

using namespace std;

//global variables/constants, function prototypes
void headerfn(); //void function without parameters 

ifstream fin;
ofstream fout;

//function definitions
int main(){
    
    //ifstream fin;
    //ofstream fout;
    
    while(fin){
    //system("color f0");
    headerfn(); //function call
    
    

    string first[0],last[1];       
    int prog1[0],prog2[1],prog3[2],test1[3],test2[4],test3[5];
     
    fin.open("input4.txt");
    fout.open("results1d.txt");
    
    if (!fin){
              cout<<"Input failure";
              system("pause");
              return 1;
              }
    if(!fout){
              cout<<"Output Failure"<<endl;
              system("pause");
              return 1;
              } //end of error check
    
           
    //read the data
   for (int row =0; row<7; row++)
    fin>>first[row]>>last[row];
    for(int col=0; col<6; col++)
    fin>>prog1[col]>>prog2[col]>>prog3[col]>>test1[col]>>test2[col]>>test3[col];
    }// end of outer for
0

If you have 7 people, all your arrays need to be of length 7. You can't use the same for loops in this case. I would strongly (having seen another poster go through something similar) discourage you from this way of doing it...it's not just going to "work" the same way.
But, what I would recommend you do, if this is your course of action, is to write some small samples for yourself of how to read one row in the text file (there's not going to be much difference in theory, just make sure you are reading into variables that match the type of the data at that position in the text file. Once you've gotten one "row." that is each of your parallel arrays has one item in it, encase it in a loop over the length of your arrays (7). I know you're appreciative of the help, and I appreciate your appreciation, and we will still help you but you have to make sure you are getting the most out of it.

1

Thanks. I think the whole arrays had gotten overwhelming for me. I decided to go back and thoroughly read the book again and I can see where I made mistake on defining array size.
Arrays does not seem to be complicated as long as you're using the correct logic and have loops at the braces at the proper places.
My challenge at this time is incorporating the arrays(2d) with functions so my main looks neat and clean with all the functions after the main.
I will go ahead and set this thread as solved as you have answered basically all of my questions and have helped me tremendously throughout which I really appreciate.
I will spend some time practicing and work on the errors but if I do come up with something I can't figure out or find reference, I may knock back in with more questions! :)
Thanks again. We need more mentors like yourself.

0

No problem. I'm glad to help. It's great that you went back and reviewed all that material -- it's of great benefit. Definitely post back if you need anything.

0

Hey guys, I have done for this problem. Overall, I'm using the link list and node. I treat 1 line with 6 grades + an average as a node. And a list contains 7 nodes.
---------------------------------------------------------

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>

using namespace std;

const int row=7;
const int col=6;

class node
{
	private:
		int grade_1;
		int grade_2;
		int grade_3;
		int grade_4;
		int grade_5;
		int grade_6;
		float average;
		node *next;
		node *previous;
	public:
		node(int g1,int g2,int g3,int g4,int g5,int g6)
		{
			grade_1 = g1;
			grade_2 = g2;
			grade_3 = g3;
			grade_4 = g4;
			grade_5 = g5;
			grade_6 = g6;
			average = 0.0;
			next =NULL;
			previous = NULL;
		}
	//--------------------------
		void setG1(int g1)
		{
			grade_1 = g1;
		}
		int getG1()
		{
			return grade_1;
		}
	//--------------------------
		void setG2(int g2)
		{
			grade_2 = g2;
		}
		int getG2()
		{
			return grade_2;
		}
	//--------------------------
		void setG3(int g3)
		{
			grade_3 = g3;
		}
		int getG3()
		{
			return grade_3;
		}
	//--------------------------
		void setG4(int g4)
		{
			grade_4 = g4;
		}
		int getG4()
		{
			return grade_4;
		}
	//--------------------------
		void setG5(int g5)
		{
			grade_5 = g5;
		}
		int getG5()
		{
			return grade_5;
		}
	//-------------------------
		void setG6(int g6)
		{
			grade_6 = g6;
		}
		int getG6()
		{
			return grade_6;
		}
	//--------------------------
		void setAverage(int g1,int g2,int g3,int g4,int g5,int g6)
		{
			average = static_cast<float>(g1+g2+g3+g4+g5+g6)/6;
		}
		float getAverage()
		{
			return average;
		}
	//--------------------------
		node& setNext(node *n)
		{
			next = n;
			return (*this);
		}
		node* getNext()
		{
			return next;
		}
	//----------------------------
		node& setPrevious(node *p)
		{
			previous = p;
			return (*this);
		}
		node* getPrevious()
		{
			return previous;
		}
};
class list
{
	private:
		node *front;
		node *rear;
	public:
		list()
		{
			front = NULL;
			rear = NULL;
		}
		list& insert(int g1,int g2,int g3,int g4,int g5,int g6)
		{
			node *n = new node(g1,g2,g3,g4,g5,g6);
			if (front==NULL && rear==NULL)
			{
				front = n;
				rear = n;
			}
			else
			{
				n->setPrevious(rear);
				rear->setNext(n);
				rear = n;
			}
			return (*this);
		}
		ostream& write(ostream& out) const
		{
			node *temp = front;

			while (temp!=NULL)
			{
				temp->setAverage(temp->getG1(),temp->getG2(),temp->getG3(),temp->getG4(),temp->getG5(),temp->getG6());
				out << temp->getG1() << " " << temp->getG2() << " " << temp->getG3() << " " << temp->getG4() << " " << temp->getG5() << " " << temp->getG6() << " " << temp->getAverage() << endl;
				temp = temp->getNext();
			}
			return (out);
		}
};
ostream& operator<<(ostream& out,list& l)
{
	l.write(out);
	return out;
}
int main()
{
	list l;
	int arr[7][6];
	char fname[30]="D:\\source.txt";
	ifstream fin;
	ofstream fon;

	fin.open(fname);
	
	while (!fin.eof())
	{
		for (int i=0;i<row;i++)
		{
			for (int j=0;j<col;j++)
				fin >> arr[i][j];
		}
	}
	
	fin.close();

	for (int i=0;i<row;i++)
	{
			l.insert(arr[i][0],arr[i][1],arr[i][2],arr[i][3],arr[i][4],arr[i][5]);
	}

	cout << l << endl;	
	return 0;
}

Edited by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.