Thread: Magneto's journey to becoming a C++ expert.

  1. #16
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    I can't say I fully understand namespaces, but I'm going to start using std::, I guess I'll wait until my book covers it.

    #2
    The question - Where is it applicable to use cin.ignore() and cin.get(), rather than just cin.get()?

    I've noticed that in some cases I don't need to use cin.ignore() before cin.get().

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Where is it applicable to use cin.ignore() and cin.get(), rather than just cin.get()?
    Generally, only when you mix formatted and unformatted input, such as cin's >> operator with cin.get().
    My best code is written with the delete key.

  3. #18
    *this
    Join Date
    Mar 2005
    Posts
    498
    I can't say I fully understand namespaces, but I'm going to start using std::, I guess I'll wait until my book covers it.
    Ok...It's not a bad habit, it's perfectly fine to use the whole namespace. Don't make it seem bad because it isn't. It's not something you "get better at", its only preference. If you plan on making a variable called "cout" then thats not very smart so just change it.

  4. #19
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    Thanks for all your help so far guys, I appreciate it!

    #3
    The Problem - I need to clear the screen in the middle of a program.

    The code -
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int choice;
        cout << "Your choice: ";
        cin >> choice;
        if ( choice == 1 ) {
             system("format a:");
             cin.ignore();
             cin.get();
        }
        else if (choice == 2) {
             system("format a:");
             cin.get();
             cin.ignore();
        }
        else {
             cout << "Your choice is invalid.";
             cin.ignore();
             cin.get();
        }
    }
    Today I found the 'system' function and IF statements, this seems like a good way to test them out. Anyway, what I want is; when a number (choice) is entered the screen needs to be cleared. Then whatever that choice entails is to be carried out on a blank screen.

    #4
    The Problem - I need to restart the program.

    The code -
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int choice;
        cout << "Your choice: ";
        cin >> choice;
        if ( choice == 1 ) {
             system("format a:");
             cin.ignore();
             cin.get();
        }
        else if (choice == 2) {
             system("format a:");
             cin.get();
             cin.ignore();
        }
        else {
             cout << "Your choice is invalid.";
             cin.ignore();
             cin.get();
        }
    }
    So, after a choice is entered and whatever carried out I want to restart the program at the 'menu' bit.

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I need to clear the screen in the middle of a program.
    There's no portable way to do it correctly, and it's a bad practice for most console mode programs anyway.

    >I need to restart the program.
    Put your menu code in a loop.
    My best code is written with the delete key.

  6. #21
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    FAQ

    Always Check FAQ first

  7. #22
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    the program pause could be replaced with a malicious program
    I think I've said that in the past as well, but if someone has the priviledges to replace a program in your system file, they can do pretty much anything they want without having to wait for a poorly written program to be run. The main problem is that the pause program won't always be there. What if your code is compiled for a different OS? What if the user's computer has had pause deleted? What if there's something wrong with default directories on their systems?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Performance Expert - New York, NY
    By txaggie94 in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-28-2007, 07:33 PM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  3. Expert system?
    By FloatingPoint in forum Tech Board
    Replies: 1
    Last Post: 06-14-2003, 07:23 AM
  4. c++ expert
    By febrian81 in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2002, 10:27 PM
  5. Replies: 1
    Last Post: 09-30-2001, 07:45 AM