locked
Rookie C++ programmer needs some help!! RRS feed

  • Question

  • Hi, everyone! Sorry about the previous post.
    Im really a rookie to both languages that Im learning now. (English and C++).

    I have 5 compile errors, and I don't know how to fix it.  I try passing array into functions and reutrn the array( arry is passing by the reference so I should't return it right ?)

    error 1 :'sorting' : cannot convert parameter 1 from 'long [20]' to 'long'

    error 2 : 'search' : cannot convert parameter 1 from 'long [20]' to 'long'

    I try to find the answer from the book, but I dont understand what the error mean!!

    Also, I want to code read and write file from the function; however, the errors show

    error 3 : 'infile' : undeclared identifier

    error 4 : 'outfile' : undeclared identifier

    I trying to use function to bubble sourt and serach; however, Im not sure if they work or not. Because I couldnt get in the runing.

    Please help me with those problem. Thanks!!!!

     

     

    #include <iostream>
    
    
    
    #include <iomanip>
    
    
    
    #include <fstream>
    
    
    
    #include <cmath>
    
    
    
    using namespace std;
    
    
    
    
    
    
    
    const int SIZE = 20;	//size of array
    
    
    
    
    
    
    
    //Function prototypes
    
    
    
    void ReadFile(long, double, double, double);	// read the inflie and calculate the wage
    
    
    
    void WriteFile(long, double, double, double);	// write the data into a outfile
    
    
    
    void sorting(long, double, double, double);		// sort the data in the outfile
    
    
    
    int search(long, int);		// serach the data.
    
    
    
    
    
    
    
    
    
    
    
    int main()
    
    
    
    {
    
    
    
    	char fileName[50];		//contains the filename on the storge device
    
    
    
    	char ofileName[50];		//contains the filename to the storge device
    
    
    
    	
    
    
    
    	long EmpID[SIZE] ;		//an array holds employee identification number
    
    
    
    	double Hours[SIZE];		//an array holds the number of hour worked by each empioyee
    
    
    
    	double PayRate[SIZE];	//an array holds each employee's hourly pay rate
    
    
    
    	double Wage[SIZE];		//an array holds each employee's gross wages
    
    
    
    	int count = 0 ,result; 
    
    
    
    
    
    
    
    	//always display one decimal places
    
    
    
    	cout << fixed << showpoint << setprecision(2);
    
    
    
    	
    
    
    
    	ifstream infile;			//filename used in source code
    
    
    
    	ofstream outfile;			//filename used for output file
    
    
    
    
    
    
    
    	
    
    
    
    	//get the name of the input file on the storage device
    
    
    
    	cout << "Please enter the name of the input file: ";
    
    
    
    	cin >> fileName;
    
    
    
    	infile.open(fileName);
    
    
    
    	cout << endl;
    
    
    
    
    
    
    
    	// call function to read the file
    
    
    
    	ReadFile(EmpID[SIZE], Hours[SIZE], PayRate[SIZE], Wage[SIZE]);
    
    
    
    
    
    
    
    	//write the data in to a output file
    
    
    
    	cout << "Please enter the name of the output file: ";
    
    
    
    	cin >> ofileName;
    
    
    
    	outfile.open(ofileName);
    
    
    
    	cout << endl;
    
    
    
    
    
    
    
    	//call function to write data to a outfile
    
    
    
    	WriteFile(EmpID[SIZE], Hours[SIZE], PayRate[SIZE], Wage[SIZE]);
    
    
    
    
    
    
    
    	//call sorting function
    
    
    
    	sorting(EmpID, Hours, PayRate, Wage);
    
    
    
    
    
    
    
    	//call serach function
    
    
    
    	search(EmpID, result);
    
    
    
    	
    
    
    
    	// Error message for the search
    
    
    
    	
    
    
    
    	result = search(EmpID,result);
    
    
    
    
    
    
    
    	if (result == -1)
    
    
    
    		cout << "ERROR! The number you entered is not valid.\n";
    
    
    
    	else
    
    
    
    	{
    
    
    
    		outfile << "The number you entered is valid.\n";
    
    
    
    		outfile << "Employee " << EmpID[result] << " worked ";
    
    
    
    		outfile << Hours[result] << " hours at $" << PayRate[result];
    
    
    
    		outfile << " per hour and earned $" << Wage[result] << ".\n";
    
    
    
    	}
    
    
    
    
    
    
    
    	// Print programmer name
    
    
    
    	cout << "Programmer: Iris Wang\n";
    
    
    
    	
    
    
    
    return 0;
    
    
    
    }
    
    
    
    
    
    
    
    //***********************************************
    
    
    
    //	Definition of function ReadFile.			*
    
    
    
    //	Read into the file and calculate the wage	*
    
    
    
    // Calculate the wage for each employee		*
    
    
    
    //***********************************************
    
    
    
    
    
    
    
    void ReadFile(long EmpID[], double Hours[], double PayRate[], double Wage[])
    
    
    
    {
    
    
    
    		int count = 0;
    
    
    
    		infile >>EmpID[count];
    
    
    
    		while (!(EmpID[count] == -1))
    
    
    
    		{
    
    
    
    			infile >> Hours[count] >> PayRate[count];		//read the array
    
    
    
    			Wage[count]= Hours[count]* PayRate[count];		// calculate the wage
    
    
    
    			count++;										//update the increment
    
    
    
    			infile >> EmpID[count];
    
    
    
    		}
    
    
    
    	
    
    
    
    }
    
    
    
    
    
    
    
    		
    
    
    
    //***********************************************
    
    
    
    //	Definition of function WriteFile.			*
    
    
    
    //	Write into the file							*
    
    
    
    //***********************************************
    
    
    
    
    
    
    
    void WriteFile(long EmpID[], double Hours[], double PayRate[], double Wage[])
    
    
    
    {
    
    
    
    		
    
    
    
    		outfile << "\tPAYROLL PROCESSING\n\n";
    
    
    
    		outfile << "Employees in order entered:\n\n";
    
    
    
    		outfile << "Employee Number\t\tHours Worked\t\tPayRate Per Hour\t\tWages\n";
    
    
    
    
    
    
    
    		for(int count = 0;EmpID[count]!=-1 && count < SIZE; count++)
    
    
    
    		{
    
    
    
    			outfile << EmpID[count] << right << setw(24) << Hours[count] << right << setw(24) << PayRate[count];
    
    
    
    			outfile << right << setw(30) << Wage[count] <<endl;
    
    
    
    		}
    
    
    
    	// Print programmer name
    
    
    
    	outfile << "Programmer: Iris Wang\n";
    
    
    
    }
    
    
    
    
    
    
    
    
    
    
    
    //***********************************************
    
    
    
    //	Definition of function sourting				*
    
    
    
    //	serach the data in the outfile				*
    
    
    
    //***********************************************
    
    
    
    
    
    
    
    void sorting(long EmpID[], double Hours[], double PayRate[], double Wage[])	
    
    
    
    {
    
    
    
    	double temp;//Hours,tempPay,tempWage;
    
    
    
    	int Id;
    
    
    
    	int i;
    
    
    
    	bool swap;
    
    
    
    
    
    
    
    		do 
    
    
    
    		{ 
    
    
    
    			swap = false;
    
    
    
    			for( i=0; i< (SIZE-1); i++)
    
    
    
    			{
    
    
    
    					if(EmpID[i] > EmpID[i+1])
    
    
    
    					{
    
    
    
    					Id = EmpID[i];
    
    
    
    					EmpID[i] = EmpID[i + 1];
    
    
    
    					EmpID[i+ 1] = Id;
    
    
    
    
    
    
    
    					temp = Hours[i];
    
    
    
    					Hours[i] = Hours[i + 1];
    
    
    
    					Hours[i + 1] = temp;
    
    
    
    
    
    
    
    					temp = PayRate[i];
    
    
    
    					PayRate[i] = PayRate[i + 1];
    
    
    
    					PayRate[i + 1] = temp;
    
    
    
    
    
    
    
    					temp = Wage[i];
    
    
    
    					Wage[i] = Wage[i + 1];
    
    
    
    					Wage[i + 1] = temp;
    
    
    
    
    
    
    
    					swap = true;
    
    
    
    					}
    
    
    
    			}
    
    
    
    		}while (swap);
    
    
    
    
    
    
    
    		cout << "\nEmployees sorted on identification numbers:\n";
    
    
    
    		cout << "Employee Number\t\tHours Worked\t\tPayRate Per Hour\t\tWages\n";
    
    
    
    		for(int t = 0; t < SIZE; t++)
    
    
    
    		{
    
    
    
    			cout << EmpID[t] << right << setw(24) << Hours[t] << right << setw(24) << PayRate[t];
    
    
    
    			cout << right << setw(30) << Wage[t] <<endl;
    
    
    
    		}
    
    
    
    	// Print programmer name
    
    
    
    	outfile << "Programmer: Iris Wang\n";
    
    
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    //***********************************************
    
    
    
    //	Definition of function serach.				*
    
    
    
    //	serach the data in the outfile				*
    
    
    
    //***********************************************
    
    
    
    
    
    
    
    int search(long EmpID[], int value)
    
    
    
    {
    
    
    
    int first = 0,last = SIZE-1,middle;	
    
    
    
    		int position= -1;
    
    
    
    		bool found = false;
    
    
    
    		cout << "Enter an employee number for your search: ";
    
    
    
    		cin >> value;
    
    
    
    
    
    
    
    	while (!found && first <= last)
    
    
    
    	{
    
    
    
    		middle = (SIZE-1) / 2;	//Calculate midpoint
    
    
    
    		if (EmpID[middle] == value)	//If value found at midpoint
    
    
    
    		{
    
    
    
    			found = true;
    
    
    
    			position = middle;
    
    
    
    		}
    
    
    
    		else if (EmpID[middle] > value)	//If value is in lower half
    
    
    
    			last = middle - 1;
    
    
    
    		else								//If value is in upper half
    
    
    
    			first = middle + 1;
    
    
    
    	}
    
    
    
    return position;
    
    
    
    }
    
    
    
    
    
    
    
    
    __________________________________________
    Thanks for everyone's help!!!

    This is an assignment that i found on the internet.

    Since, Im learning C++ from the book, this looks good for me to try it out.

    I just want to see if I do get the point form the book.

    I still fixint the functions.

    By the way, i think nobody @ my work will answer my question because Im working @ a SPA and is HER question!!!!!! ^^

    • Edited by IQueen Thursday, December 9, 2010 2:35 PM
    Wednesday, December 8, 2010 1:58 PM

Answers

  • Thanks for your help and you are right , this is an assignment that i found on the internet.

    Since, Im learning C++from the book, this looks good for me to try it out.

    I juast want to see if I do get the point form the book.

    Oh, ok.  Then by all means, I'll be glad to help.  Did you understand what I meant by the prototype not matching?  See, your prototype looks like this:

    int search(long, int);
    
    and your function declaration looks like this:

    int search(long EmpID[], int value)
    

    they don't match because when you put the [] after EmpID, you are saying that EmpID is an array of 'longs'.  What this means is that EmpID is also a pointer to type 'long'.  So you need to change your prototype (the first line) to this:

    int search(long *, int);
    

    note the "*".  That tells the compiler that the first parameter is a pointer.  This will remove two of your compiler errors (do the same thing for the sorting function).

     

     

    Jeff

    • Proposed as answer by mr_jeff Monday, December 13, 2010 6:33 PM
    • Marked as answer by lucy-liu Wednesday, December 15, 2010 5:25 AM
    Thursday, December 9, 2010 3:09 PM

All replies

  • I don't think you will find anyone who will take that code, run it and debug it for you.

    You need to say what the problem you are facing is, providing as much information as possible and, at the very least, format the code a bit better.


    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
    Visit my (not very good) blog at
    http://ccprogramming.wordpress.com/
    Wednesday, December 8, 2010 3:38 PM
  • Yup, this rookie needs to learn how to post a question. Describe the problem you are facing. Do you have a compile error? a link error? If so, what is the error message? What line of code does it pertain to? Or are you having a problem running? If so, what is the behaviour, and how does that differ from what you are expecting?

    If you need to post code, use the [</>] icon on the toolbar.

    Wednesday, December 8, 2010 4:48 PM
  • thanks for the [</>] !! ^^
    Wednesday, December 8, 2010 6:17 PM
  • The code insertion tool has been somewhat borked lately. I have been 'dipping' the source into Word using the clipboard...it works well.

    1. Select the source in VisualStudio and copy it to the clipboard.

    2. Open Word and paste it into a blank document.

    3. Use Ctl-A and Ctl-C to copy it back to the clipboard.

    4. Paste it into the body as before.

     

    Wednesday, December 8, 2010 6:24 PM
  • You need to reorder your functions. Move the main() function after the definition of the sorting() and search(). As an alternative, you could define a function prototype before the main() function, that looks like this:

    void sorting(long EmpIDp[], double Hours[], double PayRate[], double Wage[]);

    Your other error has to do with the fact that infile and outfile are variables inside main(), and are not visible by other functions. You can make these global variables instead by moving definitions outside the main() function.

    As a final note, please don't revise your original post in the manner you did in this thread. The edit function is there for fixing minor spelling and grammatical mistakes. Now the following post by Crescens2k and myself no longer make sense. It would have been better to simply add a new post at the end with your corrections.

     

    Wednesday, December 8, 2010 6:39 PM
  • Well, search() is declared to have it's first parameter be a 'long' but you are passing it a pointer.  I'd comment further but this code looks suspiciously like it's from a homework assignment.  Does anyone in the real world need to write an app like this?  My apologies if I am being overly negative, but I spent alot of time on a Java message board and homework questions from college students would appear all the time.
    Wednesday, December 8, 2010 6:40 PM
  • Well, search() is declared to have it's first parameter be a 'long' but you are passing it a pointer. 
    Wrong, mr_jeff. Did you actually look at the code?
    Wednesday, December 8, 2010 6:43 PM
  • Well, search() is declared to have it's first parameter be a 'long' but you are passing it a pointer. 
    Wrong, mr_jeff. Did you actually look at the code?

    Yes, and I saw this:

    int
     search(long
    , int
    );		// serach the data.
    
    

    So that's why I say "search() is declared to have it's first parameter be a 'long'"

    and then I saw

    //call serach function
    
    	search(EmpID, result);
    

    and EmpID is an array, which is why I say "but you are passing it a pointer".

     

    So how was I wrong????  Certainly the prototype doesn't match the declaration, since if you look at the function he wrote it expects a

    int
     search(long
     EmpID[], int
     value
    )
    

    but the compiler will look at the prototype when type-checking for errors, not the function itself.

    Wednesday, December 8, 2010 6:53 PM
  • Of course, the issue of prototype not matching the declaration is another thing, but like I said, this looks like a student assignment.  What company would hire someone with no C++ experience to write C++ code?  And even if they did, there should be other programmers there who can easily answer his question in 3 seconds.  But sorting and searching a trivial employee app...sounds like someone's in Computer Science 101 somewhere....
    Wednesday, December 8, 2010 7:05 PM
  • As a final note, please don't revise your original post in the manner you did in this thread. The edit function is there for fixing minor spelling and grammatical mistakes. Now the following post by Crescens2k and myself no longer make sense. It would have been better to simply add a new post at the end with your corrections.

     

    Sorry~ I will learn form this~ Thanks for your help!!! ^^

    Thursday, December 9, 2010 4:32 AM
  • Hello All,

    Thanks to all the enthu participants in the thread... but instead of fighting and complaining, it wud hav been better, if we cud answer precisely with putting the comments on the way of posing the que. as an improvement.. Another thing i wud like to say, v shud not be worried about the usefulness of que... its a problem for somebody, so he is asking the que... its a silly things for me.. i will answer.. thats all matter finish... y to think other things... :)

    Hopefully i hav been polite wid my words... nothing personal :):) enjoy pgmg...

    Thursday, December 9, 2010 12:09 PM
  • Hello All,
     
    Thanks to all the enthu participants in the thread... but instead of fighting and complaining, it wud hav been better, if we cud answer precisely with putting the comments on the way of posing the que. as an improvement.. Another thing i wud like to say, v shud not be worried about the usefulness of que... its a problem for somebody, so he is asking the que... its a silly things for me.. i will answer.. thats all matter finish... y to think other things... :)
     
    Hopefully i hav been polite wid my words... nothing personal :):) enjoy pgmg...
    I am not sure what you are saying here, but Brian was not complaining about the question. He was complaining about editing the question after it had been answered, thus rendering the answers irrelevant.
     
    Personally, I think we would be better off without the editing feature.
     

    David Wilkinson | Visual C++ MVP
    Thursday, December 9, 2010 12:36 PM
  • Of course, the issue of prototype not matching the declaration is another thing, but like I said, this looks like a student assignment.  What company would hire someone with no C++ experience to write C++ code?  And even if they did, there should be other programmers there who can easily answer his question in 3 seconds.  But sorting and searching a trivial employee app...sounds like someone's in Computer Science 101 somewhere....

    Thanks for your help and you are right , this is an assignment that i found on the internet.

    Since, Im learning C++from the book, this looks good for me to try it out.

    I juast want to see if I do get the point form the book.

     

     

    Thursday, December 9, 2010 2:27 PM
  • Thanks for your help and you are right , this is an assignment that i found on the internet.

    Since, Im learning C++from the book, this looks good for me to try it out.

    I juast want to see if I do get the point form the book.

    Oh, ok.  Then by all means, I'll be glad to help.  Did you understand what I meant by the prototype not matching?  See, your prototype looks like this:

    int search(long, int);
    
    and your function declaration looks like this:

    int search(long EmpID[], int value)
    

    they don't match because when you put the [] after EmpID, you are saying that EmpID is an array of 'longs'.  What this means is that EmpID is also a pointer to type 'long'.  So you need to change your prototype (the first line) to this:

    int search(long *, int);
    

    note the "*".  That tells the compiler that the first parameter is a pointer.  This will remove two of your compiler errors (do the same thing for the sorting function).

     

     

    Jeff

    • Proposed as answer by mr_jeff Monday, December 13, 2010 6:33 PM
    • Marked as answer by lucy-liu Wednesday, December 15, 2010 5:25 AM
    Thursday, December 9, 2010 3:09 PM
  • I think we would be better off without the editing feature.

    Maybe MS forums could borrow from some excellent code review tools for web (Fogbugz?), or at least, snippet-posting sites ( pastie.org,  gist.github.com) ? The  [</>]  thing is better than nothing... but looks so XX century.

    --pa 

    Thursday, December 9, 2010 4:26 PM
  • mr_jeff, I just wanted to say that your subsequent posts clarified your thinking, and you gave some excellent answers.
    Thursday, December 9, 2010 5:47 PM
  • mr_jeff, I just wanted to say that your subsequent posts clarified your thinking, and you gave some excellent answers.

    Thanks.  I'm hoping to end up as an MVP on these MSFT boards someday.  I might as well ask, what do these gold medals under our name mean?  I usually see MVPs with 5 of them.  Somehow I got 1, but I'm not really sure how I even got it.
    Thursday, December 9, 2010 6:00 PM
  • I might as well ask, what do these gold medals under our name mean? 
    http://social.msdn.microsoft.com/Forums/en-US/help#310
    Thursday, December 9, 2010 6:50 PM
  • ohh~~ Thanks Jeff,

    after I take look you early's post i do find that mistake!  Thank for your help!!! ^^

    Saturday, December 11, 2010 10:52 PM