# Solved: Conversion issues in C++ ???



## Hillian (Apr 12, 2004)

My code is as follows:

// hw9.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

/*

CSI 1007 
HW9 
Due: 4/20/07 
Brian Bertalot 
Teacher: C. Kwiatkowski

Purpose: The purpose is to read in from a file, a list of an unknown amount of
integers. After read in, the program will sort the data using a bubble
sort to sort the data in ascending order. From there, the program will
ask a user to input a integer and will search for that specific 
integer using a binary search. If the integer is found, it'll return a
message proclaiming it was found in the list of data, but if the
integer was not found in the list, it'll return a message saying so.

Input: Data from the text file(input.txt)containing an unknown number of 
integers.

Output: The program outputs whether or not a user specified value is located in
the vector.

*/

//Function Prototypes
int isSorted(vector<int>&vecList,int length);
void reverse(vector<int>&vecList,int length);
void bubbleSort(vector<int>&vecList,int length);
int binarySearch(const vector<int>&vecList,int length,int searchItem);

int _tmain(int argc, _TCHAR* argv[])
{
//declare vector
vector<int> vecList;

//declare temp variable
int temp;
size_t length;
int searchItem;

//File Variables
ifstream infile;

//Open File
infile.open("input.txt");

//read ints into vector
while (!infile.eof())
{
infile >> temp;
vecList.push_back(temp);
}

length = vecList.size();

isSorted(&vecList,length);

if (isSorted == 2)
reverse(&vecList,length);
else if (isSorted == 0)
bubbleSort(&vecList,length);
else
binarySearch(&vecList,length,searchItem);

return 0;
}

/*
Function: int isSorted(vector<int>&vecList,int length)
Purpose: to check and see if the vector is sorted in ascending order 
Input: the vector vecList and the length of the vector
Output: returns a 2 is the list is in descending order, a 0 if the list
is in no order, or a 1 id the list is sorted in ascending order
*/

int isSorted(vector<int>&vecList,int length)
{
int i;
int sortA = 1;
int sortD = 1;

if (length < 2)
return 1;

for (i = 0; i <= length - 2; i++)
{
if (vecList_ > vecList[i + 1])
sortA = 0;
if (vecList < vecList[i + 1])
sortD = 0;
}

if (sortA == 1)
return 1;
else if (sortD == 1)
return 2;
else
return 0;
}

/*
Function: void reverse(vector<int>&vecList,int length) 
Purpose: the list is in descending order, this function turns the list
around and makes the list into ascending order instead
Input: the vector vecList and the length of the vector
Output: none-void function 
*/

void reverse(vector<int>&vecList,int length)
{
int i, temp;

for (i = 0; i <= (length - 1)/2; i++)
{
temp = vecList;
vecList= vecList[length - 1 - i];
vecList[length - 1 - i]=temp;
}
}

/*
Function: void bubblesort(int mainVector[],int length)
Purpose: to go through the vector and list the numbers in ascending order 
Input: the vector of data and the integer length(size of vector) 
Output: none-void function
*/

void bubbleSort(vector<int>&vecList,int length)
{
int temp = 0;
int iteration = 0;
int index = 0;

for (iteration = 1; iteration < length; iteration++)
{
for (index = 0; index < length; index++)
if(vecList[index] < vecList[index + 1])
{
temp = vecList[index];
vecList[index] = vecList[index + 1];
vecList[index + 1] = temp;
}
}
}

/*
Function: int binarySearch(const vector<int>&vecList,int length,
int searchItem) 
Purpose: to do a binary search to check to see if a user specified integer
is in the vector list 
Input: the vector vecList,the length of the vector, and searchItem, the 
user specified integer 
Output: a -1 if the searchItem don't match a integer in the vector or the
location of the searchItem
*/

int binarySearch(const vector<int>&vecList,int length,int searchItem)
{
int first = 0;
int last = (length - 1);
int mid;

bool found = false;

while (first <= last && !found)
{
mid = (first + last) / 2;

if (vecList[mid] == searchItem)
found = true;
else if (vecList[mid] > searchItem)
last = (mid - 1);
else
first = (mid + 1);
}

if (found)
return mid;
else
return -1;
}

I keep getting these errors dealing with conversions:

c:\Documents and Settings\Brian Bertalot\My Documents\Visual Studio Projects\hw9\hw9.cpp(74): error C2664: 'binarySearch' : cannot convert parameter 1 from 'std::vector<_Ty> *__w64 ' to 'const std::vector<_Ty> &'
Reason: cannot convert from 'std::vector<_Ty> *__w64 ' to 'const std::vector<_Ty>'
No constructor could take the source type, or constructor overload resolution was ambiguous

c:\Documents and Settings\Brian Bertalot\My Documents\Visual Studio Projects\hw9\hw9.cpp(69): error C2446: '==' : no conversion from 'int' to 'int (__cdecl *)(std::vector<_Ty> &,int)'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

c:\Documents and Settings\Brian Bertalot\My Documents\Visual Studio Projects\hw9\hw9.cpp(69): error C2040: '==' : 'int (__cdecl *)(std::vector<_Ty> &,int)' differs in levels of indirection from 'int'
with
[
_Ty=int
]
c:\Documents and Settings\Brian Bertalot\My Documents\Visual Studio Projects\hw9\hw9.cpp(70): error C2664: 'reverse' : cannot convert parameter 1 from 'std::vector<_Ty> *__w64 ' to 'std::vector<_Ty> &'
A reference that is not to 'const' cannot be bound to a non-lvalue_


----------

