I need some help

So i have this problem but i'm still confused on how to compose a working code for it.

Write a C++ program that will get a collection of n numbers from the user. If the user entered 0 or a negative number the program terminates the process of entering numbers. The program uses the search() that is used to search a specified value by the user through the keyboard. If the value exists on the list of n numbers, the function returns how many times/ occurrences of that number from the given list. Otherwise, the program returns an appropriate message: “NO SUCH NUMBER!”
and display it on the screen.
Sample Output:

Enter a number: 3
Enter a number: 2
Enter a number: 3
Enter a number: 2
Enter a number: 2
Enter a number: 3
Enter a number: 0

Enter number to search: 4
NO SUCH NUMBER!

Do you want to search more (y/n) => y

Enter a number: 3
Enter a number: 2
Enter a number: 3
Enter a number: 2
Enter a number: 2
Enter a number: 3
Enter a number: -1

Enter number to search: 3
There are 3 copies of “3” on my list.

Do you want to search more (y/n) => n

Press any key to continue …………
What have you written so far?
so far this

#include <iostream>
using namespace std;

void search (int x);


int main()
{
int nos;
cout<<"Enter a number: ";
cin>>nos;

Honestly, i'm not that much of a fan of programming anymore. I just want to get this thing over with as soon as possible.
Last edited on
OK, so so far it accepts one number. Just one. This problem requires you to use dynamic memory, are you allowed to use things like std::vector? (I really hope so)
Not sure what that is but i guess. We can use a loop so we can repeat the process, then when it enters a negative number, it can stop. Then it will try and search for the number that was repeated.
OK, so since you can use std::vector that will make things a LOT easier on us.

First you should include the header for std::vector:
#include <vector>
You can find reference material here: http://www.cplusplus.com/reference/stl/vector/

Now, you can make a std::vector as simple as this:
std::vector<int> my_list;
where 'int' is the variable type the std::vector will be a list of. (int is probably what you want anyway)

Then, in the loop where you ask the user to input the numbers, you can add the inputted number to the std::vector like this:
my_list.push_back(number);

Once you have finished the input, you can access the size of the vector with my_list.size() and access an element with the [] operator:
my_list[x]
where x is a number or variable. The vector class acts like a normal array for this, so you can use it in an expression and assign to it.

You can loop through the vector just like an array and count the number of times a number is in it, just like you would with an array.

Give it all a try and post here when you get stuck or have a question ;)
wow, it's sounds a little bit too complicated. Isn't there a much more simplier way? I haven't reached the level of using vectors yet.
vectors are the absolute simplest way to do this. It just doesn't get any simpler - solutions without vectors are either harder or limited to only a certain number of items.
so then can you like give a sample code? i really don't know how to compose it. really. i just want this thing done.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> v;
    int x;
    do
    {
        cin >> x;
        v.push_back(x);
    }while(x);
    cout << endl << "The elements you entered are: " << endl;
    for(unsigned i = 0; i < v.size(); ++i)
    {
        cout << v[i] << ' ';
    }
}


Hopefully you can figure out how to use a vector ;)
Topic archived. No new replies allowed.