| | |
Program wont work (Need help with parameter passing)
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
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)
I am only getting 3 Warnings right now and it is for unitialized variables (in_val, odd_tally, even_tally)
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; void examine(int, int, int); void print(int &, int &); void readFile(ifstream&, int); int odd(int); int even(int); int main() { int in_val, odd_tally, even_tally; ifstream dataIn; dataIn.open("prj4.txt"); //Open File if (dataIn.fail()){ //Failure opening file cout << "Error opening data file.\n"; } else { readFile(dataIn, in_val); 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 } } int odd(int odd_tally) { return odd_tally + 1; //Everytime a odd integer is found, add 1 to odd_tally. } int even(int even_tally) { return even_tally + 1; //Everytime a even integer is found, add 1 to even_tally. } void print(int &odd_tally,int &even_tally) //Display the total number of even and odd integers in the file. { cout << "There are " << even_tally << " even integers in the file."; cout << "There are " << odd_tally << " odd integers in the file."; cout << endl; }
Last edited by acdougla17; Nov 9th, 2009 at 12:29 pm.
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:
C++ Syntax (Toggle Plain Text)
examine(num, odd, even);
C++ Syntax (Toggle Plain Text)
examine(in_val, odd_tally, even_tally);
![]() |
Similar Threads
- Help! My mouse wont work! (USB Devices and other Peripherals)
- Program Won't Work With Vista (C#)
- Why wont this work!!! (C++)
- My Program Wont Work!! Help!!!!! (C++)
- My c++ cgi-bin program wont allow access to files (C++)
- Loop Program Wont work right..Help me please! (C)
- Why won´t it ever work!? (Troubleshooting Dead Machines)
- Restart/shutdown&ctrl+alt+del wont work (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: How to call the function to run the options
- Next Thread: Static vector losing its contents
Views: 573 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for calling, file, functions, parameter, passing, read
.net @param ajax applicatio array arrays assembly association associations binary bmp c# c++ calling change chmod chooser chrelad classes codeblocks combobox comma command compression console css csv cursor data database definedlines deserialized download edit error events executable file file-dialog filechooser fstream ftp function functions handling htaccess if-else ifstream image include input javadoc javascript kernel lines linux list match mysql open output parallel parameter parameters passing php pointer pong position print procedure process program programming properties python read reading reference remote request.getparameter saving sequential snakes sources speed store stored stream string text text-file upload url values variable view virtual web write






