CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Join Date
    Nov 2013
    Posts
    21

    Re: Help cleaning code for better console presentation

    Thanks for the suggestion, I replaced what with what you put and got a bunch of errors that dealth with Agency. I replaced both the top and bottom to match void TalentsByJob(const Agency Talents[], const int limit)

  2. #17
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Help cleaning code for better console presentation

    It is also inefficient to have the various find functions used in TalentsByJobs() as this means iterating over the array again for each one. It would be more efficient to calculate these values as part of TalentsByJobs so that only one iteration is required. Something like

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    struct Agency {
    	string talent;
    	string job;
    	double pay;
    };
    
    const string iname = "talent.txt";
    
    const int MAXSIZE = 100;
    
    string findMaxJob(const Agency Talents[], const int limit);
    void TalentsByJob(const Agency Talents[], const int limit);
    
    int main()
    {
    Agency talents[MAXSIZE];
    
    int size;
    
    	ifstream inf(iname.c_str());
    
    	if (!inf.is_open()) {
    		cout << "Cannot open input file: " << iname << endl;
    		return 1;
    	}
    
    	for (size = 0; size < MAXSIZE && (inf >> talents[size].talent >> talents[size].job >> talents[size].pay); size++);
    
    	cout << "Best show is " << findMaxJob(talents, size) << endl;
    	TalentsByJob(talents, size);
    	return 0;
    }
    
    void TalentsByJob(const Agency Talents[], const int limit)
    {
    string show;
    
    	cout << "Enter the Job: ";
    	cin >> show;
    
    bool found = false;
    
    int	notalent = 0;
    
    double amount = 0.0;
    double hamt = 0.0;
    double lamt = 999999999.0;
    
    string highest;
    string lowest;
    
    	for (int i = 0; i < limit; i++)
    	{
    		if(Talents[i].job == show)		
    		{
    			if (found == false)
    			{
    				cout << "List of Talents for the Job: " << show << endl;
    				cout << "\nTalent\t\t" << "\tPay\n" << endl;
    			}
    
    			cout << Talents[i].talent << "\t\t\t" << Talents[i].pay << endl;
    			amount += Talents[i].pay;
    			notalent++;
    
    			if (Talents[i].pay > hamt) {
    				hamt = Talents[i].pay;
    				highest = Talents[i].talent;
    			}
    
    			if (Talents[i].pay < lamt) {
    				lamt = Talents[i].pay;
    				lowest = Talents[i].talent;
    			}
    
    			found = true;
    		}
    	}
    
    	if (found == true)
    	{
    		cout << "\nTotal amount paid for the Job: " << amount << endl;
    		cout << "Highest paid Talent: " << highest << endl;
    		cout << "Lowest paid Talent: " << lowest << endl;
    		cout << "Average amount paid for the Job: " << amount / notalent << endl;
    		cout << endl;
    	}
    	else 
    	{
    		cout << "Job not Found\n";
    	}
    }
    
    string findMaxJob(const Agency Talents[], const int limit)
    {
    string curjob = Talents[0].job;
    string maxjob;
    
    double maxtot = 0.0;
    double curtot = 0.0;
    
    	for (int i = 0; i < limit; i++) {
    		if (Talents[i].job == curjob) {
    			curtot += Talents[i].pay;
    		} else {
    			if (curtot > maxtot) {
    				maxtot = curtot;
    				maxjob = curjob;
    			}
    			curtot = Talents[i].pay;
    			curjob = Talents[i].job;
    		}
    	}
    
    	if (curtot > maxtot)
    		maxjob = curjob;
    
    	return maxjob;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #18
    Join Date
    Nov 2013
    Posts
    21

    Re: Help cleaning code for better console presentation

    For our class its good that we show bunch of functions. Thanks for the info, I will dissect and study from this.

  4. #19
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Help cleaning code for better console presentation

    Quote Originally Posted by shyy View Post
    For our class its good that we show bunch of functions.
    But only where functions are warranted. Having a function for the sake of having a function is not good. This comes back to program design again.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #20
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Help cleaning code for better console presentation

    Quote Originally Posted by 2kaud View Post
    It is also inefficient to have the various find functions used in TalentsByJobs() as this means iterating over the array again for each one. It would be more efficient to calculate these values as part of TalentsByJobs so that only one iteration is required. Something like

    Code:
    	return maxjob;
    }
    You just can't help yourself, can you. You should at least charge if you're going to do people's work for them.

  6. #21
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Help cleaning code for better console presentation

    You just can't help yourself, can you. You should at least charge if you're going to do people's work for them.
    Its not an assessment. The OP is trying to learn for an exam on Saturday. Reading and understanding code is one way of becoming a better programmer which we should be encouraging.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #22
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Help cleaning code for better console presentation

    Quote Originally Posted by 2kaud View Post
    Its not an assessment. The OP is trying to learn for an exam on Saturday. Reading and understanding code is one way of becoming a better programmer which we should be encouraging.
    He would learn more by being led to the solution and figuring it out himself, rather than having it spoon fed to him.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured