Thread: Console Disapear!

  1. #1
    C++ Learner! XelleX90's Avatar
    Join Date
    Nov 2006
    Location
    Norway
    Posts
    9

    Exclamation Console Disapear!

    Hey, im new to the C++ Language, and i got a book for X-mass (Accelerated C++) where it shows one of the easyest codes, but there is something strange with my console, it disapears after i type in my first name! what to do


    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    	// Ask for a persons name
    	std::cout << "Please enter your first name: ";
    	
    	// Read the name
    	std::string name;    // define name
    	std::cin >> name;    // read into name
    	
    
    	// Write a greetings
    	std::cout << " Greetings,  " << name << "! have a whonder full time with accelerated c++" << std::endl;
    	
    	return 0;
    	
    }

    i tryied with cin.get() too but then the first letter in my name whont show
    Code:
     
    #include <iostream>
    #include <string>
    
    int main()
    {
    	// Ask for a persons name
    	std::cout << "Please enter your first name: ";
    	std::cin.get();
    	// Read the name
    	std::string name;    // define name
    	std::cin >> name;    // read into name
    	std::cin.get();
    
    	// Write a greetings
    	std::cout << " Greetings,  " << name << "! have a whonder full time with accelerated c++" << std::endl;
    	std::cin.get();
    	return 0;
    	
    }
    any help would be apritiated
    Last edited by XelleX90; 12-25-2006 at 05:44 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can try using a std::cin.ignore() before the std::cin.get(). Alternatively, you can ditch both those extra statements and just run your program from the command line.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++ Learner! XelleX90's Avatar
    Join Date
    Nov 2006
    Location
    Norway
    Posts
    9
    ok thanks take care

  4. #4
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    the reason it's disappearing is because it doesn't require any input from the user. it prints what it has to and then exits sucesfully. because you (i'm assuming) double clicked on the program instead of running it from the console, it exits and takes the console with it.

    as laserlight said, you could either get input from the keyboard at the end of the program or you could run it directly from the command line.

  5. #5
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    just so's you know, you can use the
    Code:
    using namespace std;
    which makes it alot easier to program in my opinion....

    Code:
    #include <iostream>
    #include <string>
    // namespace
    using namespace std;
    
    int main()
    {
    	// Ask for a persons name
    	cout << "Please enter your first name: ";
    	cin.get();
    	// Read the name
    	string name;    // define name
    	cin >> name;    // read into name
    	cin.get();
    
    	// Write a greetings
    	cout << " Greetings,  " << name << "! have a whonder full time with accelerated c++" << endl;
    	cin.get();
    	return 0;
    	
    }
    hope this helps

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    start / run / type cmd
    drag your program in to the black window bobs your uncle

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    std::system("PAUSE") is nice non-portable code for Windows.
    Last edited by CodeMonkey; 12-26-2006 at 01:13 AM. Reason: code mistake
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by beene
    just so's you know, you can use the
    Code:
    using namespace std;
    which makes it alot easier to program in my opinion....

    Code:
    #include <iostream>
    #include <string>
    // namespace
    using namespace std;
    
    int main()
    {
    	// Ask for a persons name
    	cout << "Please enter your first name: ";
    	cin.get();
    	// Read the name
    	string name;    // define name
    	cin >> name;    // read into name
    	cin.get();
    
    	// Write a greetings
    	cout << " Greetings,  " << name << "! have a whonder full time with accelerated c++" << endl;
    	cin.get();
    	return 0;
    	
    }
    hope this helps
    I strongly advise not to do so. It's against the whole purpose of namespaces.. The purpose of having things in namespaces is to avoid name collision (i.e. a function named max() and a variable named max). Including the whole std namespace brings name collisions quite often. Using the std:: prefix is a nice way to access the contents of the namespace std. If you absolutely can't bear typing those extra 5 characters, you can use 'using std::something' which includes only parts of the namespace in the global namespace but this is still not as good as typing std:: everytime. It's not easier at all, it's just faster not to type it... until you step into a bug caused by name collision.

  9. #9
    C++ Learner! XelleX90's Avatar
    Join Date
    Nov 2006
    Location
    Norway
    Posts
    9
    thanks 4 all advise's

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The reason your std::cin.get() was not working was because you added too many to your program. What std::cin.get() does is read a character from the input stream. In your second piece of code you have this:
    Code:
    	// Ask for a persons name
    	std::cout << "Please enter your first name: ";
    	std::cin.get();
    That asks for a name, then reads a single character. That is why the first character is missing, the std::cin.get() is reading it and ignoring it.

    What you should be doing is adding a std::cin.get() or std::cin.ignore() only after you call std::cin >> to read in data, and then once at the end of your program to keep the window open.

    The reason that is necessary is that when the user is entering their name into your program, they will type the name and hit <enter>. Typing <enter> adds a newline character to the input. When you read in data with std::cin >>, it stops at the newline character and leaves it there for later. Calling std::cin.get() or std::cin.ignore() once will ignore that newline. Calling it again will cause the code to wait for the user to hit <enter> again. So the final code should look like this:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    	// Ask for a persons name
    	std::cout << "Please enter your first name: ";
    
    	// Read the name
    	std::string name;    // define name
    	std::cin >> name;    // read into name
    	std::cin.get();
    
    	// Write a greetings
    	std::cout << " Greetings,  " << name << "! have a whonder full time with accelerated c++" << std::endl;
    	std::cin.get();
    	return 0;
    	
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 09:59 PM
  2. Full Screen Console
    By St0rmTroop3er in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2005, 09:59 PM
  3. Problems with a simple console game
    By DZeek in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2005, 02:02 PM
  4. Console Functions Help
    By Artist_of_dream in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 03:44 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM