Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 24

Thread: C++ help

  1. #11
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: C++ help

    Code:
    #include <iostream>
    using namespace std;
    int main() {
      float t; cin>>t;
      int change = static_cast<int> (t * 100);
      int q = change / 25; change %= 25;
      int d = change / 10; change %= 10;
      int n = change / 5; change %= 5;
      int p = change;
    
      cout>>q>>" ">>d>>" ">>n>>" ">>p>>endl;
      return 0;
    }
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  2. #12
    Join Date
    Jan 2006
    Beans
    239

    Re: C++ help

    Quote Originally Posted by slavik View Post
    Code:
    #include <iostream>
    using namespace std;
    int main() {
      float t; cin>>t;
      int change = static_cast<int> (t * 100);
      int q = change / 25; change %= 25;
      int d = change / 10; change %= 10;
      int n = change / 5; change %= 5;
      int p = change;
    
      cout>>q>>" ">>d>>" ">>n>>" ">>p>>endl;
      return 0;
    }
    Is there another way of doing this without using using "namespace std;" because I haven't gotten that far in the book were they explain what that does.

  3. #13
    Join Date
    Apr 2007
    Location
    Arkansas
    Beans
    222
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: C++ help

    You might need more parenthesizes. For example,

    dimes = (int)(amount - (quarters * 0.25) / 0.10);

    Maybe should be:

    dimes = (int)( (amount - (quarters * 0.25) ) / 0.10);

    EDIT: if you keep with the your original technique (sorry i didn't see the last 2 posts)
    Last edited by jonathanmotes; July 20th, 2007 at 07:05 AM.

  4. #14
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: C++ help

    Quote Originally Posted by sdmike View Post
    Is there another way of doing this without using using "namespace std;" because I haven't gotten that far in the book were they explain what that does.
    The namespace is just to allow you him to use "cout" and "cin" without needing "std::cout"

    You'll eventually get to that good stuff...

  5. #15
    Join Date
    Jan 2006
    Beans
    239

    Re: C++ help

    My calculations suck because lets say if there is 0.01 then everything goes to zero.

    back to the drawing board.

  6. #16
    Join Date
    Jan 2006
    Beans
    239

    Re: C++ help

    Code:
    #include <iostream>
    using namespace std;
    int main() {
      float t; cin>>t;
      int change = static_cast<int> (t * 100);
      int q = change / 25; change %= 25;
      int d = change / 10; change %= 10;
      int n = change / 5; change %= 5;
      int p = change;
    
      cout>>q>>" ">>d>>" ">>n>>" ">>p>>endl;
      return 0;
    }
    When I tried using modulus (%) I got errors everytime.

    Can this code be changed into, what I kind of have?

  7. #17
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: C++ help

    The modulus should work... Those ">>" operators are backwards though...

    How about this:
    Code:
    #include <iostream>
    #include <cmath>
    
    int main()
    {
        float t;
    
        std::cin >> t;
    
        int c = (int)floor(t*100);
    
        int q = c / 25;
        c %= 25;
        int d = c / 10;
        c %= 10;
        int n = c / 5;
        c %= 5;
        int p = c;
        std::cout << q << " " << d << " " << n << " " << p << sdt::endl;
        return 0;
    }

  8. #18
    Join Date
    Nov 2005
    Beans
    403
    Distro
    Ubuntu Development Release

    Re: C++ help

    Quote Originally Posted by sdmike View Post
    When I try it this way. It compiles but it's still out of whack.

    For example if someone gave me 86 cents,

    3 quarters
    1 dime
    0 nickel
    1 pennies

    Code:
    # include <iostream>
    
    float amount;
    float quarters;
    float dimes;
    float nickels;
    float pennies;
    
    
    int main()
    {
    while (true){
    
    std::cout << "Enter an amount less than $1.00: ";
    std::cin >> amount;
    
    if (amount == 0)
        break;
    
    quarters = (int)(amount / 0.25);
    
    dimes = (int)(amount - (quarters * 0.25) / 0.10);
    
    nickels = (int)(amount - (dimes * .10) / 0.05);
    
    pennies = (int)(amount - (nickels * 0.05) / 0.01);
    
    
    std::cout << "You have " << quarters << " quarters" << ".\n";
    std::cout << "You have " << dimes << " dimes" << ".\n";
    std::cout << "You have " << nickels << " nickels" << ".\n";
    std::cout << "You have " << pennies << " pennies" << ".\n";
    }
    return (0);
    
    }
    The lines calculating the amount of coins don't calculate what you think they do. For example,
    Code:
    dimes = (int)(amount - (quarters * 0.25) / 0.10);
    is the same as,
    Code:
    dimes = (int)(amount - ((quarters * 0.25) / 0.10) );
    when what you intended was,
    Code:
    dimes = (int)( (amount - (quarters * 0.25)) / 0.10);
    Look up "operator precedence"!

    But in any case that approach is wrong as the quarters are only subtracted for the purpose of this calculation---by the time you get along to nickels, the "quarter amount" is still left in the total and is not subtracted. You should be permanently subtracting the quarters, dimes, nickels etc. as they are accounted for. (This is implicit in Slavik's code.)

    Code:
    # include <iostream>
    
    float amount;
    float quarters;
    float dimes;
    float nickels;
    float pennies;
    
    
    int main()
    {
    	while (true){
    		
    		std::cout << "Enter an amount less than $1.00: ";
    		std::cin >> amount;
    		
    		if (amount == 0)
    			break;
    		
    		quarters = (int)(amount / 0.25);
    		amount -= (quarters * 0.25);
    		
    		dimes = (int)(amount / 0.10);
    		amount -= (dimes * 0.10);
    		
    		nickels = (int)(amount / 0.05);
    		amount -= (nickels * 0.05);
    		
    		pennies = (int)(amount / 0.01);
    		
    		
    		std::cout << "You have " << quarters << " quarters" << ".\n";
    		std::cout << "You have " << dimes << " dimes" << ".\n";
    		std::cout << "You have " << nickels << " nickels" << ".\n";
    		std::cout << "You have " << pennies << " pennies" << ".\n";
    	}
    	return (0);
    	
    }
    See the difference?
    Last edited by WebDrake; July 20th, 2007 at 09:28 AM.

  9. #19
    Join Date
    Nov 2005
    Beans
    403
    Distro
    Ubuntu Development Release

    Re: C++ help

    Quote Originally Posted by slavik View Post
    Code:
    #include <iostream>
    using namespace std;
    int main() {
      float t; cin>>t;
      int change = static_cast<int> (t * 100);
      int q = change / 25; change %= 25;
      int d = change / 10; change %= 10;
      int n = change / 5; change %= 5;
      int p = change;
    
      cout>>q>>" ">>d>>" ">>n>>" ">>p>>endl;
      return 0;
    }
    The reason you get errors with Slavik's code is because he uses the wrong operator with cout, he was using >> instead of <<. It should be,

    Code:
    #include <iostream>
    using namespace std;
    int main() {
    	float t; cin>>t;
    	int change = static_cast<int> (t * 100);
    	int q = change / 25; change %= 25;
    	int d = change / 10; change %= 10;
    	int n = change / 5; change %= 5;
    	int p = change;
    	
    	cout << q <<" " << d << " " << n << " " << p << endl;
    	return 0;
    }
    A slightly nicer version (which includes the output statements you desire, and omits the "using namespace std" since you seem to want to avoid that) is,

    Code:
    #include <iostream>
    
    int main() {
    	float t;
    	std::cout << "Enter an amount less than $1.00: ";
    	std::cin >> t;
    	int change = static_cast<int> (t * 100);
    	int q = change / 25; change %= 25;
    	int d = change / 10; change %= 10;
    	int n = change / 5; change %= 5;
    	int p = change;
    	
    	std::cout << "You have " << q << " quarters" << std::endl;
    	std::cout << "You have " << d << " dimes" << std:: endl;
    	std::cout << "You have " << n << " nickels" << std::endl;
    	std::cout << "You have " << p << " pennies" << std::endl;
    	
    	return 0;
    }
    EDIT: Sorry Wybiral, didn't read closely enough BTW I approve of your using floor(), it seems a safer bet to me than just an int cast.

  10. #20
    Join Date
    Jan 2006
    Beans
    239

    Re: C++ help

    Quote Originally Posted by WebDrake View Post
    The reason you get errors with Slavik's code is because he uses the wrong operator with cout, he was using >> instead of <<. It should be,

    Code:
    #include <iostream>
    using namespace std;
    int main() {
    	float t; cin>>t;
    	int change = static_cast<int> (t * 100);
    	int q = change / 25; change %= 25;
    	int d = change / 10; change %= 10;
    	int n = change / 5; change %= 5;
    	int p = change;
    	
    	cout << q <<" " << d << " " << n << " " << p << endl;
    	return 0;
    }
    A slightly nicer version (which includes the output statements you desire, and omits the "using namespace std" since you seem to want to avoid that) is,

    Code:
    #include <iostream>
    
    int main() {
    	float t;
    	std::cout << "Enter an amount less than $1.00: ";
    	std::cin >> t;
    	int change = static_cast<int> (t * 100);
    	int q = change / 25; change %= 25;
    	int d = change / 10; change %= 10;
    	int n = change / 5; change %= 5;
    	int p = change;
    	
    	std::cout << "You have " << q << " quarters" << std::endl;
    	std::cout << "You have " << d << " dimes" << std:: endl;
    	std::cout << "You have " << n << " nickels" << std::endl;
    	std::cout << "You have " << p << " pennies" << std::endl;
    	
    	return 0;
    }
    EDIT: Sorry Wybiral, didn't read closely enough BTW I approve of your using floor(), it seems a safer bet to me than just an int cast.
    What is "static_cast<int>"?

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •