Vector's problem

Hello! I have someting strange in my code.
There is my program.

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 "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cstdlib> 
using namespace std;
template<typename T>
vector<T> getSeparatedValuesFromUser(char separator = ',')
{
stringstream ss;
string str;
getline(cin, str);
replace(str.begin(), str.end(), separator, ' ');
ss << str;
T value{ 0 };
vector<T> values;
while (ss >> value) { values.push_back(value); }
return values;
}
int main()
{

double y;
cout << "Imput something\n";
cin >> y;
auto t = getSeparatedValuesFromUser<double>();
cout<< t[0];
system("pause\n");
return 0;
}

And it dosent work.
When I swap the lines it starts to work.
1
2
3
4
5
6
7
8
9
10
int main()
{
double y;
cout << "Imput something\n";
auto t = getSeparatedValuesFromUser<double>();
cin >> y;
cout<< t[0];
system("pause\n");
return 0;
}

So what is the problem? Thanks in advance.
Last edited on
What's the point of cin >> y;? You don't use the value of y for anything.
There is a part of my program. When I get things together where "auto t" is below some "cin" 's. I have the same problem.
cin >> y >> ws;

Mixing cin >> and getline() is fraught with difficulty.
cin >> will leave linefeed '\n' in the stream ... to be picked up by a following getline(), resulting (probably) in an empty line.
getline() extracts '\n' from the stream.

ws - see http://www.cplusplus.com/reference/istream/ws/
may be used to remove any succeeding whitespace - including the linefeed - from the stream.
Last edited on
Thank you for your reply.
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{

	double y,x;
	cout << "Input something\n";
	cin >> y>>ws;
	cout << "Input X\n";
	cin >> x>>ws;
	auto t = getSeparatedValuesFromUser<double>();
	cout << t[0];
	system("pause\n");
	return 0;
}

It is closer to my code. And in this way, it still dpsen't work((
EDIT 2
1
2
3
4
5
6
7
8
9
10
	double y,x;
	cout << "Input something\n";
	cin >> y;
	cout << "Input X\n";
	cin >> x>>ws;
	cout << "Input t\n";
	auto t = getSeparatedValuesFromUser<double>();
	cout << t[0];
	system("pause\n");
	return 0;

With this I get this output:
1
2
3
4
5
6
7
Input something
5
Input X
6
7 8 9
Input t
7

Why "imput t" follows after the "cout << "Imput t\n";"?
Last edited on
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
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
using namespace std;
template<typename T>
vector<T> getSeparatedValuesFromUser(char separator = ',')
{
stringstream ss;
string str;
getline(cin, str);
replace(str.begin(), str.end(), separator, ' ');
ss << str;
T value{ 0 };
vector<T> values;
while (ss >> value) { values.push_back(value); }
return values;
}

int main()
{
	double y,x;
	cout << "Imput[sic] something\n";
	cin >> y;
	cout << "Imput X\n";
	cin >> x >> ws;            // Only put ws here, because of getline to follow.
	auto t = getSeparatedValuesFromUser<double>();
	cout << t[0];
}


Imput[sic] something
0
Imput X
0
3,4,5
3 



Alternatively:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    string dummy;
	double y,x;
	cout << "Imput[sic] something\n";
	cin >> y;
	cout << "Imput X\n";
	cin >> x;   getline( cin, dummy );
	auto t = getSeparatedValuesFromUser<double>();
	cout << t[0];
}
Last edited on
Thank you. Now I have another problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	string dummy;
	double y,a;
	cout << "Input[sic] something\n";
	cin >> y;           // Only put ws here, because of getline to follow.
	getline(cin, dummy);
	cout << "Input Array\n";
	auto t = getSeparatedValuesFromUser<double>();
	a= accumulate(t.begin(), t.end(), 0);
	cout << "Output:   " << a;
	system("pause\n");
	return 0;
}

and I get
1
2
3
4
5
Input[sic] something
5
Input Array
0.5 0.5
Output:   0

I have something wrong with accumulate function. It acts like int, but it should be double.
accumulate returns a value of the same type as the third argument.

Try like this instead:
 
a = accumulate(t.begin(), t.end(), 0.0);
Last edited on
Thank you. I want to loop my program and use this code
1
2
3
4
5
6
7
setlocale(LC_CTYPE, "rus");
	char yn;
	do
	{
...............................
cout<< "Type Y or Д" << flush;
	} while (cin >> yn && (yn == 'Y' || yn == 'y' || yn == 'Д' || yn == 'д'));

The problem is that the loop works with letter Y, but dosent work with cyrillic letter Д in spite of having setlocate. When I press Д it just closes the program.
Topic archived. No new replies allowed.