I was having an error, it says cannot convert string into int..i have no idea how to convert string into int. My teacher asked me to read 5 students names into the array..help me pls..

#include<iostream>
#include<string>
#include <stdlib.h>
using namespace std;


int search(int data[], int size, int value)
{

	
	int lower, middle, upper;
	lower = 0;
	upper = size-1;
	
	while(true)
	{
		middle = (lower + upper)/2;
		if (data[middle] == value)
			return middle;
		else if (lower >= upper)
			return -1;
		else if (data[middle] > value)
			upper = middle -1;
		else
			lower = middle +1;
	}
}

void main()
{
	string name[5];

	for (int i=0;i<5;i++)
	{
		cin >> name[i];
	}


	int index;
	int input;
	cout << "\n\nSearch = ";
	cin >> input;

	
	index = search(name,5,input);
	
	
	if (index == -1)
		cout << "Not Found "<<endl;
	else
		cout << "Found at " << index <<endl;

}

Please wrap code in tags next time..

Why do you need to get the numbers a string first? Just use an array of int for "names" and get them from cin directly as integers, just like you do with "input".

so change the type of "names" to "int names[5];" and it will work.

Also, the size of your array is 5 not 500 (the value to send to search).

Just use an array of int for "names" and get them from cin directly as integers, just like you do with "input".

By this,do you mean i have to change...

string name[5];

to...

int name[5];

because if i do this, i wouldn't be able to input characters...
and i need to enter characters for the student's name...

What does int data[] have to do with anything? And why is value being declared as an int if it's a string you want to be looking for? If value is the string you want to search for in an array of strings then pass an array of strings and a string to search for to a search function that expects those parameters, not an array of int and an int to search for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.