Program closes after first step

// Final Project.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

string place;
int time;
char confirm;

int main()
{
cout << "Please select your destination." << endl << "1. Las Vegas, 2. London, 3. Rome, 4. Berlin, 5. Madrid, 6. Paris" << endl;
cin >> place;
if (place == "Las Vegas") {
cout << "You have selected " << place << "Loading Flight Times..." << endl << "12:10, 13 : 10, 14 : 10, 15 : 10, 16 : 50, 17 : 00. Later times are currently unavailable." << endl;
cout << "Please select the time you would like" << endl;
cin >> time;
if (time = 1210) {
"You have selected 12:10, is that correct?";
}
cin >> confirm;
if (confirm == 'y') {
cout << "Your tickets are now printing. Thank you for flying with us." << endl;
}
}

return 0;
}
Last edited on
Hello and welcome to cplusplus.com! When posting code, please use the <> button to the right of the text box so that it becomes highlighted.

When asking questions, it is best to give an explanation of what your program is currently doing and what you expected it should have done. This helps the helpers to guide you towards your expectations instead of wandering aimlessly around only to circle back to your expectations.

You have an if statement on line 13 that says if (place == "Las Vegas"). You will never trigger this if you use formatted input operation with cin (cin >> place;). You must instead use an unformatted input operation such as, getline(cin, place);

You probably meant to use == instead of = for if (time = 1210) {. Also the "You have selected 12:10..." is missing cout.
Last edited on
Appreciate it, im struggling to get it to trigger the if statement if they input 1210 with cin >> time; It doesnt do anything and then closes if you repeatedly press enter/ however i would like it to check the if statement and then ask for confirmation..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Final Project.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

string place;
int time;
char confirm;
int main()
{
	cout << "Please select your destination. Using Correct Capitalization" << endl << "1. Las Vegas, 2. London, 3. Rome, 4. Berlin, 5. Madrid, 6. Paris" << endl;
	getline(cin, place);
	if (place == "Las Vegas") {
		cout << "You have selected " << place << " Loading Flight Times..." << endl << "12:10, 13:10, 14:10, 15:10, 16:50, 17:00. Later times are currently unavailable." << endl;
		cin.ignore;
		getline(cin, time);
		if (time == 1210) {
			cout << "You have selected 12:10, is that correct?" << endl;
		}
		else if (time == 1310) {
			cout << "You have selected 13:10, is that correct?" << endl;
		}
		else if (time == 1410) {
			cout << "You have selected 14:10, is that correct?" << endl;
		}
		else if (time == 1510) {
			cout << "You have selected 15:10, is that correct?" << endl;
		}
		cin >> confirm;
		if (confirm == 'y') {
			cout << "Your tickets are now printing. Thank you for flying with us." << endl;
		}
		else {
			cout << "Please correct your time frame";
		}

	}
	getchar();
	return 0;	
}

cout << "Please select your destination. Using Correct Capitalization"

You are leaving too much to the user entry being exactly correct. Unless your assignment specifically asks you to do it this way you should try to get users to input numbers at this stage corresponding to locations.
Even then you'd have to do some input validation to make sure the numbers entered are within range, no leading/lagging characters etc and then proceed with the rest of the program
getline is not meant for integers. Simply change getline(cin, time); back to what it was. http://www.cplusplus.com/reference/string/string/getline/

Likely your integer input is being skipped over because of cin.ignore
Last edited on
Thank you all for the help! Really appreciate it
Topic archived. No new replies allowed.