Multiple inputs from cin

I need two user inputs (int) to be pushed back into two different vectors respectively. What I have now is the first user input is pushed back into both vectors...

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
 
void ReadDataA()
{
  double a;
  std::stringstream ssa;
  std::string stringA;
  std::vector<double> c1;
  std::vector<double>::iterator iter1;

  std::cin >> a;
  
  ssa << a;
  ssa >> stringA;

  for(int i = 1; i <= stringA.length(); ++i)
  	c1.push_back(i);

  std::cout << "Vector c1 = ";

  for(iter1 = c1.begin(); iter1 != c1.end(); ++iter1)
  	std::cout << *iter1;

  std::cout << std::endl;
}

void ReadDataB()
{
  double b;
  std::stringstream ssb;
  std::string stringB;
  std::vector<double> c2;
  std::vector<double>::iterator iter2;

  std::cin >> b;

  ssb << b;
  ssb >> stringB;

  for(int j = 1; j <= stringB.length(); ++j)
  	c2.push_back(j);

  std::cout<< "Vector c2 = ";

  for(iter2 = c2.begin(); iter2 != c2.end(); ++iter2)
  	std::cout << *iter2;

  std::cout << std::endl;

}


int main()
{ 
  ReadDataA();
  ReadDataB();

  return 0;
}
add using namespace std; after include all libraries, it will make your life easier.
add using namespace std; after include all libraries, it will make your life easier.

Do not do that. It might make it a tad easier now, but you'll get fucked in the future. Stick to using std:: Pretty much anyone who knows their shit will tell you the same.

Edit: If you actually want to make your life easier, use the c++11 feature called "auto"

instead of writing this -
for (iter1 = c1.begin(); iter1 != c1.end(); ++iter1)

You just write this -

for (auto& iter1 : c1)

Edit 2: This works fine for me. They are beinged pushed back into two different vectors -

http://gyazo.com/b3298630539d81b2603ff5f5be2c91e3

Edit 3: If you want THE ACTUAL USER INPUT to be pushed back into each vector. Then why are you using a for-loop, and why are you pushing back "i" in the first one?

Instead, push back the user input like you want right?

c1.push_back(a);
and
c2.push_back(b);

http://gyazo.com/838d1bf2d95faf8b14d358372523f092

Edit 4: If you wanna do what fg109 suggested under, which Im guessing is, then make your vectors of strings, not double.
Last edited on
1
2
3
c1.push_back(i);
//...
c2.push_back(j);

You are pushing back the indices. I think you meant

1
2
3
c1.push_back(stringA[i]);
//...
c2.push_back(stringB[j]);

But if so, you'd be pushing characters into a vector of doubles.
Topic archived. No new replies allowed.