What is wrong in this code

Can anyone tell me what is coded wrong in this program for it to exit immediately after I enter the value for costShares? I have gone over and over and cannot seem to find anything that would cause this, but yet again am sure it is something obvious.

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
#include <iostream>
#include <string>
using namespace std;

int numberShares;
int costShares;

string stockName;
string stockSymbol;

int main()

{
	cout << "enter name of stock";
	cin >> stockName;

	cout << "enter company stock symbol";
	cin >> stockSymbol;

	cout << "number of shares purchased";
	cin >> numberShares;

	cout << "cost per shares";
	cin >> costShares;

	cout << "what is the commission rate?";
	cin >> commisionRate;


	
	return 0;
}
Last edited on
You don't have a variable for commisionRate;
Simple as that!

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
#include <iostream>
#include <string>
using namespace std;

int numberShares;
int costShares;
int commisionRate;


string stockName;
string stockSymbol;

int main()

{
	cout << "enter name of stock";
	cin >> stockName;

	cout << "enter company stock symbol";
	cin >> stockSymbol;

	cout << "number of shares purchased";
	cin >> numberShares;

	cout << "cost per shares";
	cin >> costShares;

	cout << "what is the commission rate?";
	cin >> commisionRate;



	return 0;
}


Tulu
Add the following at line 29 and 30 to prevent the code from closing immediately:

1
2
int temp;
cin >> temp;


Also not sure if this is related to your problem, but you are using cin >> to get a string from the user. If the user inputs more than 1 word, it will cause lines in your code to skip over one another. To solve this, you could use the std::getline(cin, inputStringHere) to get a string from the user.

EDIT: Also make sure you've declared commisionRate somewhere otherwise your code won't run.
Last edited on
Okay thanks that is a start, here is what it looks like after adding variable for commisionRate; however I am still doing something wrong as it continues to end the program after I enter numberShares value

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
#include <iostream>
#include <string>
using namespace std;

int numberShares;
int costShares;
int commisionRate;

string stockName;
string stockSymbol;

int main()

{
	cout << "enter name of stock";
	cin >> stockName;

	cout << "enter company stock symbol";
	cin >> stockSymbol;

	cout << "number of shares purchased";
	cin >> numberShares;

	cout << "cost per shares";
	cin >> costShares;

	cout << "what is the commission rate?";
	cin >> commisionRate;


	
	return 0;
}
Read my answer ^ :)
Thanks, you guys are awesome!
Topic archived. No new replies allowed.