Console Disapear!

This is a discussion on Console Disapear! within the C++ Programming forums, part of the General Programming Boards category; Hey, im new to the C++ Language, and i got a book for X-mass (Accelerated C++) where it shows one ...

  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 04:44 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    907
    std::system("PAUSE") is nice non-portable code for Windows.
    Last edited by CodeMonkey; 12-26-2006 at 12: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,340
    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, 08: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, 01:02 PM
  4. Console Functions Help
    By Artist_of_dream in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 02:44 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 09:19 AM

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