Thread: Where are all these errors coming from?

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    49

    Where are all these errors coming from?

    I'm trying to write a program that adds two money amounts in old-style British currency---pounds, shillings, pence---and asks if the user wants to continue. It also adds a pound every 20 shillings, and adds a shilling every 12 pence. (it's an exercise in my book).

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	
    		int pnd, shl, pnc;
    		int pnd2, shl2, pnc2;
    		int totp, tots, totpn;
    		char c;
    		char ch;
    
    		while (ch != 'n') 
    		{
    			cout << "Enter two amounts in pounds, shillings, and pence: ";
    			cin >> pnd >> c >> shl >> c >> pnc;
    			cout << endl;
    			cout << "Enter second amount: ";
    			cin >> pnd2 >> c >> shl2 >> c >> pnc2;
    			cout << endl;
    
    			totp = pnd + pnd2;
    			tots = shl + shl2;
    			totpn = pnc + pnc2;
    
    			while (!(tots % 20))
    			{
    				totp += 1;
    				tots -= 20;
    			}
    
    			while (!(totpn % 12))
    			}
    				tots += 1;
    				totpn -= 12;
    			}		
    
    			cout << "Total is " << totp << "." << tots
    				 << "." << totpn << endl << endl;
    			cout << "Continue (y/n)?";
    			cin >> ch;
    		} 
    
    		return 0;
    }
    The problem is it brings up seventeen errors, most of them pertaining to a (or several) missing semicolon. I've looked over the code and I don't see any syntax mistakes. What is wrong with the code?

    By the way, I know I might have made a few calculating mistakes (to display the correct result), but I'm going to fix any stuff like that once I rid of these errors.

  2. #2
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    what line numbers do the errors occur on?

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    All your errors stem from one backwards bracket. Change the bracket after "while (!(totpn % 12))" to point the other way.

  4. #4
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    I don't see much wrong with your code. One thing i did notice though is that the test expression in youre while loop would probably generate a warning from the compiler; something like 'ch is being used before initialization.' Just incase you want your code to be totally 'perfect' in compilation (like i do), initiallize ch to something. I usually put something like ch = 99; That shoudl get rid of the warning. Sorry i can't be of more help

    By any chance, would you happen to be using "Object-oriented programming in C++" by robert lafore?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    49
    Thanks! I didn't even think to look at the braces.

    I have just one more question; my original test expressions for the carrying of pounds and shillings (20 shillings = 1 pound, 12 pounds = 1 shilling) didn't give correct results, so after a bit of trial and error the tests are now just:

    Code:
                            while (totpn > 11)
    			{
    				tots += 1;
    				totpn -= 12;
    			}
    					
    			while (tots > 19)
    			{
    				totp += 1;
    				tots -= 20; 
    			}
    whereas my original tests were in this form:

    Code:
    while (!(tots % 20))
    They didn't produce correct results, and I'm not sure why. "While tots is evenly divisible by 20" is how I read it, so it's hard for me to figure out what the problem is.


    Quote Originally Posted by jrahhali
    By any chance, would you happen to be using "Object-oriented programming in C++" by robert lafore?
    I sure am.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    They didn't produce correct results, and I'm not sure why. "While tots is evenly divisible by 20" is how I read it, so it's hard for me to figure out what the problem is.
    I think you're error comes from that fact that zero modulus a number is zero, so you get something like:

    tots=20
    20%20==0 so do while loop, tots-=20 so tots=0
    0%0==0 so stay in while loop, tots-=20 so tots=-20

    From here on I'm not sure what happens, don't know if every compiler would assess -20%20 as zero or not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. errors using my parser- MSVC++ 6, bhtypes.h
    By AeroHammer in forum C++ Programming
    Replies: 6
    Last Post: 01-25-2005, 07:11 PM
  3. opengl Nehe tutorial errors when compiling
    By gell10 in forum Game Programming
    Replies: 4
    Last Post: 07-14-2003, 08:09 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM