vectors

Feb 23, 2013 at 2:42pm
Hi,
I am a beginner of c++ as you see this post in beginner section :). Iam having troubles with running small programs created as practice. In this program, I am trying to get inputs from a function and add to the vector and print the vector.
FYI: I am using codeblocks compiler.

#include <iostream>
#include <vector>
#include <conio.h>

using namespace std;

int choose (int x);

int main()
{
vector <int> *_vote;
for(i=0;i<=10;i++)
{
int choice = choose();
_vote = new vector <int>();
_vote->push_back(choice);
}
cout<< _vote->size()<<endl;
_getch();
}

int choose (int x)
{
int x;
cout<< "Enter your choice, 1,2 or 3: ";
cin>>x;
return x;
}


The error displayed:

\Vect_pract02.cpp: In function 'int main()':
\Vect_pract02.cpp:12:9: error: 'i' was not declared in this scope
\Vect_pract02.cpp:14:29: error: too few arguments to function 'int choose(int)'
\Vect_pract02.cpp:7:5: note: declared here
\Vect_pract02.cpp: In function 'int choose(int)':
\Vect_pract02.cpp:24:8: error: declaration of 'int x' shadows a parameter
Last edited on Feb 23, 2013 at 2:43pm
Feb 23, 2013 at 3:14pm
Don't new your vectors, it makes no sense. Just use them as they are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>

using namespace std;

int choose()
{
    cout << "Enter your choice, 1,2 or 3: ";
    int x;
    cin >> x;
    return x;
}

int main()
{
    vector <int> vote;
    for(int i=0; i<10; ++i)
    {
        int choice = choose();
        vote.push_back(choice);
    }
    cout << vote.size() << '\n';
}

online demo: http://ideone.com/zZZtVz
Feb 23, 2013 at 3:14pm
Can you read error messages? They are very clear and even point to statements that contain them. So try to correct your program yourself.
Feb 23, 2013 at 3:30pm
Cubbi
Thanks for the reply, but i have tried like how you did initially. And had tried the same way again now.. not working, and throwing the same error. Will it be because i am using code blocks rather than visual c++ ?
Feb 23, 2013 at 3:51pm
It doesn't matter what compiler you're using, at least not for 1998 code I posted.
Topic archived. No new replies allowed.