21 Replies - 795 Views - Last Post: 14 February 2013 - 01:43 AM
#1
Data file sorting question
Posted 12 February 2013 - 02:07 PM
I am a computer science student taking my first C++ class and this is my first time posting a query online. So, I apologize in advance for my lack of savvy. Up until now, I have not had any problems whatsoever but my current assignment is throwing me for one heck of a loop so I was hoping one of you more experienced folk can point me in the right direction.
These are my assignment instructions:
1 - Your assignment is to prompt the user to enter the filename with the path on disk. If the file does not exist in the specified location, your program should exit with a suitable error message.
2 - The first thing your program should do is output to the screen a copy of the data read in from the disk file. This is known as “echoing” the input data.
3 - Your program should then calculate and display the average score for males, females, community college students, and university students. Display your results to two places of decimals, and write your program to automatically list your four calculated averages one to a line, along with a suitable label for each result.
4 - Finally, your program should calculate and display to two places of decimals the overall average score for all of the students surveyed.
** I have no code to paste yet but after getting two basic questions answered, I will compile my code and post it for further discussion. I just want to be sure my algorithm is correct before laying down the code. I am sure planning it correctly will save me hours of correcting code down the line.
Questions:
1 - I understand loops fairly well but I don't know how to get the while loop to read info from a data file, one "column" at a time. They are not allowing us to do arrays yet but even if they did, I think arrays work line by line instead of column by column. I have a feeling though, that through the use of loops, I can potentially manipulate the data search to the point that it will bypass everything except the data I need but I have no idea if that is the direction I need to go or if it is a matter of figuring out how to get a loop to read data from specific columns.
2 - The provided text file is this:
Bailey M CC 68
Harrison F CC 71
Grant M UN 75
Peterson F UN 69
Hsu M UN 79
Bowles M CC 75
Anderson F UN 64
Nguyen F CC 68
Sharp F CC 75
Jones M UN 75
McMillan F UN 80
Gabriel F UN 62
Since I am not outputting data to the file but instead, querying for input from the file, I would guess that I do not need any outfile commands but I am not sure about that. I am guessing the teacher put a list of names in there to make it harder to access the data I need (like the gender column). I am happy to provide more details if needed and thank you for your time!
Replies To: Data file sorting question
#2
Re: Data file sorting question
Posted 12 February 2013 - 02:54 PM
Jim
#3
Re: Data file sorting question
Posted 12 February 2013 - 03:08 PM
jimblumberg, on 12 February 2013 - 01:54 PM, said:
Jim
Hello Jim,
Thanks for the heads up! Ok, here is a quazi template I whipped up:
I pseudo coded some stuff that I wasn't sure how to represent in C++ and I just put in the info to get the average for hte men and women.
// main.cpp
// a04
// No Source
// Created by Jason Rodriguez on 2/10/13.
// Copyright (c) 2013 PCC. All rights reserved.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//Declare variables to manipulate data
char m;
char f;
string commcoll;
string uni;
double malescore;
double maleaverage;
double femalescore;
double femaleaverage;
double commcollscore;
double commcollaverage;
double uniscore;
double uniaverage;
double overallsum = 0;
int count = 0;
//Declare stream variables
ifstream inData;
ofstream outData;
//Open input file
inData.open("/Users/jasonrodriguez/Desktop/scores.txt");
if (!inData) {
cout << "Cannot open input file. "
<< "Program will now terminate." << endl;
return 1; }
//Open input file
inData.open("/Users/jasonrodriguez/Desktop/scores.txt");
inData << fixed << showpoint << setprecision(2);
inData >> malescore; //read the test score
while (inData)
{
if (column 2 == m)
malescore = malescore + column 4 input;
else (column 2 == f)
femaleaverage = femaleaverage;
}
inData.close();
outData.close();
return 0;
}
This post has been edited by jimblumberg: 12 February 2013 - 04:24 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.
#4
Re: Data file sorting question
Posted 12 February 2013 - 04:03 PM
// main.cpp
// a04
// No Source
// Created by Jason Rodriguez on 2/10/13.
// Copyright (c) 2013 PCC. All rights reserved.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//Declare variables to manipulate data
char m;
char f;
string commcoll;
string uni;
double malescore;
double maleaverage;
double femalescore;
double femaleaverage;
double commcollscore;
double commcollaverage;
double uniscore;
double uniaverage;
double overallsum = 0;
int count = 0;
//Declare stream variables
ifstream inData;
ofstream outData;
// open output file and run program exit failsafe command
inData.open("/Users/jasonrodriguez/Desktop/scores.txt");
if (!inData) {
cout << "Cannot open input file. "
<< "Program will now terminate." << endl;
return 1; }
while(infile.get()) // echo
{
cout << infile;
}
outData << fixed << showpoint << setprecision(2);
inData >> malescore; //give a value to male score
while (inData) // while reading incoming data from file, execute the conditions
{
if (column 2 == m)
malescore = malescore + column 4 input;
else (column 2 == f)
femaleaverage = femaleaverage
inData.close();
return 0;
}
This post has been edited by jimblumberg: 12 February 2013 - 04:24 PM
Reason for edit:: Fixed Code Tags.
#5
Re: Data file sorting question
Posted 12 February 2013 - 04:03 PM
There is no need to open the file twice.
#6
Re: Data file sorting question
Posted 12 February 2013 - 04:10 PM
Without code tags around it, your code gets turned to mishmash, by the forum software.
1) Don't open the input file twice - just once (the first one), is fine. Do you need to specify that you're opening the file for reading only?
2) You want to read in the data, one line at a time. You'll count the number of CC students, etc., and keep a running average for the three types, and you've got that started. Since the data is strictly formatted (4 items on each line, and in the same order), you can read them right in with cin, I believe. The space between them will prevent any mixing. (I code in C only).
I'd suggest char sex, with integers of maleNum and femNum, instead of two char variables.
3) The user has to enter the file name, so prompt them, and get it, with appropriate error messages if the file can't be found.
Good quick start, however.
This post has been edited by Adak: 12 February 2013 - 04:16 PM
#7
Re: Data file sorting question
Posted 12 February 2013 - 04:13 PM
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//Declare variables to manipulate data
char m;
char f;
string commcoll;
string uni;
double malescore;
double maleaverage;
double femalescore;
double femaleaverage;
double commcollscore;
double commcollaverage;
double uniscore;
double uniaverage;
double overallsum = 0;
int count = 0;
//Declare stream variables
ifstream inData;
ofstream outData;
// open output file and run program exit failsafe command
inData.open("/Users/jasonrodriguez/Desktop/scores.txt");
if (!inData) {
cout << "Cannot open input file. "
<< "Program will now terminate." << endl;
return 1; }
while(infile.get()) // echo
{
cout << infile;
}
outData << fixed << showpoint << setprecision(2);
inData >> malescore; //give a value to male score
while (inData) // while reading incoming data from file, execute the conditions
{
if (column 2 == m)
malescore = malescore + column 4 input;
else (column 2 == f)
femaleaverage = femaleaverage
inData.close();
return 0;
}
#8
Re: Data file sorting question
Posted 12 February 2013 - 04:22 PM
That's the BIG while loop, so it has to go around everything you're doing with the data.
while you have more data {
read the data: name sex type score
increment the number of students, sex, and the type of student
update the sum of the scores, etc.
}//loop back
This post has been edited by Adak: 12 February 2013 - 04:23 PM
#9
Re: Data file sorting question
Posted 12 February 2013 - 04:54 PM
35 while(infile.get()) // echo
36 {
37 cout << infile;
38 }
istream::get
#10
Re: Data file sorting question
Posted 12 February 2013 - 05:10 PM
Adak, on 12 February 2013 - 03:10 PM, said:
icon (top dead center almost), of the editor icon bar.
Without code tags around it, your code gets turned to mishmash, by the forum software.
1) Don't open the input file twice - just once (the first one), is fine. Do you need to specify that you're opening the file for reading only?
2) You want to read in the data, one line at a time. You'll count the number of CC students, etc., and keep a running average for the three types, and you've got that started. Since the data is strictly formatted (4 items on each line, and in the same order), you can read them right in with cin, I believe. The space between them will prevent any mixing. (I code in C only).
I'd suggest char sex, with integers of maleNum and femNum, instead of two char variables.
3) The user has to enter the file name, so prompt them, and get it, with appropriate error messages if the file can't be found.
Good quick start, however. ;)/>/>/>
[/quote]
Thanks for the info! I am not clear on why I shouldn't use a two chars though. Don't I need to declare them so when the program reads the file, it can differentiate what to do depending on if the char in that particular line is an M or a F?
[quote name='#define' date='12 February 2013 - 03:54 PM' timestamp='1360713244' post='1801723']
Data is usually read from a stream and stored in a [i]variable[/i]. When outputted to a stream [i]from[/i] a [i]variable[/i].
[code]
35 while(infile.get()) // echo
36 {
37 cout << infile;
38 }
istream::get
Hmm, I think I understand. So, in order to echo the file, I need to note istream.get instead of infile?
Ok, I think I am making some headway now. How is this looking? Am I getting it down? :
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//Declare variables to manipulate data
char M;
char F;
string file;
string commcoll;
string uni;
double malescore;
double maleaverage;
double femalescore;
double femaleaverage;
double commcollscore;
double commcollaverage;
double uniscore;
double uniaverage;
double overallsum = 0;
int count = 0;
int score;
//Declare stream variables
ifstream inData;
ofstream outData;
// Promt user for file location
cout << "Please input file location: ";
cin >> file;
// open output file and run program exit failsafe command
inData.open(file);
if (!inData) {
cout << "Cannot open input file. "
<< "Program will now terminate." << endl;
return 1; }
outData << fixed << showpoint << setprecision(2);
// while reading incoming data from file, execute the conditions
while (inData)
{
if (char = M)
malescore = malescore + score;
else if (char = F)
femalescore = femalescore + score;
count ++;
}
inData.close();
return 0;
}
#11
Re: Data file sorting question
Posted 12 February 2013 - 05:16 PM
if(sex=='M') {
malescores+=newscore;
++maleNum;
}else {
femalescores+=newscore;
++femNum;
}
++studentNum;
is what I was suggesting. Comparisons require two == chars, not just one. One is for assignment.
I'd be quite tempted to use the data echoing to confirm that in fact, the data was being read into the variables, properly.
get the data from the file, and assign it to the variables.
print the variables to confirm the data is being read properly.
process the data, either before or after the echoing, whichever you prefer.
Just a suggestion, of course.
You need to work with ALL your variables, inside that while loop, though.
But you're on the right track.
This post has been edited by Adak: 12 February 2013 - 05:22 PM
#12
Re: Data file sorting question
Posted 12 February 2013 - 06:02 PM
Adak, on 12 February 2013 - 04:16 PM, said:
if(sex=='M') {
malescores+=newscore;
++maleNum;
}else {
femalescores+=newscore;
++femNum;
}
++studentNum;
is what I was suggesting. Comparisons require two == chars, not just one. One is for assignment.
I'd be quite tempted to use the data echoing to confirm that in fact, the data was being read into the variables, properly.
get the data from the file, and assign it to the variables.
print the variables to confirm the data is being read properly.
process the data, either before or after the echoing, whichever you prefer.
Just a suggestion, of course.
You need to work with ALL your variables, inside that while loop, though.
But you're on the right track.
Hmm, I think am starting to follow. So, newscore is supposed to be the integer value of M for each line that the while loop repeats by and that value is given by the data file and added to the total score which is represented by "malescores". If so, I need to create an int variable for newscore and the file itself will give it a numerical value per line?
#define, on 12 February 2013 - 03:54 PM, said:
35 while(infile.get()) // echo
36 {
37 cout << infile;
38 }
istream::get
Thank you for that! So, is this an echo? :
{
cout << istream.get;
}
#13
Re: Data file sorting question
Posted 12 February 2013 - 06:12 PM
Now inside that loop that echoes the data, you should have ALL your variables being assigned their values, and totals being summed up where appropriate, and increments to other variables (like maleNum or femNum), being made.
It can all be done inside one BIG while loop.
#14
Re: Data file sorting question
Posted 12 February 2013 - 06:20 PM
{
cout << istream.get;
}
The get is a function so has (). You need a named instance of the istream ie
{
cout << inData.get();
}
Using a variable:
{
char ch = inData.get()
cout << ch;
}
.
This post has been edited by #define: 12 February 2013 - 06:21 PM
#15
Re: Data file sorting question
Posted 12 February 2013 - 07:28 PM
Adak, on 12 February 2013 - 04:16 PM, said:
if(sex=='M') {
malescores+=newscore;
++maleNum;
}else {
femalescores+=newscore;
++femNum;
}
++studentNum;
is what I was suggesting. Comparisons require two == chars, not just one. One is for assignment.
I'd be quite tempted to use the data echoing to confirm that in fact, the data was being read into the variables, properly.
get the data from the file, and assign it to the variables.
print the variables to confirm the data is being read properly.
process the data, either before or after the echoing, whichever you prefer.
Just a suggestion, of course.
You need to work with ALL your variables, inside that while loop, though.
But you're on the right track.
Hmm, ok so I kept in mind what you said and made a few changes. I decided to sort the gender scores first. I think I am having a problem with the scores being read. The compiler keeps saying that the int variables here :
while (inData)
{
if(sex=='M')
{
maleScore = maleScore + maleTotal;
++maleCount;
}
else if(sex =='F')
{
femScore = femTotal;
++femCount;
}
}
are not declared. I don't know why though. I have a feeling that I am not making the connection between the variables and the source file. I thought all I had to do was use (inData) as the condition in my while loop. Here is the whole code so far:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//Declare variables to manipulate data
char sex;
string name;
string school;
string fileSource;
int maleScore;
int femScore;
int UniScore;
int commcollScore;
int maleTotal;
int femTotal;
int uniTotal;
int commcollTotal;
double sum = 0;
int femCount = 0;
int maleCount = 0;
int score;
//Declare stream variables
ifstream inData;
ofstream outData;
inData >> name >> sex >> school >> score;
// Promt user for file location
cout << "Please input file location: ";
cin >> fileSource;
// open output file and run program exit failsafe command
inData.open(fileSource);
if (!inData)
{
cout << "Cannot open input file. "
<< "Program will now terminate." << endl;
return 1;
}
outData << fixed << showpoint << setprecision(2);
// echo the data file
while (inData)
{
cout << name << sex << school << score << endl;
inData >> name >> sex >> school >> score;
}
// while reading incoming data from file, execute the conditions
while (inData)
{
if(sex=='M')
{
maleScore = maleScore + maleTotal;
++maleCount;
}
else if(sex =='F')
{
femScore = femTotal;
++femCount;
}
}
}
Lastly, I am a bit confused as to how to use the counter variables maleCount, femCount, and studentCount, to make the loop stop. I do think I can use the count variables to divide by in order to find the average though.
|
|

New Topic/Question
Reply



MultiQuote




|