weird exiting error

when i execute this program about halfway through the program simply exits how do i fix this?
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
  #include "stdafx.h"

#include<iostream>
#include<string>
#include<sstream>
#include<fstream>

using namespace std;

int main()
{
	int a;
	int b;
	cout<<"You find yourseelf in a small village, you hear the sound of birds chirping in the distance."<< endl;
	cout<<"There are paths leading in all directions yet two of them are blocked by local guards." << endl;
	cout<<"type 2 for north or 4 for west"<< endl;
	cin>> a;
	if (a = 2)
	{
		cout<<"You bump into a freindly villager, the villager was kind and asked if you wanted some resorces to get started with your expedition."<< endl;
		cout<<"Take the resorces?"<< endl;
		cout<<"type 1 for yes and 2 for no";
	}
	cin.get();
	return 0;
}
Try replacing cin.get(); by
1
2
std::cin.clear();
std::cin.ignore(10000, '\n');


Also if (a = 2) is incorrect, replace it by if (a == 2)
This is because "=" is the assignment operator, and it gives a the value of 2. For comparisons you have to use "==".

Just in case you didn't know, you can change int a; int b; into int a, b;
Last edited on
Topic archived. No new replies allowed.