Check my C++ code?

airbornflghtairbornflght Houston, TX Icrontian
edited January 2006 in Internet & Media
okay, here is a proggy i made today, took about 20 min, and I keep getting into a loop in the bold part, no matter if i hit x, y or any other character, it always says illegal entry. I moved a few things around and tried a few things, but i dont have access to a compliler (borland) right now. so if some one would look at it and see if it should all run right, or if you wanna be awesome and complile it for me and see if everything goes alright let me know. (if you compile it and wanna stick the binary on here or email it to me at airbornlflght at cox dot net.
// pay_roll.cpp

#include <iostream.h>

int main()
{
 float hrwg, hrs, ovrtim, txprc, taxamt, grspay, netpay;
 char taxans;

 ovrtim = 0; //
 txprc = 0;  //  initialize potentially unused vars to 0.
 taxamt = 0; //
 netpay = 0; //

 cout << "How much do you make an hour? (leave the dollar sign off) $";
 cin >> hrwg;
 cout << " " << endl;

 cout << "How many hours did you work this pay-period? ";
 cin >> hrs;
 cout << " " << endl;


 cout << "Do you know the current tax rate (percent)[use y or n]? ";
 cin >> taxans;
 cout << " " << endl;
[B]do
	{
		if (taxans != 'y' || taxans != 'n')
			{
			cout << "That is an illegal entry.\n";
			cout << "Please re-enter y or n again.\n";

			cout << " " << endl;

			cout << "Do you know the current tax rate (percent)[use y or n]? ";
			cin >> taxans;
			cout << " " << endl;
			}
	}
	while (taxans != 'y' || taxans != 'n');[/B]

	if (taxans == 'y')
		{
			cout << "What is the current tax rate (leave of the % sign)? ";
			cin >> taxprc;
			cout << " " << endl;
		}
	

 if (taxans == 'y')      //do math
	{
		if (hrs > 40)
			{
				ovrtim = hrs - 40;
				hrs = 40;
				grspay = (hrs * hrwg) + (ovrtim * hrwg * 1.5);
				netpay = grspay - (txprc * grspay);
				taxamt = txprc * grspay;
			}
		else
			{
				grspay = hrs * hrwg;
				taxamt = txprc * grspay;
				netpay = grspay - (txprc * grspay);
			}
	}

 else
	{
		if (hrs > 40)
			{
				ovrtim = hrs - 40;
				hrs = 40;
				grspay = (hrs * hrwg);
			}
		else
			{
				grspay = (hrs * hrwg);
			}

	}


cout << "Total Hours Worked: " << hrs + ovrtim << endl;

if (ovrtim != 0)
	{
		cout << "Total Normal Hours: " << hrs << endl;
		cout << "Total Overtime Hours: " << ovrtim << endl;
	}

else
	{
		cout << "Total Hours: " << hrs << endl;
	}


if (netpay != 0)
	{
		cout << "Amount of gross pay: " << grspay << endl;
		cout << "Amount of taxes: " << taxamt << endl;
		cout << "Amount of net pay: " << netpay << endl;
	}

else
	{
		cout << "Amount of pay: " << grspay << endl;
	}



	return 0;
}

Comments

  • EMTEMT Seattle, WA Icrontian
    edited January 2006
    It's a logic problem :)
    (taxans != 'y' || taxans != 'n')
    

    Do you know Boolean logic? By DeMorgan's Law this statement is the same as
    ( !(taxans=='y'&&taxans=='n') )
    
    which more clearly will always be true since taxans will not be both y and n.

    In English what your check in the if and while statements does is "as long as taxans isn't y OR isn't n, keep asking." But if it is one then it's still not the other--that's why your program continues the loop. You need to change || to && so the check is "as long as taxans isn't y and isn't n, keep asking." You were probably getting this confused with something like "as long as it's not true that taxans is y or taxans is n, keep asking."

    Sorry if I was a little wordy :)
  • airbornflghtairbornflght Houston, TX Icrontian
    edited January 2006
    no not at all, thanks for the reply. I just thought if it was or, if one was equal to x or y it would break out.

    my logic said that as long as both were not x or y it would stay and when one was x or y it would break out of the do-while. but I see the logic now. we are not really to this point in the class yet, and the program was supposed to actually be very basic, but I wanted to ad taxes and to have it only diplay the data if it was relevant.

    yeh I know the idea behind boolean logic and the truth tables for or,and and not. but I'm not sure if it goes more in depth.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited January 2006
    I thought what I was asking was: as long as taxans isnt y or n keep asking, but if it is one of them then break. i know im obviously wrong but fail to see the logic of why it wont work, I know you explained it, and i see that && will work, but how come || wont?

    *nevermind* if one of them is x then its not y, which will make the or question true and continue the loop, I feel stupid now.
  • EMTEMT Seattle, WA Icrontian
    edited January 2006
    Yeah, it's pretty confusing until you get the hang of thinking of the expression's evaluation instead of how you describe it in English. But if you wanted to be precise about saying it in English, it would be "neither y nor n"; aka a boolean NOR. A NOR B = NOT(A OR B). And sure enough,
    !(taxans=='y' || taxans=='n')
    
    (by DeMorgan's) is the same as
    (taxans!='y' && taxans!='n')
    
    which is what you've just changed it to to make it work.

    Or generally I try to say things like "if it's not y and it's not n" instead of "if it's not y or n" because you simply can't code the second way.
Sign In or Register to comment.