Stuck with user-defined functions !!

Good Day everyone

This is my first topic here . I am a newbie who is trying so hard not to hate CS and actually pass the course with a functioning brain !!!

I would really appreciate your help because I am going crazy here trying to figure out what the hell is wrong with my code . It won't work even though I've got 0 errors in compiling !!

Actually I have two problems

The first problem:

* Write a value returning function called length() that takes a positive integer and returns the number of digits in that integer . Using the written function in part1 , write a value returning function called isPalindrome () that checks if a positive integer is palindrome or not . The function takes a positive integer and returns true if the integer is a palindrome and false otherwise .
Test your functions by writing a main function that prompts the user to input an integer and outputs its number of digits and " It is a palindrome ' message if its a palindrome , otherwise " It is not a palindrome " .

Here is my code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <string>
#include <cctype>
#include <cmath>

using namespace std ;

int length (int) ;
bool isNumPalindrome (int) ;

int main ()
{
	int num , digits = 0  ;
	  

	cout << "Enter an integter : " ;
	cin >> num ;

	length (num) ;

	cout << "The number of digits in your integer is " << digits << endl ;

	isNumPalindrome (num);

	if ( isNumPalindrome (num) == 1 )
			cout << "It is a Palindrome" << endl ;
		else
			cout << "It is not a Palindrome" << endl ;

	return 0 ;
}

int length ( int num )
{
	int digits = 0 ;

	if ( num == 0 )
	digits = 1;

	while ( num != 0 )
	{
		digits++ ;
		num = num / 10 ;
	}

	return digits ;
}

bool isNumPalindrome(int num)
{
  int pwr = 0;

  if(num < 10)						
		return true;
  else						
  {
   while(num / static_cast<int>(pow(10,pwr)) >= 10)										
		      pwr++;
	
     while(num >= 10)				
     {
	      if((num / static_cast<int>(pow(10,pwr))) != (num % 10))
		      return false;				
else							
		  {						
	         num = num % static_cast<int>(pow(10,pwr)); 
								
	  	     num = num / 10;			      
	         pwr = pwr - 2;			      
	      }
	 }

    return true;
  }


		
}



Example : if I enter 10 I should get 2 digits and " it is not a palindrome message " , but the number of digits I get is 0 ( the second part works fine )



The second problem :

For research purposes and to better help students, the admission office of your local university wants to know how well female and male students perform in certain courses. You will receive a file that contains female and male students GPA's for certian courses. Due to confidentiality, the letter code f is used for female students and m is used for male students. Every file entry consists of a letter code followed by a GPA. Each line has one entry. The number of entries in the file is unknown. Write a program that computes and outputs the average GPA for both female and male students. Format your results to two decimal places. Your program should use the following functions.

a. Function openFiles: This function opens the input and output files, and sets the output of the floating-point numbers to two decimal places in a fixed decimal format with a decimal point and trailing zeros.

b. Function initialize: This function initializes variables such as countFemale, countMale, sumFemaleGPA, and sumMaleGPA.

c. Function sumGrades: This function finds the sum of the female and male students GPA's.

d. Function averageGrade: This function finds the average GPA for female and male students.

e. Function printResults: This function outputs the relevant results.

f. There can be no global variables. Use the appropriate parameters to pass information in and out of functions,



Here are the enteries in the GPA.txt file :

f 3.40
f 4.00
m 3.56
m 3.80
f 2.30
f 3.95
m 3.90
m 4.00
m 2.00
f 4.00
f 2.80
m 3.70
m 2.98
f 3.89
m 4.00
f 3.90
m 1.90
m 2.90
f 1.50
f 2.67
m 3.80
m 2.35
f 2.90
f 3.70
f 4.00
m 3.78
m 4.00
f 3.98


Here is the code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void openFiles (ifstream&, ofstream&);
void initialize (ifstream&, ofstream&);
void sumGrades (float&, float&, ifstream&, ofstream&, int&, int&);
void averageGrade (int&, int&, float&, float&, ifstream&, ofstream&);
void printResults (int&, int&, float&, float&, ifstream&, ofstream&, float&, float&);

int main() 
{
	ifstream in ;
	ofstream out ;
	openFiles (in, out) ;
	initialize (in , out) ;

	return 0;
}

void openFiles(ifstream& in, ofstream& out) 
{
	in.open("GPA.txt");
	out.open("Ex3Out.txt");

	out << fixed << showpoint;
	out << setprecision(2);

	initialize(in , out);
}

void initialize(ifstream& in, ofstream& out) 
{
	char gender;
	float GPA;
	string line;
	int countFemale = 0;
	int countMale = 0;
	float sumFemaleGPA = 0;
	float sumMaleGPA = 0;

	while (!in.eof()) 
	{
		in >> gender >> GPA;
		
		if (gender == 'f')  
		{
			countFemale++; 
			sumFemaleGPA = sumFemaleGPA + GPA;
		}

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

	sumGrades(sumFemaleGPA, sumMaleGPA, in, out, countFemale, countMale);
	averageGrade(countFemale, countMale, sumFemaleGPA, sumMaleGPA, in, out);
}

void sumGrades(float& sumFemaleGPA, float& sumMaleGPA, ifstream& in, ofstream& out, int& countFemale, int& countMale) 
{
	out << "Sum female GPA: " << sumFemaleGPA << endl;
	out << "Sum male GPA: " << sumMaleGPA << endl;
}

void averageGrade(int& countFemale, int& countMale, float& sumFemaleGPA, float& sumMaleGPA, ifstream& in, ofstream& out) 
{
	float averageFemaleGPA = sumFemaleGPA / countFemale;
	float averageMaleGPA = sumMaleGPA / countMale;
	printResults(countFemale, countMale, sumFemaleGPA, sumMaleGPA, in, out, averageFemaleGPA, averageMaleGPA);
}

void printResults(int& countFemale, int&countMale, float& sumFemaleGPA, float& sumMaleGPA, ifstream& in, ofstream& out, float& averageFemaleGPA, float& averageMaleGPA) 
{
	
	out << "Female count: " << countFemale << endl;
	out << "Male count: " << countMale << endl;
	out << "Average female GPA: " << averageFemaleGPA << endl;
	out << "Average male GPA: " << averageMaleGPA;

	in.close();
	out.close();
}



In the output file I get 15 for the female counter where the actual number of enteries is 14 like the male counter . Also I want to have the input values in the output file as well ( meaning when I execute then go and check the output file I would like to see the 28 enteries and then under them the results of the calculations ) .

Thanks alot in advance

Waiting !!
Last edited on
First
The length function is fine. The problem is how you call it. The called function evaluates to some value (the one you return from it) but in your code, you don't do anything with that value. You should assign it to 'digits' digits = length(num); or to make your code shorter, print ti immediately cout << lenght(num);. Similar problem is with palindrome. You don't need to call the function twice. You're not doing anything with the first returned value, only with the second.

Second
You get 15 females because when you read the last number eof is not yet found so your program reads again and fails, but since there is no error checking between your input and calculations, you process the last input twice (this is somewhat random, I believe usually caused by an extra whitespace at the end of file).
The more clear solution is
1
2
3
4
5
while(!in.eof()){
   in >> gender >> GPA;
   if(in.fail) break;
   ...
}
The shorter solution is
1
2
3
4
while(in >> gender >> GPA){
   //no input here
   ...
}


To copy the contents of in to out, you have to reset in get pointer ( http://www.cplusplus.com/reference/iostream/istream/seekg/ ) and read/write everything in a loop ( use getline to preserve spaces )

By the way, you are using way more references than you need..
Thaaaaaaaanks a lot for the help .
You saved me !!!

Problem 1 is working fine now .
I fixed problem 2 and got the right value for the counter
as for the many references I am using
any help on how to reduce them ?



You need to use references when you assign to the arguments, call their (non constant) methods or if they are big and you want to avoid copying. There is nothing really wrong with abusing &s, but you should know when you need them and when you don't.

It is always best to take stream objects as references.
sumGrades only prints the values, so it doesn't need the references and it doesn't use the last two arguments at all.
printResults doesn't change anything so no numbers need a &. Also, it doesn't do anything with the sums.
averageGrade doesn't change anything either. Since printResults took many references, you might have wanted to have this function take the same variables as references too, but since now it doesn't , you need none.
First

you can also do the first problem with out making change in your code like hamsterman said you can instead send the variable digits by reference
int length (int num, int& digits)
this can be your function call
than you can delete line 35 and 46
Topic archived. No new replies allowed.