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.
#include <iostream>
#include <vector>
// #include <istream> // istream is auto included with iostream. This is reduendant.
usingnamespace 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;
}
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.