why does this keep closing

#include <iostream>

using namespace std;

int main()
{
int lol;
cout << "Hello, and welcome to Joe's awesome adventure where the goal is to nothing but laugh\n here you will read hilarious jokes and\n to read the next one all you have to do is type lol then press enter.\n Got it?\n then let's start!" << endl;
cin >> lol;
cout << "Three guys, stranded on a desert island, find a magic lantern containing a genie, who grants them each one wish.\n The first guy wishes he was off the island and back home.\n The second guy wishes the same. The third guy says I’m lonely.\n I wish my friends were back here";
cin >> lol;
cout << "A guy has a talking dog. He brings it to a talent scout. \nThis dog can speak English,\n he claims to the unimpressed agent. \nOkay, Sport,\n the guys says to the dog, \nwhat’s on the top of a house?" "Roof!\n the dog replies. \nOh, come on...\n the talent agent responds. \nAll dogs go ‘roof’. \nNo, wait,\n the guy says. He asks the dog \nwhat does sandpaper feel like?\n Rough!\n the dog answers. The talent agent gives a condescending blank stare. He is losing his patience. \nNo, hang on,\n the guy says. \nThis one will amaze you. \n He turns and asks the dog: \nWho, in your opinion, was the greatest baseball player of all time?\n Ruth! \ngoes the dog.\n And the talent scout, having seen enough, boots them out of his office onto the street.\n And the dog turns to the guy and says\n Maybe I shoulda said DiMaggio";
cin >> lol;



}
Last edited on
The program closes once main reaches its end because programs automatically close when they're done.

See this thread:

http://cplusplus.com/forum/beginner/1988/
like this?
#include <iostream>
#include <limits>
using namespace std;

int main()
{
int lol;
cout << "Hello, and welcome to Joe's awesome adventure where the goal is to nothing but laugh\n here you will read hilarious jokes and\n to read the next one all you have to do is type lol then press enter.\n Got it?\n then let's start!" << endl;
cin >> lol;
cout << "Three guys, stranded on a desert island, find a magic lantern containing a genie, who grants them each one wish.\n The first guy wishes he was off the island and back home.\n The second guy wishes the same. The third guy says I’m lonely.\n I wish my friends were back here";
cin >> lol;
cout << "A guy has a talking dog. He brings it to a talent scout. \nThis dog can speak English,\n he claims to the unimpressed agent. \nOkay, Sport,\n the guys says to the dog, \nwhat’s on the top of a house?" "Roof!\n the dog replies. \nOh, come on...\n the talent agent responds. \nAll dogs go ‘roof’. \nNo, wait,\n the guy says. He asks the dog \nwhat does sandpaper feel like?\n Rough!\n the dog answers. The talent agent gives a condescending blank stare. He is losing his patience. \nNo, hang on,\n the guy says. \nThis one will amaze you. \n He turns and asks the dog: \nWho, in your opinion, was the greatest baseball player of all time?\n Ruth! \ngoes the dog.\n And the talent scout, having seen enough, boots them out of his office onto the street.\n And the dog turns to the guy and says\n Maybe I shoulda said DiMaggio";
cin >> lol;
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;

}
Last edited on
once i press enter, for the first time, then it displays all the text and closes instantly
Last edited on
I understand your issue after reading this. First, you're not understanding how C++ works. The data types (int, char, string, float, etc.) determine the kind of information that a variable can store. Reading over your program, one of the first lines I come to is int lol; where you're creating a variable "lol" and defining it as type int. int variables can only accept whole numbers (within a specified range, but that's irrelevant to this discussion). After reading through your first cout statement, I notice you ask the user type "lol". There is nothing wrong with having them do this.

Your first issue, however, comes from the fact that "lol" is a string, or a character array, depending on how you want to look at it. For simplicity, let's just say string. The user types "lol" and you tell the program that you want to store that in the variable lol. Again not an issue, until we take a closer look. The variable lol was declared as an int and "lol" is a string...we have an issue. cin can not figure out how to handle this (which is why you get some really funky results).

Since I believe you're just learning, you have two options, you can ask the user to enter a number (probably 0 is easiest) to continue (which is the most logical thing to do) or you can change the data type of lol to a string (which is a smart thing to do, but means you have to learn a little bit more).

Since I see you have a more current version of a C++ compiler, here is the easiest way to change the variable lol to a string:
1
2
3
4
#include <string>
// skip a few lines
// change int lol; to:
string lol;


Now you can continue using your code like you originally wanted to. Be careful though, cin has delimiters of whitespace (read: if you ask the user to enter a string and they happen to input a space before they press Enter/Return you'll have some unexpected side effects again). A proper solution to this is to change your cin >> lol; to this cin.getline(lol); because getline uses just one default delimiter instead of all whitespaces, and that's the Enter/Return key.

I hope this has helped you understand your code a little better.

Happy Programming =)

Edit: As a more technical note, I believe the exact reason why your code was terminating was because upon entering the string "lol", it was handled as a character string. Each cin of lol would accept one character which 'l' would be the first, 'o' second, and 'l' for the third, and since there was a value left in the buffer (I believe part of the '\n') it skipped the cin.ignore portion.
Last edited on
Topic archived. No new replies allowed.