Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-15-2006, 06:53 AM   #1 (permalink)
krisl100
krisl100
 
Join Date: Oct 2006
Location: Montreal
Posts: 34
krisl100 is on a distinguished road
remove duplicate code & pass counter

In my last excercise I have an IF/Else statement & a second pass counter, here is the code:

How can I remove the second pass counter (++pass) & the duplicate line of code? I just need a very simple solution. Thanks.
Code:
//Students are allowed to fail one exam, not both, this is how we achieve that goal.
		if ((midterm < EXAMSPASS && final < EXAMSPASS) || course < PASSMARK)
		{
			fail++;
			cout << endl << "RESULT: " << name << " " << "Failed " << endl;
			cout << "-------------------------------------------" << endl;
		}
		else if (course >= HONOURS)
		{
			pass++;
			cout << endl << "RESULT: " << name << " " << "Passed with Honours " 
				 << endl;
			cout << "-------------------------------------------" << endl;

		}
		
		else
		{
			//Since pass+1 is set above, need to reset the counter this way
			//so it counts the passed results correctly
			++pass;
			cout << endl << "RESULT: " << name << " " << "Passed " << endl;
			cout << "-------------------------------------------" << endl;
		}

Last edited by redhead; 10-15-2006 at 10:07 AM.
krisl100 is offline   Reply With Quote
Old 10-15-2006, 10:12 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
hmm.. from what I can read out of your code part here, shouldn't this be enough ?
Code:
if ((midterm < EXAMSPASS && final < EXAMSPASS) || course < PASSMARK)
		{
			fail++;
			cout << endl << "RESULT: " << name << " " << "Failed " << endl;
			cout << "-------------------------------------------" << endl;
		}
		else
		{  /** if they don't fit the fail part, they pass **/ 
			pass++;
			cout << endl << "RESULT: " << name << " " << "Passed";

			if(course >= HONOURS)
			    cout << " with Honours ";
			cout << endl;
			cout << "-------------------------------------------" << endl;

		}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-15-2006, 10:22 AM   #3 (permalink)
krisl100
krisl100
 
Join Date: Oct 2006
Location: Montreal
Posts: 34
krisl100 is on a distinguished road
Wow, that did it, sometimes it just takes a second pair of eyes, I knew it was something simple. Thanks.
krisl100 is offline   Reply With Quote
Old 10-15-2006, 10:48 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
You could even simplify it more ie:
Code:
/* allways show name */
cout <<  endl << "RESULT: " << name << " ";
if ((midterm < EXAMSPASS && final < EXAMSPASS) || course < PASSMARK)
  {
    fail++;
    cout << "Failed ";
  }
else
  {  /** if they don't fit the fail part, they pass **/ 
    pass++;
    cout << "Passed";
    if(course >= HONOURS)
      cout << " with Honours ";
  }
cout << endl << "-------------------------------------------" << endl;
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-16-2006, 04:09 AM   #5 (permalink)
krisl100
krisl100
 
Join Date: Oct 2006
Location: Montreal
Posts: 34
krisl100 is on a distinguished road
It works well, except that when the program is first run, it does not ask for the user's name ex: "Enter name or type QUIT to exit"

It just starts with Enter midterm mark, once in the loop it works fine, ex: when it prints out the first set of results, then it asks for the user name.
krisl100 is offline   Reply With Quote
Old 10-16-2006, 05:13 AM   #6 (permalink)
krisl100
krisl100
 
Join Date: Oct 2006
Location: Montreal
Posts: 34
krisl100 is on a distinguished road
Please disregard my last entry, I figured it out. Thanks.
krisl100 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how the servlet will integrate the LDAP code j.gohel Java 19 04-16-2005 12:55 AM
Cisco Code breaking sde Code Newbie News 0 05-21-2004 07:10 AM
remove duplicate entries from an array? Admin PHP 3 07-14-2003 12:30 PM


All times are GMT -8. The time now is 08:34 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting