Need some help with looping

This is a discussion on Need some help with looping within the C++ Programming forums, part of the General Programming Boards category; This is a basic version of the programming I'm working on. Now as most of you can see the output ...

  1. #1
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    Question Need some help with looping

    This is a basic version of the programming I'm working on.

    Now as most of you can see the output is going to be..... 12345.
    But I want it to just print out 5 not 12345.

    b/c the program I'm trying to make is that you enter an amount of money and it will give you you're amount of money in pennie, nickel, dime, and quarter.

    thanks......
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() 
    {
    
    	float pennie = .01;
    	float nickel = .05;
    	float dime = .10;
    	float quarter = .25;
    
    
    
    	for ( float MoneyEnter = 1.00; MoneyEnter <= 5.00; MoneyEnter++ ) {
    
    		cout << MoneyEnter;
    
    	}
    
    
    	return(0);
    }
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    So you loop from 1 to 5, printing the value every time. Now you want to print only 5?

    I don't understand the problem.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Assembler + Basic from 79
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    22
    I'm still pretty new to C++ but I think the FOR loop must have a signed integer type for the interater variable.
    Look at it from the perspective of trying to interate. Does the computer interate by 1, 0.1, 0.001, 0.0001, etc? You get the idea.
    Twin engine aircraft are way better. If one engine quits the other takes you to the scene of the crash.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Nope, no such thing. The parts of the for loop can be pretty arbitrary things. The step size is given by the third expression. Note that ++ always means +=1 (unless it's overloaded in weird ways). And yes, that's a bit strange for floating point numbers, but it's just the way the language works.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Assembler + Basic from 79
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    22
    Cool, well I learned something new then but its the first I've ever seen of a float in a For loop.
    Twin engine aircraft are way better. If one engine quits the other takes you to the scene of the crash.

  6. #6
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    here the program i'm working on

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() 
    {
    
    	float MoneyEnter;
    	float pennie = .01;
    	float nickel = .05;
    	float dime = .10;
    	float quarter = .25;
    
    	cout << "Enter amount less then a $1.00 \n>";
    	cin >> MoneyEnter;
    
    	
    
    	return(0);
    }
    When the user enter in a number less then a dollar it will tell you the number of quarters, dimes, nickels, and pennies needed.
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  7. #7
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57
    That what i was talking about on the top program just in a different way ... sorry about the confusion
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 12:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 06:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21