Program wont work (Need help with parameter passing)

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2009
Posts: 1
Reputation: acdougla17 is an unknown quantity at this point 
Solved Threads: 0
acdougla17 acdougla17 is offline Offline
Newbie Poster

Program wont work (Need help with parameter passing)

 
0
  #1
Nov 9th, 2009
OK, so I have this project to do and cant figure it out. I am not positive on calling functions correctly and I am kind of lost when it comes to parameter passing. So my program needs to read a text file 1 integer at a time, find out if it is odd or even, add 1 to either odd_tally or even_tally, and then display the total # of even and odd integers.

I am only getting 3 Warnings right now and it is for unitialized variables (in_val, odd_tally, even_tally)

  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. void examine(int, int, int);
  5. void print(int &, int &);
  6. void readFile(ifstream&, int);
  7. int odd(int);
  8. int even(int);
  9.  
  10. int main()
  11. {
  12. int in_val, odd_tally, even_tally;
  13. ifstream dataIn;
  14.  
  15. dataIn.open("prj4.txt"); //Open File
  16.  
  17.  
  18. if (dataIn.fail()){ //Failure opening file
  19. cout << "Error opening data file.\n";
  20. }
  21. else
  22. { readFile(dataIn, in_val);
  23. while (!dataIn.eof()) //Loop until end of text file
  24. {
  25. examine(in_val, odd_tally, even_tally); //Call examine function that will determine if integer s odd or even.
  26. print(odd_tally, even_tally); //Call function to display results
  27. }
  28. dataIn.close();
  29. }
  30. return 0;
  31. }
  32.  
  33.  
  34. void readFile(ifstream &someFile, int txt_int)
  35. {
  36. while (someFile >> txt_int); //Read File
  37. cout << endl;
  38. }
  39.  
  40. void examine(int num,int odd,int even)
  41. {
  42. if (num % 2 ==0)
  43. {
  44. int even(even); //Call even function if num is even
  45. }
  46. else
  47. {
  48. int odd(odd); //Call odd function if num is odd
  49. }
  50. }
  51.  
  52. int odd(int odd_tally)
  53. {
  54. return odd_tally + 1; //Everytime a odd integer is found, add 1 to odd_tally.
  55. }
  56.  
  57. int even(int even_tally)
  58. {
  59. return even_tally + 1; //Everytime a even integer is found, add 1 to even_tally.
  60. }
  61.  
  62. void print(int &odd_tally,int &even_tally) //Display the total number of even and odd integers in the file.
  63. {
  64. cout << "There are " << even_tally << " even integers in the file.";
  65. cout << "There are " << odd_tally << " odd integers in the file.";
  66. cout << endl;
  67. }
Last edited by acdougla17; Nov 9th, 2009 at 12:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 140
Reputation: restrictment is on a distinguished road 
Solved Threads: 10
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster
 
0
  #2
Nov 9th, 2009
	while (!dataIn.eof())                                   //Loop until end of text file
				{
					examine(in_val, odd_tally, even_tally); //Call examine function that will determine if integer s odd or even.
					print(odd_tally, even_tally);			//Call function to display results
				}
				dataIn.close();
			}
	return 0;
}
 
 
void readFile(ifstream &someFile, int txt_int)
{ 
    while (someFile >> txt_int); 					//Read File
	cout << endl;
}
 
void examine(int num,int odd,int even)
{	
	if (num % 2 ==0)                        
		 {
			int even(even);                 //Call even function if num is even
		 }
		else
		 {
			int odd(odd);                  //Call odd function if num is odd
		 }
}

Your parameters are not the same here. The appropriate argument would be:
  1. examine(num, odd, even);
not:
  1. examine(in_val, odd_tally, even_tally);
Reply With Quote Quick reply to this message  
Reply


Message:




Views: 573 | Replies: 1
Thread Tools Search this Thread



Tag cloud for calling, file, functions, parameter, passing, read
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC