Results 1 to 11 of 11

Thread: need help with this program.

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    5

    need help with this program.

    #include <iostream.h>
    int num1,num2;
    int acc=1000;
    char atm;
    char keepon='y'||'Y';
    ;
    void main ()

    {

    while (keepon=='y'||'Y')
    {
    cout <<"1. Make a deposit (Press D\n"
    <<"2. Widthraw money (Press W)\n"
    <<"3. See account info (Press A)\n"
    <<"4. Exit Program (Press E)"<<endl;
    cin >> atm;

    switch (atm)
    {
    case 'D':
    cout <<"Enter amount you want to despoit" <<endl;
    cin >> num1;
    cout <<"You have a total of " <<num1+acc <<" in your account"<<endl;
    cout << "To continue enter Y" <<endl;
    cin >> keepon;
    break;
    case 'W':
    cout <<"Enter amount you want to widthraw" <<endl;
    cin >> num1;
    cout <<"You have a total of " <<acc-num1 <<" in your account"<<endl;
    cout << "To continue enter Y" <<endl;
    cin >> keepon;
    break;
    case 'A':
    cout <<"Your Account info" <<endl;
    cout << "To continue enter Y" <<endl;
    cin >> keepon;
    break;
    case 'E':
    cout <<"Goodbye. Have a nice day!" <<endl;
    break;
    default:
    cout <<"\nInvalid operation";
    break;
    }
    }
    }
    ok this is what i wrote. what i am trouble is that how do I get the balance to roll over?
    Like after I press D and deposit $200 I have a total balance of $1200 (everyone start with $1000 balance). then I ask if you want to continue. press y for yes, and bring to the main menu, next I press W for widthraw. but when I widthraw like $500. it widthdraw from the balance of $1000 not my prevous balance, which is $1200. anyone could help me with this?

  2. #2
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Think about it, it really isn't that hard...
    When making a deposit or withdrawal and you show the result you add or subtract to the acc variable. But when doing it on the cout line you don't actually change the variable so you would have to have num1+acc or acc-num2 on a seperate line.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    5
    but that is only good for 2 times tho. i have a loop, and if the person want to despoist 100 times, is should be able to allow them to do that. since you are saying i could just do "num1+acc or acc-num2 " then they only can do it for like 2 desposit right?

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Wrong, you misunderstood him. He meant that instead of
    Code:
    case 'D':
      cout <<"Enter amount you want to despoit" <<endl;
      cin >> num1;
      cout <<"You have a total of " <<num1+acc <<" in your account"<<endl;
    you should write
    Code:
    case 'D':
      cout <<"Enter amount you want to despoit" <<endl;
      cin >> num1;
      acc = num1+acc;
      cout <<"You have a total of " <<acc <<" in your account"<<endl;
    or even better instead of
    acc = num1 + acc;
    write
    acc += num1;

    And write
    #include <iostream>
    using namespace std;

    instead of
    #include <iostream.h>

    The .h header is deprecated and does not follow the newest C++ standard.

    Finally, use [code] tags instead of [quote] tags, they preserve formatting like indentation, which makes the code easier to read.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    5
    thanks cornedbee, you answer my thread while i was at school. at that time I already figure it out myself..

    i can't recall but I believe i did the same thing you said on the post..
    my school uses borland 5.5 I guess its very old since I was told to use #include <iostream.h>


    Anyway. Now its the most difficult part. when I press A, its suppose to display the account info. what my teacher mean is that display the history transaction. Like all the widtdraw and deposit that was made. Now this i really have no clue on how to even do this. could you please help me on this one?

    And whats the cin command to exit the program? its for the Case E.

  6. #6
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    I'm affraid I can't help you with the history transactions stuff but I think I can help with ending the program.
    Your loop begins with saying "while (keepon=='y'||'Y')" so I would assume that you just have to change the keepon value to something that is not y or Y (i.e. n or N for No). What the "cin >> keepon;" line does is it puts what ever character the user types in the variable keepon, you can do that in code useing the = operator, but I guess you already knew that... So your code should probably look something like this:
    Code:
    case 'E': 
    cout <<"Goodbye. Have a nice day!" <<endl; 
    keepon = 'n';
    break;
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    McCain is right.

    The transaction history is easy too. I would recommend a ostringstream from the header <sstream>, but since you have to use an outdated compiler (you should complain) you will have to use a ostrstream from <strstream.h>. This works like cout, but it saves everything into a string instead of writing it out.
    Don't use endl, use '\n' instead.
    Like this:
    Code:
    #include <strstream.h>
    //...
    ostrstream log;
    log << "Original total: " << acc << '\n';
    // ...
    case 'D':
      cout <<"Enter amount you want to despoit" <<endl;
      cin >> num1;
      acc += num1;
      cout <<"You have a total of " <<acc <<" in your account"<<endl;
      log << "Deposition: " << num1 << '\n';
      log << "New total: " << acc << '\n';
    // etc.
    
    // output:
    case 'A':
      cout << log.str() << endl;
    To clear the history you could write:
    log.str("");
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    5
    we never learned "log.str("");" and we suppose to use stuff we learn. so that might even hold against me when it comes to grading the project.

  9. #9
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    Thats fair enough and you should use what you learn to show that you've taken it in. However, in this instance (and the other times you ask questions on here) you have been given a good solution.

    I finished my degree some time ago and have been working in a software company. They don't care (within reason) what you do as long as it gets done. Therefore, in terms of your grades - if they tell you specifically to do something then do it - otherwise improvise. They can't take you down for doing something they didn't restrict.

    On the other hand, if they told you to implement this in a particular way, then you've got to do it.

    Just a couple of thoughts from someone who's done it before.

    HD

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    raz0r - my normal disclaimer applies here. If your teachers have a problem with something that has been said here, they're welcome to come and attempt to fight it out with us.

    Although unless they really want to prove just how crap they are, they're best getting a good book and learning how to do it themselves first.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    They would first have to survive our attack for using an outdated compiler. That's hard enough if you're not immortal.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width