PDA

View Full Version : @ Shaq 2000 or any that knows C++



Bandito
05-26-2014, 05:25 PM
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

void openfiles(ifstream &inFile, ofstream &outFile);
void initialize(int &countFemale, int &countMale, double &sumFemaleGPA, double &sumMaleGPA);
void sumGrades(int &countFemale, int &countMale, ifstream &inFile, double &sumFemaleGPA, double &sumMaleGPA);
void averageGrade(double sumMaleGPA, double sumFemaleGPA, double &averageFemaleGPA, double &averageMaleGPA, int countFemale, int countMale);
void printResults(double averageFemaleGPA, double averageMaleGPA, ofstream &outFile);
int _tmain(int argc, _TCHAR* argv[])
{
ifstream Filein;
ofstream Fileout;
int countF, countM;
double sumGPAF, sumGPAM, aveGPAF,aveGPAM;
openfiles(Filein, Fileout);
initialize(countF, countM, sumGPAF,sumGPAM);
sumGrades(countF, countM, Filein, sumGPAF, sumGPAM);
averageGrade(sumGPAM, sumGPAF, aveGPAF, aveGPAM, countF, countM);
printResults(aveGPAF, aveGPAM, Fileout);
system("pause");
return 0;
}

void openfiles(ifstream &inFile, ofstream &outFile)
{
inFile.open("C:\\Users\\Bandito\\Desktop\\entrada_C++.txt");
outFile.open("C:\\Users\\Bandito\\Desktop\\saida_C++.txt");
outFile << fixed<< setprecision(2);
}
void initialize(int &countFemale, int &countMale, double &sumFemaleGPA, double &sumMaleGPA)
{
countFemale = 0;
countMale = 0;
sumFemaleGPA = 0;
sumMaleGPA = 0;
}
void sumGrades(int &countFemale, int &countMale, ifstream &inFile, double &sumFemaleGPA, double &sumMaleGPA)
{
double line;
char sex;
while (inFile >> line)
{

sumFemaleGPA = sumFemaleGPA + line;
countFemale++;

sumMaleGPA = sumMaleGPA + line;
countMale++;

}
}

void averageGrade(double sumMaleGPA, double sumFemaleGPA, double &averageFemaleGPA, double &averageMaleGPA, int countFemale, int countMale)
{
averageFemaleGPA = sumFemaleGPA / countFemale;
averageMaleGPA = sumMaleGPA / countMale;
}
void printResults(double averageFemaleGPA, double averageMaleGPA, ofstream &outFile)
{
cout << "The average female GPA is: " << averageFemaleGPA << " and the average male GPA is: " << averageMaleGPA << endl;
outFile << "The average female GPA is: " << averageFemaleGPA << " and the average male GPA is: " << averageMaleGPA << endl;
}

Can you tell me what is wrong with the code?

Or somebody else that knows about code?

ace23
05-26-2014, 05:41 PM
What's the line and error message?

ace23
05-26-2014, 05:46 PM
You have two arrows in your while loop.

Based on what reppy posted, I'm guessing that's something I haven't learned yet. I'm new to C++.

reppy
05-26-2014, 05:47 PM
Is it parsing a text file? Like C:\Users\Bandito\Desktop\entrada_C++.txt?

What does that file look like?

Also, in sumGrades, how does it know whether or not the line belongs to a male or female? You never use the sex variable.

double line;

while (inFile >> line)
{
}

does "inFile >> line" automatically parse as a double?

If I was doing it in C#, it would look something like this

string line;
double sum = 0;

while ((line = file.ReadLine()) != null)
{
double number = Double.Parse(line);
sum += number;
}

Maybe C++ has something that will automatically parse as a double for you. But I don't assume it would.

Bandito
05-26-2014, 06:16 PM
What's the line and error message?
There is no error message. It just doesn't read from the input file. My main problem is with the function called sumGrades

Bandito
05-26-2014, 06:18 PM
Is it parsing a text file? Like C:\Users\Bandito\Desktop\entrada_C++.txt?

What does that file look like?

Also, in sumGrades, how does it know whether or not the line belongs to a male or female? You never use the sex variable.

double line;

while (inFile >> line)
{
}

does "inFile >> line" automatically parse as a double?

If I was doing it in C#, it would look something like this

string line;
double sum = 0;

while ((line = file.ReadLine()) != null)
{
double number = Double.Parse(line);
sum += number;
}

Maybe C++ has something that will automatically parse as a double for you. But I don't assume it would.


2.00
3.00
4.00
1.52
1.34
3.54
4.00
1.89

this is what the file is like.

about the sex, I had it made differently but I change it because it wasn't reading the file at all. So what I did is left it like that until my program read the input file.

What I was using was f for female and m for male and using ifelse to compare.

