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 <stdlib.h>
using namespace std;

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

	int lower, middle, upper;
	lower = 0;
	upper = size-1;
		middle = (lower + upper)/2;
		if (data[middle] == value)
			return middle;
		else if (lower >= upper)
			return -1;
		else if (data[middle] > value)
			upper = middle -1;
			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;
		cout << "Found at " << index <<endl;


Edited by Jackie91: n/a

8 Years
Discussion Span
Last Post by Lerner

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).

Edited by mike_2000_17: n/a


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];


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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.