Thread: C program

  1. #16
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by Adak View Post
    Overall it looks - a little over complicated (what do you care about sales price dollars and cents? Or cash price dollars and cash price cents?). You seem to have the math all set - good!

    Half those variables should wing their way to the variable graveyard. They just muck up the program with unneeded junk, imo.

    Run-time and Clarity are the clarion call for programs, and your program has no problem with run-time.

    If I want to use any value for the input is it ok if I just do it like this

    Code:
    double SalesPrice = '\n;
    double CashPaid = '\n;
    I'm not sure if this would be okay. Guess I have to delete the numbers after the "remaining".

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by XodoX View Post
    If I want to use any value for the input is it ok if I just do it like this

    Code:
    double SalesPrice = '\n;
    double CashPaid = '\n;
    I'm not sure if this would be okay. Guess I have to delete the numbers after the "remaining".
    Doubles are numbers, and '\n' (note the second single quote btw), is a newline char - which is just an 8 bit number, itself.

    So, no.

    double SalesPrice; (leaving SalesPrice uninitialized), is fine, or
    double SalesPrice = 0.0; is fine

    Either method above, is fine.

  3. #18
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Thank you

    It says thoug "want to try again?"

    should I just make it reset ? If I just use printf to say yes or no it might be to simple. I dont know. Also dont know how I would do it though. Couldn't find a good C code database online.

    Edit:

    I gues I could do it with if. If xx then yes if yy then no. Im not sure what xx could be in order to make it ask this.
    Last edited by XodoX; 07-03-2008 at 06:07 PM.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by XodoX View Post
    Thank you

    It says thoug "want to try again?"

    should I just make it reset ? If I just use printf to say yes or no it might be to simple. I dont know. Also dont know how I would do it though. Couldn't find a good C code database online.

    Edit:

    I gues I could do it with if. If xx then yes if yy then no. Im not sure what xx could be in order to make it ask this.
    Usually a user query is done something like this:

    Code:
    do  {
       print Enter your Sales Price
       scanf(" %f", &salesPrice);
       
       print Enter your Cash Paid
       etc.
    
       print Buy Another Item (y/n) ?
       scanf(" %c", &another);
    
    }while(another != 'n' || another != 'N')
    This was what you were trying to show me before, right? I thought you were showing me something with a newline because of the backslash in it - \n.

    Is this something I would use outside the classroom? Absolutely not. Scanf is just not well suited for the real world, these days. For a homework assignment, it's OK.

    If you have more questions, please post your code so I can see what you're writing about. It's hard to understand the details from a posted description of something you can't see.
    Last edited by Adak; 07-03-2008 at 07:11 PM.

  5. #20
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Code:
     Quarters = Remaining / 25; // How many quarters?  0
    Remaining = Remaining % 25; // How much left?  9
    Dimes = Remaining / 10; // How many dimes?  0
    Remaining = Remaining % 10; // How much left? 9
    Nickels = Remaining / 5; // How many nickels?   1
    Pennies = Remaining % 5; // How many pennies? 4
    After this part there gotta be something that asks "Want to do this(try this) again?"Meaning want to calculate something again or not. Thats what I meant.
    So basically this part would work .

    Code:
    print Buy Another Item (y/n) ?
       scanf(" %c", &another);
    
    }while(another != 'n' || another != 'n')
    Which I would just add to the end.
    Last edited by XodoX; 07-03-2008 at 07:08 PM.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Right. The do loop I posted above, needs to wrap around your posted code.

    do

    your code all goes here

    want to buy another item?

    while, etc

    Nope. The do needs to go near the top of your logic, and the question "want to buy another item?" needs to go after all the other logic has finished. Then comes the while line.

    One of those small n's needs to be a capital N - my mistake.
    Last edited by Adak; 07-03-2008 at 07:13 PM.

  7. #22
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    oh well this gets confusing now. But I am trying to understand.

    Code:
    do  {
       
    SPDollars = (int)SalesPrice;                         // SPDollars = 5
    SPCents = (int)(SalesPrice * 100.00) % 100; // 5.66 * 100.00 = 566.00; 566 % 100 = 66 cents
    CPDollars = (int)CashPaid;
    CPCents = (int)(CashPaid * 100.00) % 100;
       
      if (Remaining < 0) {
       // We had to borrow a dollar
       Dollars--;
       Remaining += 100;
    };
    
       printf ( "Buy Another Item (y/n)" ) ?
       scanf(" %c", &another);
    
    }while(another != 'n' || another != 'N');
    Thats how I understand it.

    Unless you meant to put the code in yours and put it to the very end of the entire code.
    Last edited by XodoX; 07-03-2008 at 07:43 PM.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by XodoX View Post
    oh well this gets confusing now. But I am trying to understand.

    Code:
    do  {
       
    SPDollars = (int)SalesPrice;                         // SPDollars = 5
    SPCents = (int)(SalesPrice * 100.00) % 100; // 5.66 * 100.00 = 566.00; 566 % 100 = 66 cents
    CPDollars = (int)CashPaid;
    CPCents = (int)(CashPaid * 100.00) % 100;
       
      if (Remaining < 0) {
       // We had to borrow a dollar
       Dollars--;
       Remaining += 100;
    };
    
       printf ( "Buy Another Item (y/n)" ) ?
       scanf(" %c", &another);
    
    }while(another != 'n' || another != 'N');  //replace || with &&
    Thats how I understand it.

    Unless you meant to put the code in yours and put it to the very end of the entire code.
    Another correction: || should be replaced with && in the while loop.

    The code to handle each purchase and return of change (if any), needs to go inside the do loop.

    I don't know what you mean by "put the code in yours" ?? When you bring the code blocks together into the program, I believe you'll see where they need to go.

    If not, post the code, and describe the problem *in detail*. You have an ability to leave me floating on a sea of question marks, from some of your posts.

  9. #24
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by Adak View Post
    Another correction: || should be replaced with && in the while loop.

    The code to handle each purchase and return of change (if any), needs to go inside the do loop.

    I don't know what you mean by "put the code in yours" ?? When you bring the code blocks together into the program, I believe you'll see where they need to go.

    If not, post the code, and describe the problem *in detail*. You have an ability to leave me floating on a sea of question marks, from some of your posts.

    No, if this is correct than everything is fine. The code who needs to be handled is in there now.
    It looks like this now. So the single purchase is in it.

    Code:
     #include <iostream.h>
    
    main()
    {
    
    double SalesPrice = 0.0;
    double CashPaid = 0.0;
    
    int SPDollars; // Sales price dollars
    int SPCents;   // Sales price cents
    int CPDollars; // Cash paid dollars
    int CPCents;   // Cash paid cents
    
    int Dollars;   // Number of $1 bills
    int Quarters;  // Number of Quarters
    int Dimes;     // Number of Dimes
    int Nickels;   // Number of Nickels
    int Pennies;   // Number of Pennies
    int Remaining;
    
    
    do  {
       
    SPDollars = (int)SalesPrice;                         // SPDollars = 5
    SPCents = (int)(SalesPrice * 100.00) &#37; 100; // 5.66 * 100.00 = 566.00; 566 % 100 = 66 cents
    CPDollars = (int)CashPaid;
    CPCents = (int)(CashPaid * 100.00) % 100;
       
      if (Remaining < 0) {
       // We had to borrow a dollar
       Dollars--;
       Remaining += 100;
    };
    
       printf ( "Buy Another Item (y/n)" ) ?
       scanf(" %c", &another);
    
    }while(another != 'n' && another != 'N')
    
    Quarters = Remaining / 25; // How many quarters?  0
    Remaining = Remaining % 25; // How much left?  9
    Dimes = Remaining / 10; // How many dimes?  0
    Remaining = Remaining % 10; // How much left? 9
    Nickels = Remaining / 5; // How many nickels?   1
    Pennies = Remaining % 5; // How many pennies? 4
    
    
    
    return 0;
    }
    If you dont mean it like this I assume you mean the ENTIRE code in the loop instead of just my part but I hope like THIS is ok.
    Last edited by XodoX; 07-03-2008 at 08:22 PM.

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Like that is NOT OK, sorry.

    For each purchase, you will have to have the code INSIDE the do loop, that gives back change, of course.

    THEN you ask them if they'd like to make another purchase.

    So drop this line of code, down below where you return the change (or print the amount of
    change to be returned): (and you will print that up properly won't you?)

    Code:
    }while(another != 'n' && another != 'N')
    Last edited by Adak; 07-03-2008 at 08:44 PM.

  11. #26
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by Adak View Post
    Like that is NOT OK, sorry.

    For each purchase, you will have to have the code INSIDE the do loop, that gives back change, of course.

    THEN you ask them if they'd like to make another purchase.
    Sorry but I don;t understand that at all. And I thought I finally got it
    Now I understand it like the ENTIRE ode has to be in the loop.
    I give up....

    Nothing has to be returned. Just has to ask the question if the user wants to run the program again or not.

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by XodoX View Post
    Sorry but I don;t understand that at all. And I thought I finally got it
    Now I understand it like the ENTIRE ode has to be in the loop.
    I give up....

    Nothing has to be returned. Just has to ask the question if the user wants to run the program again or not.
    Not that bad:

    Code:
     #include <iostream.h>
    
    main()
    {
    
    double SalesPrice = 0.0;
    double CashPaid = 0.0;
    
    int SPDollars; // Sales price dollars
    int SPCents;   // Sales price cents
    int CPDollars; // Cash paid dollars
    int CPCents;   // Cash paid cents
    
    int Dollars;   // Number of $1 bills
    int Quarters;  // Number of Quarters
    int Dimes;     // Number of Dimes
    int Nickels;   // Number of Nickels
    int Pennies;   // Number of Pennies
    int Remaining;
    
    
    do  {
       
    SPDollars = (int)SalesPrice;                         // SPDollars = 5
    SPCents = (int)(SalesPrice * 100.00) &#37; 100; // 5.66 * 100.00 = 566.00; 566 % 100 = 66 cents
    CPDollars = (int)CashPaid;
    CPCents = (int)(CashPaid * 100.00) % 100;
       
      if (Remaining < 0) {
       // We had to borrow a dollar
       Dollars--;
       Remaining += 100;
    };
    
       printf ( "Buy Another Item (y/n)" ) ?
       scanf(" %c", &another);
    
    //}while(another != 'n' && another != 'N')
    
    Quarters = Remaining / 25; // How many quarters?  0
    Remaining = Remaining % 25; // How much left?  9
    Dimes = Remaining / 10; // How many dimes?  0
    Remaining = Remaining % 10; // How much left? 9
    Nickels = Remaining / 5; // How many nickels?   1
    Pennies = Remaining % 5; // How many pennies? 4
    
    }while(another != 'n' && another != 'N')     //from 7 lines up, to here, that's all. :)
    
    return 0;
    }
    Put some good print statements in there to show what the Dollars, Quarter...Pennies, of change are, and aren't you all done?
    Last edited by Adak; 07-03-2008 at 09:35 PM.

  13. #28
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Code:
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct {
    	int dollars;
    	int nickel;
    	int pennies;
    }CHANGE; 
    
    // Calculate Change
    CHANGE convertChange(char s_money[], float f_money)
    {
    	char dot = '.';
    	CHANGE change;
    	char *m_ptr, pennies[5];
    
    	// Get the dollar part
    	change.dollars = f_money;
    
    	// Find the decimal point
    	m_ptr = strchr(s_money, dot);
    	
    	// And get the pennies part
    	strcpy(pennies, ++m_ptr);
    	change.pennies = atoi ( pennies );
    	
    	// Not sure what is a nickle
    
    	return change;
    }
    would something like this work? Use Rands so not sure what nickle is....

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, that way was mentioned back on the first page of this thread iirc, and can work just fine (although I didn't run your code).

    Clearly, you'd need to add Quarters - 25 pennies, dimes - 10 pennies, and nickels are worth 5 pennies, to your change struc.

    This is a problem that can be solved a lot of different ways.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM