Now I am having an issue passing my vector into a function. Any ideas?

This code works fine but I cant help but think that there has to be a better way to do this!!!

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
  void TestVector::sixElementVector()
{
   vector<int> data(6);

   int a = 0;
   for (int i = 0; i < data.size(); i++)
   {
      cout << "Please enter a number between 10 and 101"
         << " to store" << endl;
      int number = 0;
      cin >> number;
      if (number <= 101 && number >= 10 && number != data[0] && number != data[1] && number != data[2] && number != data[3] && number != data[4] && number != data[5])
      {

         data[a] = number; a++;
      }

      else
      {
         cout << "Incorrect entry\nPlease enter a number between 101 and 10!" << endl;
         i--;
      }
   }
   cout << data[0] << " " << data[1] << " " << data[2] << " " << data[3] << " " << data[4] << " " << data[5];
   
}
Last edited on
And how would someone put this in assending or desending order when it is displayed?
1
2
3
4
5
6
7
8
9
10
11
12
13
14


if(number <= 101 && number >= 10) // If it's not between 10 and 101 why even bother 
                                                           // checking if it's equal to any other data
{
    for(int j = 0; j < data.size(); j++) // if it is between 10 and 101. Then you can do this check using a for-loop
    {
        if(number != data[j]) //if the the number does not exist in the loop data vector, set data[a] to number
        {
             data[a] = number;
              a++;
        }
    }
}



I cooked this up in a minute haven't 100% checked if it worked, try it :)
Last edited on
Now I am having an issue passing my vector into a function. Any ideas?


#include "Vector.h"
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;

TestVector::TestVector()
{
cout << "Welcome to the six-element vector storage app!" << endl;
}

void TestVector::sixElementVector()
{
vector<int> data(6);

int a = 0;
for (int i = 0; i < data.size(); i++)
{
cout << "Please enter a number between 10 and 101"
<< " to store" << endl;
int number = 0;
cin >> number;
if (number <= 101 && number >= 10 && number != data[0] && number != data[1] && number != data[2] && number != data[3] && number != data[4] && number != data[5])
{

data[a] = number; a++;
}

else
{
cout << number << " is an invalid number \n\n Please reenter a number!" << endl;
i--;
}
}
cout << data[0] << " " << data[1] << " " << data[2] << " " << data[3] << " " << data[4] << " " << data[5] << endl;

searcha(data);
}

void TestVector::searcha(const vector<int> &v)
{
cout << "Please enter the interger you are searching for in the vector" << endl;

int searchNum = 0;
cin >> searchNum;

for (int i = 0; i < v.size(); i++)
{
if (searchNum == v[i])
{
cout << searchNum << " is one of the numbers in the array!" << endl;
}

else
{
cout << searchNum << " is not in the array!" << endl;
}
}


}

TestVector::~TestVector()
{
cout << "Thank you and Goodbye!!" << endl;
}

-----------------------------------------------------------------------
HEADER FILE

#pragma once
#include <iostream>
#include <vector>
#include <cstdlib>
class TestVector
{
public:
TestVector();
void sixElementVector();
void searcha(const vector<int>&);
~TestVector();
};


Please edit your post and use code tags.

Now I am having an issue passing my vector into a function. Any ideas?


Can you define problem? Are you getting errors? in that case please copy paste the full error messages.
Topic archived. No new replies allowed.