Help cleaning code for better console presentation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Help cleaning code for better console presentation

  1. #1
    Join Date
    Nov 2013
    Posts
    21

    Help cleaning code for better console presentation

    Hi guys,

    I have this function and it works, the problem is that it couts a bunch of stuff that I don't need. What this functions does asks the user to input a job (show) and if the show is found to list all the talents (actors/actresses). If the show is not found, to print "show not found". I have the function working just need to present it better. What I want the function to do is just print "show not found" and nothing else. How I have it currently will print:

    Thanks




    Notice how its outputting all the unnecessary information. If the job is found should look like this:



    Uploaded with ImageShack.us


    Code:
    void TalentsByJob(Agency Talents[], int limit)
    {
    	system("CLS");
    
    	string show;
    	cout << "Enter the Job: ";
    	cin >> show;
    	bool found = false;
    
    	cout << "List of Talents for the Job: " << show << endl;
    	cout << "\nTalent\t\t" << "\tPay\n" << endl;
    
    	for (int i = 0; i < limit; i++)
    	{
    		if(Talents[i].job == show)
    		{
    			cout << Talents[i].talent << "\t\t\t" << Talents[i].pay << endl;
    			found = true;
    		}
    	}
    
    	cout << "\nTotal amount paid for the Job: " << findSum(Talents, limit, show) << endl;
    	cout << "Highest paid Talent: " << findMax(Talents, limit, show) << endl;
    	cout << "Lowest paid Talent: " << findMin(Talents, limit, show) << endl;
    	cout << "Average amount paid for the Job: " << findAvg(Talents, limit, show) << endl;
    	cout << endl;
    
    	if(!found)
    		cout << "\nJob not found!\n" << endl;
    
    	system("PAUSE");
    	system("CLS");
    }

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,854

    Re: Help cleaning code for better console presentation

    What's the problem? If found is true then display the stats for the found job. If found is false, just output job not found. Output the heading if the job is found and found is not true.

    This is simple program design logic.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Nov 2013
    Posts
    21

    Re: Help cleaning code for better console presentation

    The function works its just I don't want so much output if the job is not found.

    displaying the max, min and avg and total is not necessary since the job is not found in the database.

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,854

    Re: Help cleaning code for better console presentation

    Quote Originally Posted by shyy View Post
    The function works its just I don't want so much output if the job is not found.

    displaying the max, min and avg and total is not necessary since the job is not found in the database.
    That's what I was talking about in my post #2. Only output what is required. It comes down to program design again.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Nov 2013
    Posts
    21

    Re: Help cleaning code for better console presentation

    Where I am getting confused is when I put in the for loop in the beginning. If I put both

    Code:
    	cout << "List of Talents for the Job: " << show << endl;
    	cout << "\nTalent\t\t" << "\tPay\n" << endl;
    in the if statement where its found = true, it would cout x amount of times those two sentences. That is why I placed them on top.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,229

    Re: Help cleaning code for better console presentation

    Quote Originally Posted by shyy View Post
    Where I am getting confused is when I put in the for loop in the beginning. If I put both

    Code:
    	cout << "List of Talents for the Job: " << show << endl;
    	cout << "\nTalent\t\t" << "\tPay\n" << endl;
    in the if statement where its found = true, it would cout x amount of times those two sentences. That is why I placed them on top.
    You know if the job is found or not and you know how to use if statements. Use those two pieces of knowledge to conditionally output whatever you want to.

    Code:
    if(found)
    {
        output something
    }
    else
    {
        output something else
    }

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,854

    Re: Help cleaning code for better console presentation

    Quote Originally Posted by shyy View Post
    Where I am getting confused is when I put in the for loop in the beginning. If I put both

    Code:
    	cout << "List of Talents for the Job: " << show << endl;
    	cout << "\nTalent\t\t" << "\tPay\n" << endl;
    in the if statement where its found = true, it would cout x amount of times those two sentences. That is why I placed them on top.
    The first step, as I've said in another post, is to produce a design before you start coding. It's no good just trying to 'fix' some code that isn't doing what you want. You need to design first, then code the design, then test/debug as required.

    For the moment, forget the code. Design the function in English (or other natural language) to say how you would do what is required if you were doing it yourself using pen and paper. Only when you have a design that you are satisfied with do you then code the function from the design.

    In this case, the design/code is fairly trivial and IMO I would have expected no problems with it if you are going to sit a final exam shortly.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Nov 2013
    Posts
    21

    Re: Help cleaning code for better console presentation

    Am I getting close with the syntax?

    Code:
    void TalentsByJob(Agency Talents[], int limit)
    {
    	system("CLS");
    
    	string show;
    	cout << "Enter the Job: ";
    	cin >> show;
    	bool found = false;
    
    	for (int i = 0; i < limit; i++)
    	
    		if(Talents[i].job == show)		
    			found = true;
    		else
    			found = false;
    
    	if(found == true)
    	{
    		cout << "Found";
    	}
    	else
    	{
    		cout << "not Found";
    	}
    
    	system("PAUSE");
    	system("CLS");
    }

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,229

    Re: Help cleaning code for better console presentation

    Kind of, but you have a pretty big logic bug in your for loop that wasn't in your first post.

  10. #10
    Join Date
    Nov 2013
    Posts
    21

    Re: Help cleaning code for better console presentation

    is it how I am implementing the found? My biggest problem is with the header, how to not get it to repeat but only show if the show is found. I don't know how to implement that, since the for loop will always loop the header.

  11. #11
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,854

    Re: Help cleaning code for better console presentation

    Whay happens if you move the header output into the for loop, but only output it if show is found and header has not already been output?

    In your if-statement within the for loop, what happens to found if some elements of the array match show and then some more don't?

    Would it output found or not-found?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  12. #12
    Join Date
    Nov 2013
    Posts
    21

    Re: Help cleaning code for better console presentation

    What I have so far

    Code:
    void TalentsByJob(Agency Talents[], int limit)
    {
    	system("CLS");
    
    	string show;
    	cout << "Enter the Job: ";
    	cin >> show;
    	bool found = false;
    
    	for (int i = 0; i < limit; i++)
    	{
    
    		if(Talents[i].job == show)		
    		{
    			found = true;
    		}
    		else
    		{
    			found = false;
    		}
    	}
    
    	if(!found)
    	{
    		cout << "List of Talents for the Job: " << show << endl;
    		cout << "\nTalent\t\t" << "\tPay\n" << endl;
    
    		for (int i = 0; i < limit; i++)
    			if(Talents[i].job == show)	
    				cout << Talents[i].talent << "\t\t\t" << Talents[i].pay << endl;
    	}
    	else
    	{
    		cout << "Job not Found\n";
    	}
    
    	system("PAUSE");
    	system("CLS");
    }

  13. #13
    Join Date
    Nov 2013
    Posts
    21

    Re: Help cleaning code for better console presentation

    Why is it if I put if(found == true) I get not found for both if its found and not found?

  14. #14
    Join Date
    Nov 2013
    Posts
    21

    Re: Help cleaning code for better console presentation

    Got it working! Thanks guys!

    Code:
    void TalentsByJob(Agency Talents[], int limit)
    {
    	system("CLS");
    
    	string show;
    	cout << "Enter the Job: ";
    	cin >> show;
    	bool found = false;
    
    	for (int i = 0; i < limit; i++)
    	{
    
    		if(Talents[i].job == show)		
    		{
    			found = true;
    		}
    		else
    		{
    			
    		}
    	}
    
    	if(found == true)
    	{
    		cout << "List of Talents for the Job: " << show << endl;
    		cout << "\nTalent\t\t" << "\tPay\n" << endl;
    
    		for (int i = 0; i < limit; i++)
    			if(Talents[i].job == show)	
    				cout << Talents[i].talent << "\t\t\t" << Talents[i].pay << endl;
    
    		cout << "\nTotal amount paid for the Job: " << findSum(Talents, limit, show) << endl;
    		cout << "Highest paid Talent: " << findMax(Talents, limit, show) << endl;
    		cout << "Lowest paid Talent: " << findMin(Talents, limit, show) << endl;
    		cout << "Average amount paid for the Job: " << findAvg(Talents, limit, show) << endl;
    		cout << endl;
    	}
    	else 
    	{
    		cout << "Job not Found\n";
    	}
    
    	system("PAUSE");
    	system("CLS");
    }

  15. #15
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,854

    Re: Help cleaning code for better console presentation

    Yes - but that code is inefficient as you are iterating Talents twice rather than the one needed.

    Code:
    void TalentsByJob(const Agency Talents[], const int limit)
    {
    string show;
    
    	cout << "Enter the Job: ";
    	cin >> show;
    
    bool found = false;
    
    	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;
    			found = true;
    		}
    	}
    
    	if(found == true)
    	{
    		cout << "\nTotal amount paid for the Job: " << findSum(Talents, limit, show) << endl;
    		cout << "Highest paid Talent: " << findMax(Talents, limit, show) << endl;
    		cout << "Lowest paid Talent: " << findMin(Talents, limit, show) << endl;
    		cout << "Average amount paid for the Job: " << findAvg(Talents, limit, show) << endl;
    		cout << endl;
    	}
    	else 
    	{
    		cout << "Job not Found\n";
    	}
    }
    Also, as you are not altering the contents of Talents, it is better practice to mark as const - so any unintential change is flagged up by the compiler.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center