Thread: C Modular Program (need help please)

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    C Modular Program (need help please)

    Hi everyone,

    I'm new to this forum and to programming and I was just wondering if someone could look at this program I'm trying to create.

    I have attached it to this message.

    The problem is that when I try to compile it it displays the first two printf statements , but when I type in an integer it returns nothing. I do know that the second module "printChange" is not being called properly.

    I would really appreciate some help as to how I can get this to work, becuase I haven't been able to find or understand anything else on the web.

    Thanks again.
    Attached Files Attached Files
    Last edited by johnny-; 04-19-2011 at 09:35 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Had a look... The text formatting massively sucks, I'm guessing you're getting dozens of compiler errors, you can't do math inside the quoted string of printf() and your math is wrong to begin with.

    It's a bog standard coin change program... look up the % and / operators in your documentation.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Since you never initialize changeVal in your printChange() function it could evaluate to anything. But since you are not even calling this function you will not see any problem. You should be passing changeVal into this function instead of redefining this variable in this function.


    Jim

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Hi Comm,
    Thanks for taking a look. I know it sucks lol, told you I was new.

    What I was trying to do was get the program to read in an integer and then match it to the else if statements in the second module. The printf statement was written like that so it would output the right coin amounts instead of basign the return value on a mathematical equation or condition. Would this approach work or do modules only work on mathematically based conditions?


    Sorry if these questions are really dumb, but I really do appreciate your time.

    John.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Hi Jim,

    Thanks for your comment, I try it out.

    John.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by johnny- View Post
    Hi Comm,
    Thanks for taking a look. I know it sucks lol, told you I was new.

    What I was trying to do was get the program to read in an integer and then match it to the else if statements in the second module. The printf statement was written like that so it would output the right coin amounts instead of basign the return value on a mathematical equation or condition. Would this approach work or do modules only work on mathematically based conditions?


    Sorry if these questions are really dumb, but I really do appreciate your time.

    John.
    Not modules ... functions. And you can do anything in a function you can do in main() ... main() is just a function...

    Have you tried to compile your code?

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You have a function that should return an int, but you are returning nothing.

    Also as CommonTater stated your formatting is horrible. Here is one example:
    Code:
    #include <stdio.h>
    
    int getInput()
    {
    
    	int changeVal;
    
    	printf("Please enter an integer that representd the cents in the customer's change.\n");
    	printf("Enter the cents amount from the customer's change here: ");
    	scanf("%d%*c", &changeVal);
    	return(changeVal);
    }
     
     
     
    int printChange(int changeVal)
    {
     
     
    	if ((changeVal >=5) && (changeVal <=7))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 5c.\n");
    	}
     
    	else if ((changeVal >=8) && (changeVal <=12))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 10c");
    	}
     
    	else if ((changeVal >=13) && (changeVal <=17))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 5c\n1 x 10c\n");
    	}
     
    	else if ((changeVal >=18) && (changeVal<=22))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 20c\n");
    	}
     
    	else if ((changeVal >=23) && (changeVal <=27))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 5c\n1 x 20c\n");
    	}
     
    	else if ((changeVal >=28) && (changeVal <=32))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 10c\n1 x 20c\n");
    	}
     
    	else if ((changeVal >=33) && (changeVal<=37))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 5c\n1 x 10c\n1 x 20c\n");
    	}
     
    	else if ((changeVal >=38) && (changeVal <=42))
    	{
    		printf("The required change (cents) consists of the following coins:\n2 x 20c\n");
    	}
     
    	else if ((changeVal >=43) && (changeVal <=47))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 5c\n2 x 20c\n");
    	}
     
    	else if ((changeVal >=48) && (changeVal <=52))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 50c\n");
    	}
     
    	else if ((changeVal >=53) && (changeVal <=57))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 5c\n1 x 50c\n");
    	}
     
    	else if ((changeVal >=58) && (changeVal <=62))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 10c\n1 x 50c\n");
    	}
     
    	else if ((changeVal >=63) && (changeVal <=67))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 5c\n1 x 10c\n1 x 50c\n");
    	}
     
    	else if ((changeVal >= 68) && (changeVal <=72))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 20c\n1 x 50c");
    	}
     
    	else if ((changeVal >=73) && (changeVal <=77))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 20c\n1 x 50c");
    	}
     
    	else if ((changeVal >=78) && (changeVal<=82))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 10c\n1 x 20c\n1 x 50c");
    	}
     
    	else if ((changeVal >=83) && (changeVal <=87))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 5c\n1 x 10c\n1 x 20c\n1 x 50c\n");
    	}
     
    	else if ((changeVal >=88) && (changeVal <=92))
    	{
    		printf("The required change (cents) consists of the following coins:\n2 x 20c\n1 x 50c\n");
    	}
     
    	else if ((changeVal >=93) && (changeVal <=95))
    	{
    		printf("The required change (cents) consists of the following coins:\n1 x 5c\n2 x 20c\n1 x 50c\n");
    	}
     
     
    	return();  /// You should be returning something here.
     
     
     
     
    }
     
    int main()
    {
     
    	int changeVal;
    	changeVal = getInput();
     
     
     
     
    	return(0);
    }
    Jim

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    hey Jim,

    Thanks for the example, mate.

    What would I write as return if I wanted one of those else if statements to be printed bak to the user? Its just that I am really hazy on how to declare them and all that in main.

    Thanks for your help

    John

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by johnny- View Post
    hey Jim,

    Thanks for the example, mate.

    What would I write as return if I wanted one of those else if statements to be printed bak to the user? Its just that I am really hazy on how to declare them and all that in main.

    Thanks for your help

    John
    Do you have a C textbook or even links to online tutorials?
    If not get some... read them, do the examples, learn!

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Hey Comm,

    Just wanted to say thanks for your help. I utilized the pass by reference and it works perfectly. I'm sorry if I put you out of your way but I really do appreciate your time.

    Yea I have books and some other helpful sites, but it was just this particular point that I needed help with. Thanks again.

    John

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    Jim,

    Thanks for all your help pal. I learned a lot from this discussion.

    I really appreciate ur time


    John

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting to Modular
    By DJ_Steve in forum C Programming
    Replies: 3
    Last Post: 09-29-2009, 04:23 AM
  2. modular c programming
    By akrlot in forum C Programming
    Replies: 5
    Last Post: 10-25-2007, 07:00 AM
  3. Whats the best way do designs a modular program
    By *DEAD* in forum C++ Programming
    Replies: 3
    Last Post: 06-19-2007, 08:39 PM
  4. non-modular to modular program conversion
    By strife in forum C Programming
    Replies: 3
    Last Post: 09-22-2006, 03:13 AM
  5. Modular
    By loopshot in forum Game Programming
    Replies: 7
    Last Post: 01-20-2006, 07:24 PM