Jan 24, 2019 at 4:23pm UTC
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 Jan 24, 2019 at 4:26pm UTC
Jan 24, 2019 at 4:32pm UTC
What's the point of cin >> y;
? You don't use the value of y for anything.
Jan 24, 2019 at 4:38pm UTC
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.
Jan 24, 2019 at 4:40pm UTC
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 Jan 24, 2019 at 5:11pm UTC
Jan 24, 2019 at 5:12pm UTC
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 Jan 24, 2019 at 5:21pm UTC
Jan 24, 2019 at 5:19pm UTC
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 Jan 24, 2019 at 5:21pm UTC
Jan 24, 2019 at 10:37pm UTC
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 Jan 24, 2019 at 10:38pm UTC