getline

my getline isn't working can someone help
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;
	
	struct StockInfo
	
	{
		string coname;
		int numShares;
		int PurPrice;
		int CurrPrice;
	};
	
	int main()
	{
	const int size=10;
    int i;
	 
	 StockInfo portfolio[size];
		
		{
			for(i=0;i<10;i++) 
		cout << "enter company's  name please:"<<endl;
		getline(cin,portfolio[i].coname);
		cout << "enter the number of shares bought:"<<endl;
		cin >> portfolio[i].numShares;
		cout <<"What was the purchase price?"<<endl;
		cin >> portfolio[i].PurPrice;
		cout << "what is the current price of the share?"<<endl;
		cin >> portfolio[i].CurrPrice;
		cin.ignore();
	}
	return 0;
	}.


output
enter company's name please:
enter company's name please:
enter company's name please:
enter company's name please:
enter company's name please:
enter company's name please:
enter company's name please:
enter company's name please:
enter company's name please:
enter company's name please:
Segmentation fault (core dumped)


------------------
(program exited with code: 139)
Press return to continue

With proper indentation some problems become apparent.

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;

struct StockInfo
{
	string coname;
	int numShares;
	int PurPrice;
	int CurrPrice;
};

int main()
{
	const int size=10;
	int i;
	
	StockInfo portfolio[size];
	
	{
		for(i=0;i<10;i++) 
			cout << "enter company's  name please:"<<endl;
		getline(cin,portfolio[i].coname);
		cout << "enter the number of shares bought:"<<endl;
		cin >> portfolio[i].numShares;
		cout <<"What was the purchase price?"<<endl;
		cin >> portfolio[i].PurPrice;
		cout << "what is the current price of the share?"<<endl;
		cin >> portfolio[i].CurrPrice;
		cin.ignore();
	}
	return 0;
}

As you can see. The for loop only contains one of the statements. The program crashes when i is used to access array elements after the loop because then i is 10 and out of bounds. This is one of the reasons why it is often recommended to declare variables in the smallest scope possible. Had you made i local to the loop you would have noticed that something was wrong because the compiler wouldn't let you use i after the loop.

1
2
3
int i;
...
for (int i = 0; i < size; i++) 

Anyway, the solution to your problem is to wrap all the statements that you want to be part of the loop in a { } block, like this:

1
2
3
4
5
6
7
8
9
10
11
12
for (int i = 0; i < size; i++)
{
	cout << "enter company's  name please:"<<endl;
	getline(cin,portfolio[i].coname);
	cout << "enter the number of shares bought:"<<endl;
	cin >> portfolio[i].numShares;
	cout <<"What was the purchase price?"<<endl;
	cin >> portfolio[i].PurPrice;
	cout << "what is the current price of the share?"<<endl;
	cin >> portfolio[i].CurrPrice;
	cin.ignore();
}
Last edited on
thank you , silly mistakes !
Topic archived. No new replies allowed.