reppy
05-26-2014, 06:46 PM
Can you add a printf("%d\r\n", line) inside your while loop? Or even just printf("aaa") so you can confirm it's actually entering the loop?

My guess is that the while loop is never entered. Perhaps the file stream is invalid. I am fairly certain that C++ won't automatically parse a line from a text file as a double.

Bandito
05-26-2014, 06:49 PM
I found out why. The file that had the data was written wrong. :lol

It wrote .txt two times on the text file:roll:

so it was
entrada_C++.txt.txt

reppy
05-26-2014, 06:54 PM
Gotcha. That was my next guess, haha.

Bandito
05-26-2014, 07:20 PM
Gotcha. That was my next guess, haha.
I didn't even notice it wrongly wrote until I decided to check:lol


Now it doesn't work but I am trying to work it. I am not good with fstreams...but it is all part of learning I guess...

Bandito
05-26-2014, 08:55 PM
how the **** do you change a char to a double. And I don't meant the ASCII equivalent...<.<

Can't find anything googling...I am so mad...

reppy
05-26-2014, 09:19 PM
What are you trying?

And have you tried looking on Stackoverflow? Google something like stackoverflow c++ parse double

Bandito
05-26-2014, 10:12 PM
Here is the full code. It was done in visual C++ btw:


// ConsoleApplication7.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;

void openfiles(ifstream &inFile, ofstream &outFile);
void initialize(int &countFemale, int &countMale, double &sumFemaleGPA, double &sumMaleGPA);
void sumGrades(int &countFemale, int &countMale, ifstream &inFile, double &sumFemaleGPA, double &sumMaleGPA);
void averageGrade(double sumMaleGPA, double sumFemaleGPA, double &averageFemaleGPA, double &averageMaleGPA, int countFemale, int countMale);
void printResults(double averageFemaleGPA, double averageMaleGPA, ofstream &outFile);

int _tmain(int argc, _TCHAR* argv[])
{
ifstream Filein;
ofstream Fileout;
int countF, countM;
double sumGPAF, sumGPAM, aveGPAF,aveGPAM;
//////////////////////////////////////////////////
//////////////////////////////////////////////////
openfiles(Filein, Fileout);
initialize(countF, countM, sumGPAF,sumGPAM);
sumGrades(countF, countM, Filein, sumGPAF, sumGPAM);
averageGrade(sumGPAM, sumGPAF, aveGPAF, aveGPAM, countF, countM);
printResults(aveGPAF, aveGPAM, Fileout);
//////////////////////////////////////////////////
//////////////////////////////////////////////////
system("pause");
return 0;
}

void openfiles(ifstream &inFile, ofstream &outFile)
{
inFile.open("C:\\Users\\Bandito\\Desktop\\entrada_C++.txt");
outFile.open("C:\\Users\\Bandito\\Desktop\\saida_C++.txt");
outFile << fixed<< setprecision(2);
}
void initialize(int &countFemale, int &countMale, double &sumFemaleGPA, double &sumMaleGPA)
{
countFemale = -1;
countMale = -1;
sumFemaleGPA = 0;
sumMaleGPA = 0;
}
void sumGrades(int &countFemale, int &countMale, ifstream &inFile, double &sumFemaleGPA, double &sumMaleGPA)
{
double line = 0;
string sex = " ";
char gender;
std::stringstream ss;

while (inFile >> sex)
{

if (sex == "f")
gender = 'f';
else
if (sex == "m")
{
gender = 'm';
line = 0;
}
else
{

ss << sex<< ' ';
ss >> line;
}


if (gender == 'f')
{

sumFemaleGPA = sumFemaleGPA + line;
countFemale++;

}
else
if (gender == 'm')
{
sumMaleGPA = sumMaleGPA + line;
countMale++;

}
}
}

void averageGrade(double sumMaleGPA, double sumFemaleGPA, double &averageFemaleGPA, double &averageMaleGPA, int countFemale, int countMale)
{
averageFemaleGPA = sumFemaleGPA / countFemale;
averageMaleGPA = sumMaleGPA / countMale;
}
void printResults(double averageFemaleGPA, double averageMaleGPA, ofstream &outFile)
{
outFile << "The average female GPA is: " << averageFemaleGPA << " and the average male GPA is: " << averageMaleGPA << endl;
}

it works perfectly.

Here is the inside of the text file (called: "entrada_C++"):


f 2.00 3.00 4.00 1.52
m 1.34 3.54 4.00 1.89

Bandito
05-26-2014, 10:14 PM
What are you trying?

And have you tried looking on Stackoverflow? Google something like stackoverflow c++ parse double
I used stringstream for it. I don't know how I did it, but it worked. I am going to study stringstream really well seen as I can change string to double real easy.








I was googling for awhile on how to change char to double, but I just decided to change a string to double, which is easier.