soo
?Code:averagef= fgpa / fcounter; averagem= mgpa / mcounter;
soo
?Code:averagef= fgpa / fcounter; averagem= mgpa / mcounter;
Ahh... that looks good. And it makes sense, too, right?
Now just output it. Then run your program and see if it gives you the output you expect.
hehe thanks, but I think something is wrong maybe in my declaration area? because I get the same answer for m and f
Code://include section #include <iostream> #include <conio.h> //for use with getch #include <string> //string data types #include <fstream> // in and out data using namespace std; int main () { string name,majorname,coursename; ifstream inData; ofstream outData; double gpa=0; double mgpa=0; double fgpa=0; int averagef=0; int averagem=0; int mcounter=0; int fcounter=0; char gender=0; char m=0; inData.open("input.txt"); outData.open("outData.txt"); //stuff writting out to screen / displayed cout<<"Hello there, What is your name?"<< endl; getline(cin,name); cout<<"Welcome "<<name<<endl; cout<<"Please enter your degree your majoring in."<<endl; getline(cin,majorname); cout<<"What the name of the course your taking for your major "<<majorname<<" ?"<<endl; getline(cin,coursename); cout<<"What is your gender? Enter m for male or f for female"<<endl; cin>>gender; //Stuff written out to txt file outData<<"Person's name is " <<name<<". And there degree major is "<<majorname<<"." " The class course name for there degree major is "<<coursename<< "."<<endl; while(inData >> gender >> gpa) // loop to count the males and females in the file { if(gender=='m') { mcounter++; mgpa=mgpa+gpa; } else if (gender=='f') { fcounter++; fgpa=fgpa+gpa; } } averagef= fgpa / fcounter;// Average GPA. Divide the total GPA by the number of entries found in the file. averagem= mgpa / mcounter; // Average GPA if(gender=='m') { cout<<"the average for the male gpa is: "<<averagem<<"" <<endl; } else if (gender=='f') { cout<<"the average for the female gpa is: "<<averagef<<"" <<endl; } getch (); return 0; }
Let's look at your output code:When you use an if/else if, that means only one of those blocks of code will ever run. You're saying you want to the first block of code to run in some cases, and the second block of code to run in other cases. But you're trying to output the averages. Don't you always want to output the average for male and the average for female?Code:if(gender=='m') { cout<<"the average for the male gpa is: "<<averagem<<"" <<endl; } else if (gender=='f') { cout<<"the average for the female gpa is: "<<averagef<<"" <<endl; }
There shouldn't be any restriction on when those cout lines are executed. I'm guessing that in your head you want the cout<<"the average for the male gpa is: "<<averagem<<"" <<endl; line to run if the gender is male and the cout<<"the average for the female gpa is: "<<averagef<<"" <<endl; line to run if the gender is female. But that's the wrong way to think about it. You should always want both lines to run, because you want to see both averages. You have already separated the male average from the female average by storing them in different variables (averagem and averagef), so there is no need to add the extra ifs.
but when i didn't have the if/else, it would show both the male and the female averages, when I asked for the male or for the female
Your assignment says, "Write a program that computes and outputs the average GPA for both females and male students." So don't you want to show both male and female averages?
If you only want to show one or the other, then the problem is that you are using the gender variable for two different things. You use it to ask the user which gender he or she is. Then you use it later to read in the genders in the file. Whatever the last gender in the file is will be stored into the gender variable, so you can't use that variable to hold the answer the user gave.
I think she wants us to show only one average because if not she wouldn't have told us to ask the user if it were male or female?
But i'd also like to get only one if possible. But how would I make that work
Code://include section #include <iostream> #include <conio.h> //for use with getch #include <string> //string data types #include <fstream> // in and out data using namespace std; int main () { string name,majorname,coursename; ifstream inData; ofstream outData; double gpa=0; double mgpa=0; double fgpa=0; int averagef=0; int averagem=0; int mcounter=0; int fcounter=0; char gender=0; char m=0; inData.open("input.txt"); outData.open("outData.txt"); //stuff writting out to screen / displayed cout<<"Hello there, What is your name?"<< endl; getline(cin,name); cout<<"Welcome "<<name<<endl; cout<<"Please enter your degree your majoring in."<<endl; getline(cin,majorname); cout<<"What the name of the course your taking for your major "<<majorname<<" ?"<<endl; getline(cin,coursename); cout<<"What is your gender? Enter m for male or f for female"<<endl; cin>>gender; //Stuff written out to txt file outData<<"Person's name is " <<name<<". And there degree major is "<<majorname<<"." " The class course name for there degree major is "<<coursename<< "."<<endl; while(inData >> gender >> gpa) // loop to count the males and females in the file { if(gender=='m') { mcounter++; mgpa=mgpa+gpa; } else if (gender=='f') { fcounter++; fgpa=fgpa+gpa; } } if(gender=='m') { averagem= mgpa / mcounter; // Average GPA cout<<"the average for the male gpa is: "<<averagem<<"" <<endl; } else if (gender=='f') { averagef= fgpa / fcounter;// Average GPA. Divide the total GPA by the number of entries found in the file. cout<<"the average for the female gpa is: "<<averagef<<"" <<endl; } getch (); return 0; }
Your variable gender is used to input what the users wants and later OVERWRITTEN by the internal code that gathers the information. Use a separate variable to store the user input and check that variable later.
Something like that? or different
Code://include section #include <iostream> #include <conio.h> //for use with getch #include <string> //string data types #include <fstream> // in and out data using namespace std; int main () { string name,majorname,coursename; ifstream inData; ofstream outData; double gpa=0; double mgpa=0; double fgpa=0; double averagef=0; double averagem=0; int mcounter=0; int fcounter=0; char genderFromFile=0; char gender=0; char m=0; inData.open("input.txt"); outData.open("outData.txt"); //stuff writting out to screen / displayed cout<<"Hello there, What is your name?"<< endl; getline(cin,name); cout<<"Welcome "<<name<<endl; cout<<"Please enter your degree your majoring in."<<endl; getline(cin,majorname); cout<<"What the name of the course your taking for your major "<<majorname<<" ?"<<endl; getline(cin,coursename); cout<<"What is your gender? Enter m for male or f for female"<<endl; cin>>gender; //Stuff written out to txt file outData<<"Person's name is " <<name<<". And there degree major is "<<majorname<<"." " The class course name for there degree major is "<<coursename<< "."<<endl; while(inData >> gender >> gpa) // loop to count the males and females in the file { if(gender=='m') { mcounter++; mgpa=mgpa+gpa; } else if (gender=='f') { fcounter++; fgpa=fgpa+gpa; } } while(inData >> genderFromFile) { if(gender=='m') { averagem= mgpa / mcounter; // Average GPA cout<<"the average for the male gpa is: "<<averagem<<"" <<endl; } else if (gender=='f') { averagef= fgpa / fcounter;// Average GPA. Divide the total GPA by the number of entries found in the file. cout<<"the average for the female gpa is: "<<averagef<<"" <<endl; } } getch (); return 0; }
Have you tried it, and if so, did it do what you want it to do?
I'm a bit suspicious that you loose the original input of gender, and that you are trying to read gender from the file after you have failed to read something from that said file.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Then should I move everything here? or? I really don't know how to fix it :/
Code:while(inData >> gender >> gpa) { if(gender=='m') { mcounter++; mgpa=mgpa+gpa; averagem= mgpa / mcounter; cout<<"the average for the male gpa is: "<<averagem<<"" <<endl; } else if (gender=='f') { fcounter++; fgpa=fgpa+gpa; averagef= fgpa / fcounter; cout<<"the average for the female gpa is: "<<averagef<<"" <<endl; } }
Sigh. No. You really seem to have no grasp for the language. We already told you that you were overwriting the data you inputted from the user.
Make a new variable!
Look at those red lines and try to understand what I just did. I made a new variable to hold the data.Code://include section #include <iostream> #include <conio.h> //for use with getch #include <string> //string data types #include <fstream> // in and out data using namespace std; int main () { string name,majorname,coursename; ifstream inData; ofstream outData; double gpa=0; double mgpa=0; double fgpa=0; double averagef=0; double averagem=0; int mcounter=0; int fcounter=0; char genderFromFile=0; char gender=0; char show_gender = '\0'; char m=0; inData.open("input.txt"); outData.open("outData.txt"); //stuff writting out to screen / displayed cout<<"Hello there, What is your name?"<< endl; getline(cin,name); cout<<"Welcome "<<name<<endl; cout<<"Please enter your degree your majoring in."<<endl; getline(cin,majorname); cout<<"What the name of the course your taking for your major "<<majorname<<" ?"<<endl; getline(cin,coursename); cout<<"What is your gender? Enter m for male or f for female"<<endl; cin>>show_gender; //Stuff written out to txt file outData<<"Person's name is " <<name<<". And there degree major is "<<majorname<<"." " The class course name for there degree major is "<<coursename<< "."<<endl; while(inData >> gender >> gpa) { if(gender=='m') { mcounter++; mgpa=mgpa+gpa; } else if (gender=='f') { fcounter++; fgpa=fgpa+gpa; } } if(show_gender=='m') { averagem= mgpa / mcounter; // Average GPA cout<<"the average for the male gpa is: "<<averagem<<"" <<endl; } else if (show_gender=='f') { averagef= fgpa / fcounter;// Average GPA. Divide the total GPA by the number of entries found in the file. cout<<"the average for the female gpa is: "<<averagef<<"" <<endl; } getch (); return 0; }
Try to do something similar in your own code, to make sure you learn it.
ok, did and somewhat get it but I still don't understand why it gives the same answer for both m + f and now its more off than before on the answer?