adding values to vectors using cin

Ok so I am learning c++ and I tried to use push_back to add values to a vector using cin to get the values. However it keeps giving me some errors that I have marked in the comments of this code.

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
#include <iostream>
#include <vector>
#include <istream>

using namespace std;

vector<int> highs;
vector<int>lows;

void indata(int n1[1000], int n2[1000], int ni, int ni2, int inc,int inc2)
{
     inc = 0;
     inc2 = 0;
     cout << "imput number of numbers:\n";
     cin >> ni;
     ni = ni2;
     cout << "lets get the lows and highs\n";
     for( inc <= ni; inc++;);
          {
          cout << "imput highs:\n";
          cin >> n1; //21 C:\Dev-Cpp\stocks1.cpp no match for 'operator>>' in 'std::cin >> n1' //
          highs.push_back(n1*); //22 C:\Dev-Cpp\stocks1.cpp expected primary-expression before ')' token //
          }
     cout << "proccesing\n";
     for(inc2 <= ni2; inc2++;);
              {
              cout << "imput lows:\n";
              cin >> n2; //28 C:\Dev-Cpp\stocks1.cpp no match for 'operator>>' in 'std::cin >> n2' //
              lows.push_back(n2*); //29 C:\Dev-Cpp\stocks1.cpp expected primary-expression before ')' token //
              }
    cout << "finished";
}

int main()
{
    void indata();
}


Please help I've use google and its not is giving me the answer. I'm sure something incredibly simple is going wrong here but I can't figure it out.
Last edited on
n1 and n2 are array's. You cannot edit the values of n1 and n2 by any means, including cin.

Also, n1 and n2 are unnecessary. Try:
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
#include <iostream>
#include <vector>
// #include <istream> // istream is auto included with iostream. This is reduendant.

using namespace std;

vector<int> highs;
vector<int> lows;

//void indata(int n1[1000], int n2[1000], int ni, int ni2, int inc,int inc2)
// Try:
void indata()
{
		int userInput, numInputs;
    // inc = 0;
    // inc2 = 0;
     cout << "input number of numbers:\n";
    // cin >> ni;
    cin >> numInputs;
    // This is the same as your code. But you were making them arguements to be passed in. If you are only defining them here, they are better as local vars.
    // ni = ni2; // REDUNDANT
     cout << "lets get the lows and highs\n";
    // for( inc <= ni; inc++;);
    // BETTER FOR LOOP IS SETUP LIKE THIS:
    for (int i = 0; i < numInputs; i++)
          {
          cout << "input highs:\n";
          // cin >> n1; // THIS IS WRONG. YOU CAN'T SET THE VALUE OF AN ARRAY LIKE THIS IN C++
          // TRY:
          cin >> userInput;
          //highs.push_back(n1*); //22 C:\Dev-Cpp\stocks1.cpp expected primary-expression before ')' token //
          highs.push_back(userInput);
          // MERGE THE TWO LOOPS.
          cout << "input lows:\n";
          cin >> userInput; // Note you can reuse this var!
          lows.push_back(userInput);
          }
     cout << "proccesing\n";
     // for(inc2 <= ni2; inc2++;);
              // {
              // cout << "imput lows:\n";
              // cin >> n2; //28 C:\Dev-Cpp\stocks1.cpp no match for 'operator>>' in 'std::cin >> n2' //
              // lows.push_back(n2*); //29 C:\Dev-Cpp\stocks1.cpp expected primary-expression before ')' token //
              // }
    cout << "finished\n";
}

int main()
{
    void indata();
    // RETURN 0 after main is good practice.
    return 0;
}
Thank you, that clears a lot of things up.
I know there is a topic on this already but I having trouble with it. When run it it just flashes by. Closes immediately. I have tried cin.get, and system("PAUSE") as a last resort but i'm not sure where to place cin.get or whatever to prevent this. I am using dev C++. Thank you in advanced.
just place system("pause"); in main before return statement.

1
2
3
4
5
6
7
int main()
{
    void indata();
    // RETURN 0 after main is good practice.
    system("pause");
    return 0;
}
Topic archived. No new replies allowed.