is there any easy way????

This is a discussion on is there any easy way???? within the C++ Programming forums, part of the General Programming Boards category; was wondering if there is any easy way to have mutiple deposits with only declaring one variable Code: while (month ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    29

    Question is there any easy way????

    was wondering if there is any easy way to have mutiple deposits
    with only declaring one variable

    Code:
    while (month >= 1)////start loop
    	{
    		cout <<"Please enter total deposits per month  $: ";
    		cin >> dep;
    in the above example how would someone enter

    40.00 160.00 200.00

    as individual deposits without using an array I done it before but cant remember how???

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Well the loop will naturally go round 3 times.
    cin just reads what it needs to (say 40.00) and leaves the rest of the input for something else.
    So next time, cin reads 160.00
    etc etc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I don't know how you're gonna use those numbers,
    so don't know if this will solve it
    Code:
    totalDeposit = 0;
    while ( cin >> deposit )
        totalDeposit += deposit;
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by alphaoide
    I don't know how you're gonna use those numbers,
    so don't know if this will solve it
    Code:
    totalDeposit = 0;
    while ( cin >> deposit )
        totalDeposit += deposit;
    perhaps you should try running your code before you suggest it to others
    while( cin >> deposit ) is a bad idea.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >perhaps you should try running your code before you suggest it to others
    Perhaps you should try running someone else's code before correcting them.

    >while( cin >> deposit ) is a bad idea.
    I have an idea. You tell me why you think this is a bad idea and I'll tell you why you're wrong.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Yeah, Bjarn Stroustrup's technical FAQ uses the same code. I doubt he would do that if it was a "bad idea".

  7. #7
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >Perhaps you should try running someone else's code before correcting them.
    I did, the while cin loop never terminates. It will be pretty difficult to process the data if you never stop taking it

    >You tell me why you think this is a bad idea and I'll tell you why you're wrong.
    see above.

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by bithub
    Yeah, Bjarn Stroustrup's technical FAQ uses the same code. I doubt he would do that if it was a "bad idea".
    In many sitiuations its a good idea, in the example given above it was a bad idea. The above example would never (properly) terminate from the input accumulation part. The code that followed, which actually processes the data, isnt reached.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    15
    hi dazed,
    maybe this will assist??

    was wondering if there is any easy way to have mutiple deposits
    with only declaring one variable

    Code:
    short month = 0; //counter
    float dep = (float)0.0; //user prompt
    float balance = (float)0.0; //accumulator


    while (month <= 2)////start loop
    {
    cout <<"Please enter total deposits per month $: ";
    cin >> dep;
    balance = balance + dep; //accumulate balance
    month = month + 1; //increment counter
    }

    cout << "Balance is: $" << balance << endl;

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I did, the while cin loop never terminates.
    Sure it does. If there's an error or if end-of-file is reached. Naturally, after the loop it would be a good idea to check and see which of those it was and an informative prompt before the loop is always nice.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int n;
      int sum = 0;
    
      for ( int i = 0; i < 3; i++ ) {
        cout<<"Enter a series of numbers followed by EOF: ";
    
        while( cin >> n )
          sum += n;
    
        if ( !cin.eof() ) {
          cerr<<"Something bad happened"<<endl;
          break;
        }
        else {
          cout<<"The total is: "<< sum <<endl;
          cin.clear();
        }
      }
    }
    >The above example would never (properly) terminate from the input accumulation part.
    Only if your idea of "properly" doesn't include reaching the end-of-file, and that's a stylistic choice.
    My best code is written with the delete key.

  11. #11
    Banned
    Join Date
    Aug 2004
    Posts
    128
    you could just ask them how many deposits theyd like to make

    Code:
    cout << "How many deposites would you like to make? :  ";
    cin >> numofdep;
    
    while ( numofdep != 0 )
    {
         cout << endl << endl;
         cout << "Enter a Deposit Amount  : ";
         cin >> dep;
         balance +=dep;
         numofdep--;
    }
    
    cout << "Your Current Balance is   $" << balance << endl;
    thats my suggestion

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    How is eof() generated for cin? I always thought you have to hit ctrl-c, but that (as far as I know) quits the program without giving it a chance to process data.

    **EDIT**
    JarJarBinks, you could have just used a for loop... they tend to be easier to understand then whiles when you're looping a set number of times.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Banned
    Join Date
    Aug 2004
    Posts
    128
    yea i was gonna use a for loop, but he started up
    there with a while so i figured i wouldnt stray from anything that
    he couldnt possibly understand, and if he understands for loops
    then he can convert it himself .

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    How is eof() generated for cin?
    ctrl-D in linux, and ctrl-Z in windows.

  15. #15
    Registered User Chubz's Avatar
    Join Date
    Sep 2004
    Posts
    16
    Quote Originally Posted by Salem
    Well the loop will naturally go round 3 times.
    cin just reads what it needs to (say 40.00) and leaves the rest of the input for something else.
    So next time, cin reads 160.00
    etc etc
    Hehe, sorry to go offtopic, but man your avatar is so awesome.

    Did you actually create that yourself??

    Well, at least it's true!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:48 PM
  2. Java vs C to make an OS
    By WOP in forum Tech Board
    Replies: 59
    Last Post: 05-27-2007, 03:56 AM
  3. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  4. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  5. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM

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