3 Replies - 934 Views - Last Post: 22 May 2009 - 02:01 AM Rate Topic: -----

#1 dwade3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 21-May 09

How to fix this error

Post icon  Posted 22 May 2009 - 12:53 AM

I keep getting the following error:
"cannot convert 'std::string' to 'int' for argument '1' to 'int linearSearch(int, int*, int)'".

Basically my main arrays consists of:
string studentID[] = {"P1001", P"1002"}
int studentMark[] = {78.50, 66}

I am trying to allow the user to search for a given studentID, and the output will display the correponding marks for it. I am trying to use linear search (not sure if Im doing it right or not)

My coding looks like this at the moment:
#include <iostream>
#include <string>
using namespace std;

int linearSearch(int, int [], int);

int main()
{
	int menu;
	string studentID[] = {"P1001", "P1002"};
	float studentMark[] = {78.50, 66};
	int totalStudents=0;
	float totalMarks=0;
	float averageMark;
	string id;
	int mark;
	char studentGrade;
	int j;
	int i=linearSearch(id,studentMark,2);
  
	cout << "MAIN MENU\n"
		 << "0. Exit		 1. Statistics\n"
		 << "2. Enter mark   3. Find mark\n"
		 << "------------------------------\n"
		 << "Your choice -> ";
	cin >> menu;
  while (menu != 0)
  {
	switch (menu)
	{
		   case 1:
				for(int j=0;j<2;j++)
				 {
				   totalMarks+=studentMark[j];
				   totalStudents++;
				 }
				 averageMark=totalMarks/totalStudents;
				 cout << "Number of records: " << totalStudents <<endl;
				 cout << "Mean or average: " << averageMark <<endl;
				break;
		   case 2:
				cout << "Enter a Student Record: " <<endl;
				cout << "Student ID -> ";
				cin >> id;
				break;
		   case 3:
				cout << "Find marks for ID -> " << endl;
				cin >> id;
				
				else
				   cout << "Student Record does not exist";
				break;
		   default:
				   cout << "Invalid selection. Please make a selection between 0-3.\n"
						<< endl;
				   break;
	}	
system("Pause");
	cout << "MAIN MENU\n"
		 << "0. Exit		 1. Statistics\n"
		 << "2. Enter mark   3. Find mark\n"
		 << "----------------------------\n"
		 << "Your choice -> ";
	cin >> menu;
  }
  
return 0;
}



How can I fix the error?

Is This A Good Question/Topic? 0
  • +

Replies To: How to fix this error

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5309
  • View blog
  • Posts: 27,210
  • Joined: 10-May 07

Re: How to fix this error

Posted 22 May 2009 - 12:59 AM

View Postdwade3, on 22 May, 2009 - 01:53 AM, said:

"cannot convert 'std::string' to 'int' for argument '1' to 'int linearSearch(int, int*, int)'".


You declare id as string, & then pass it to linearSearch. When you defined linearSearch you declared it to take argument 1 as an int.

Quote

int linearSearch(int, int [], int); 
...
	string id; // <-- STRING

	//			 ... id is string... passed as argument 1
	int i=linearSearch(id,studentMark,2);





That will give you the error you are receiving. You need to either declare linearSearch to take argument one as a string, or properly declare your variable (id) as an int.
Was This Post Helpful? 0
  • +
  • -

#3 dwade3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 21-May 09

Re: How to fix this error

Posted 22 May 2009 - 01:09 AM

View Postno2pencil, on 21 May, 2009 - 11:59 PM, said:

View Postdwade3, on 22 May, 2009 - 01:53 AM, said:

"cannot convert 'std::string' to 'int' for argument '1' to 'int linearSearch(int, int*, int)'".


You declare id as string, & then pass it to linearSearch. When you defined linearSearch you declared it to take argument 1 as an int.

Quote

int linearSearch(int, int [], int); 
...
	string id; // <-- STRING

	//			 ... id is string... passed as argument 1
	int i=linearSearch(id,studentMark,2);





That will give you the error you are receiving. You need to either declare linearSearch to take argument one as a string, or properly declare your variable (id) as an int.


I cannot declare ID as int because it consists both a char and int ("P1001").

Anyways I have fixed that error abit now. But now I get more errors with more invalid conversions, such as the following:

49 invalid conversion from `std::string*' to `char'
49 initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator= (_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
49 could not convert `(&i)->std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((int)((char)(+&studentID))))' to `bool'
50 no match for 'operator[]' in 'studentID[i]'

#include <iostream>
#include <string>
using namespace std;

string linearSearch(string, string [], int);

int main()
{
	int menu;
	string studentID[] = {"P1001", "P1002"};
	float studentMark[] = {78.50, 66};
	int totalStudents=0;
	float totalMarks=0;
	float averageMark;
	string id;
	int mark;
	char studentGrade;
	int j;
	string i=linearSearch(id,studentID,2);
  
	cout << "MAIN MENU\n"
		 << "0. Exit		 1. Statistics\n"
		 << "2. Enter mark   3. Find mark\n"
		 << "------------------------------\n"
		 << "Your choice -> ";
	cin >> menu;
  while (menu != 0)
  {
	switch (menu)
	{
		   case 1:
				for(int j=0;j<2;j++)
				 {
				   totalMarks+=studentMark[j];
				   totalStudents++;
				 }
				 averageMark=totalMarks/totalStudents;
				 cout << "Number of records: " << totalStudents <<endl;
				 cout << "Mean or average: " << averageMark <<endl;
				break;
		   case 2:
				cout << "Enter a Student Record: " <<endl;
				cout << "Student ID -> ";
				cin >> id;
				break;
		   case 3:
				cout << "Find marks for ID -> " << endl;
				cin >> id;
				if(i = studentID)
				cout << "Student ID: " << studentID[i] <<endl;
				else
				cout << "Record does not exist";
				break;
		   default:
				   cout << "Invalid selection. Please make a selection between 0-3.\n"
						<< endl;
				   break;
	}	
system("Pause");
	cout << "MAIN MENU\n"
		 << "0. Exit		 1. Statistics\n"
		 << "2. Enter mark   3. Find mark\n"
		 << "----------------------------\n"
		 << "Your choice -> ";
	cin >> menu;
  }
  
return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to fix this error

Posted 22 May 2009 - 02:01 AM

Also see answers at your duplicate posting here
http://www.dreaminco...h...06428&st=0#
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1