Thread: Need help with errors

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    4

    Need help with errors

    This program accepts a number of cents between 5 and 95 and sorts it out into change of 50,20,10 and 5 cent pieces. It seems to be having errors in the printf where it printf out the number of coins.
    lines 40-43- unterminated string in function main
    line40- function call ) missing in function main.
    line 40- undefined symbol 'd' in function main.
    Code:
    #include <stdio.h>
    int main()
    {
    	int num;
    	int fifty = 0;
    	int twenty = 0;
    	int ten = 0;
    	int five = 0;
    	
    	printf("Please insert change");
    	scanf("%d%*c", &num);
    	
    	while(num >= 5 && num <= 95)
    	{
    		if(num >= 50)
    		{
    			fifty++;
    			num=-50;
    		}
    		else
    			if(num >= 20)
    			{
    				twenty++;
    				num=-20;
    			}
    			else
    				if(num >= 10)
    				{
    					ten++;
    					num=-10;
    				}
    				else
    					if(num >= 5)
    					{
    							five++;
    						num=-5;
    					}
    	}
    	
    	printf("Number of 50 cents "%d", fifty);
    	printf("Number of 20 cents "%d", twenty);
    	printf("Number of 10 cents "%d", ten);
    	printf("Number of 5 cents "%d", five);
    	return(0);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The syntax highlighting should show you exactly what the problem is. Take a look and see if you can sort it out. Interestingly enough, it also shows a bug in the coloring system.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    I would suggest looking into the use of curly braces in statements containing multiple vs. single lines.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iceaway
    I would suggest looking into the use of curly braces in statements containing multiple vs. single lines.
    Braces are already in use. It is just that the formatting of the if else chain is unconventional, although consistent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's a beautiful set of logical statements, imo. (There is a simpler way to do this, using arithmetic, but your way is OK.)

    If only there were a way to eliminate the extra char which causes the error here: *
    Code:
     printf("Number of 50 cents "%d", fifty);
    And of course, it's always a good idea to add a \n char to the end of any printf() string.

    *shamelessly borrowing a Quzah funny one liner.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by laserlight View Post
    Braces are already in use. It is just that the formatting of the if else chain is unconventional, although consistent.
    Doh! Now I see it. Unconventional indeeed, it fooled me completely. Very rebellious use of indentation and line breaks :-)

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Wait until you see Quzah's switch of index and pointer in an array access -- drives me around the bend and I know it's correct! It's just SO WRONG! <sigh>

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    while(num >= 5 && num <= 95)
    {
        if(num >= 50)
        {
            fifty++;
            num=-50;
        }
        else
              if(num >= 20)
        {
            twenty++;
            num=-20;
        }
        else
              if(num >= 10)
        {
            ten++;
            num=-10;
        }
        else
              if(num >= 5)
        {
            five++;
            num=-5;
        }
    }
    I believe there are "issues" with the highlighted sections of the code... unless those are typos.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    hk_mp5kpdw, you are an eagle-eye! Nice catch.

    Obviously the - goes before the = , not the other way around.
    Last edited by Adak; 09-27-2011 at 05:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help, im new to C++ and im getting errors
    By Tiffany_DD in forum C++ Programming
    Replies: 11
    Last Post: 05-02-2009, 08:48 AM
  2. why i get these errors..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 04-14-2009, 12:19 PM
  3. why i get these errors here
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 04-11-2009, 10:28 AM
  4. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 08:43 PM
  5. these are the errors
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-18-2002, 10:16 PM