Thread: Using If commands to reset program

  1. #16
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Think about what the line in mantd's quote does.

    "==" tests for equality. It is very important to not mix it with "="
    which assigns a value to a variable
    Double Helix STL

  2. #17
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Adding to what swgh said: In this loop you make 1 the exit value. Thus when the user enters 1, you want to get out of the loop and move on to the end of the code. Currently, you are only performing the loop while the number is equal to your exit value. != will perform the loop when the number is not equal to your exit value.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    IMO your tutorial is not the best place to learn C++ since it appears to be teaching C style strings instead of C++ strings.

    >> it shuts the program if u dont press 1 cuz of the cin.get()
    Actually, the cin.get() attempts to keep the program open, but because you type 1 and then <enter>, the cin.get() is getting the <enter> instead of waiting for you to type more. A common way to handle this is to use cin.ignore() and then cin.get().

    I don't see anything wrong with while (num == 1). It keeps asking you if you are ready until you say yes. That is correct to me.

  4. #19
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Think about it. The end condition is 1, so testing for == 1 will loop while the exit condition is true.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No, the exit condition is anything but 1. An input of 1 means not ready. The loop runs while the user is not ready. That is why the inside of the loop repeats the prompt "Enter 1 if your not rdy".

  6. #21
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I guess. I was thinking that if the user is not ready (1), it would quit, if not, it would continue with it's loop.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That would make sense, too, but based on the words in the prompt (and the fact that the OP said it was working) I'm guessing the other explanation is what was wanted.

  8. #23
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Check my example mantd, daved is correct, that is what I did in my example. I think the OP has sused this at last. He has not been on here asking for anymore adivise
    Double Helix STL

  9. #24
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Yes, but I would instruct the OP to reverse this, as many programmers (like me and swgh) expect the while loop to continue while not *exit condition* or while *continue condition*. Nonetheless, in the code posted, I believe you are right.
    EDIT: swgh, I said he was right.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #25
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >EDIT: swgh, I said he was right.

    Lol sorry I missed that bit!
    Double Helix STL

  11. #26
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Remember to assign variables memory beforehand

    I was browsing around and just wanted to add that when you create any variable (integer, char, string etc) it is generally good practice to assign it a memory sector beforehand rather then later to insure the user will have enough free memory from the beginning to run the program.

    Different types of variables are assigned memory sectors in different ways.

    e.g:
    Code:
    int someVariable = 0;
    or
    string someOtherVariable[21] = {0};
    or
    char someCharacter = ' ';

  12. #27
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    Remember to assign variables memory beforehand

    Repost....

  13. #28
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    When you declare a variable, it is allocated space on the stack. Assigning a value to that memory doesn't "assign it a memory sector".

    But still, initializing variables is a good idea anyway. (Although most compilers will warn you with warnings turned on when you use a variable that might not have been initialized.

    [edit] There's no need to double-post. It often takes a few seconds after you click the button for the post to appear. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #29
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Simply declaring an int is enough to give it space to run. The reason to initialize it to a value (usually zero) is to make sure that the variable has a known value before it is used.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  5. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM