ERROR IN FUNCTION INCLUDING STRINGS

Line 27 conversion from `std::string*' to non-scalar type `std::st

Page 1 of 1

7 Replies - 711 Views - Last Post: 27 April 2009 - 04:40 PM Rate Topic: -----

#1 TrinLH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-April 09

ERROR IN FUNCTION INCLUDING STRINGS

Posted 27 April 2009 - 11:30 AM

Hi all,

Trying to write the below program and having a problem compiling. Getting the following error: Line 27 conversion from `std::string*' to non-scalar type `std::string' requested .

Really need help as have to have this program done for tomorrow. Anyone any suggestions?

Thanks!

//Election results//
#include <iostream>
#include <cstring>

using namespace std;
const int ward=10;

void read_votes (int votes[][ward], string names[], int size);
void read_names (string names[], int max_size, string sentinel, int& size);
int search (string names[], string key, int size);
void total_votes (int totals[], int votes[][ward], int size);
void print_results (string names, int votes[][ward], int totals[], int size);

int main(){
	
	const int MAX_SIZE=50;
	const string sentinel= "xxx";
	int size=0;
	
	string names[MAX_SIZE];
	int votes[MAX_SIZE][ward];
	int totals[MAX_SIZE];
	
	read_names (names, MAX_SIZE, sentinel, size);
	read_votes (votes, names, size);
	total_votes (totals, votes, size);
	print_results (names, votes, totals, size);
	
	
	
	
	 system ("PAUSE");
	
	return 0;
	
}

//void read_votes (double votes[]ward[], string candidate_name){
	 
void read_names (string names[], int max_size, string sentinel, int& act_size){
	 int i=0;
	 string n;
	 cin>>n;
	 while (i < max_size && n!= sentinel){
		   names[i]=n;
	 i++;
	 act_size++;
	 cin>>n;
	 }
	 return;
	 }
	 
int search (string names[], string key, int size){
	for (int i=0; i<size; i++){
		if (names[i]== key)
		return i;
		else return -1;
		}
}

void read_votes (int votes[][ward], string names[], int size){
	 int ward;
	 cin>>ward;
	 while (ward != -1){
		   if (ward<=10){
		   string test_name;
		   cin>> test_name;
		   int p= search (names, test_name, size);
			   if (p == -1){ 
				  cout<<"Error: Name not amoung candidates"<<endl;
				  return;}
		   int num_votes;
		   cin>>num_votes;
		   votes[p][ward]=num_votes;
		   }
		   else{
		   cout<<"Error: invalid ward"<<endl;
		   return;}
		   cin>>ward;
		   }
	 return;
	 }
	 
void total_votes (int totals[], int votes[][ward], int size){
	for (int a=0; a<size; a++){
	for (int i=0; i<size; i++){
	for (int j=0; j<ward; j++)
	totals[a]=votes[i][j];
}
}
	return;
}
	 
	 
void print_results (string names, int votes[][ward], int totals[], int size){
	 cout<<"			  ";
	 for (int b=1; b<=10; b++)
	 cout<<"b"<<"	";
	 cout<<"   "<<"TOTAL"<<endl;
	 for (int a=0; a<size; a++){
		 for (int n=0; n<size; n++){
		 cout<<names[n]<<"   ";
			  for (int i=0; i<size; i++){
				  for (int j=0; j<ward; j++){
		 
				   cout<<votes[i][j]<<"   ";
				   cout<<endl;
		 }
		 }
		 }
		 cout<<totals[a];
		 }
		 return;   
	 }


This post has been edited by TrinLH: 27 April 2009 - 12:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: ERROR IN FUNCTION INCLUDING STRINGS

#2 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: ERROR IN FUNCTION INCLUDING STRINGS

Posted 27 April 2009 - 11:32 AM

you need to include this at the top of your code
#include <string>



how to now declare a string
string names;



Do NOT declare a string like this
string names[MAX_SIZE];



This post has been edited by Notorion: 27 April 2009 - 11:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 TrinLH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-April 09

Re: ERROR IN FUNCTION INCLUDING STRINGS

Posted 27 April 2009 - 11:35 AM

View PostNotorion, on 27 Apr, 2009 - 10:32 AM, said:

you need to include this at the top of your code
#include <string>



how to now declare a string
string names;



Do NOT declare a string like this
string names[MAX_SIZE];




Oh even if I am declaring an array of type string?
Was This Post Helpful? 0
  • +
  • -

#4 TrinLH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-April 09

Re: ERROR IN FUNCTION INCLUDING STRINGS

Posted 27 April 2009 - 11:46 AM

View PostTrinLH, on 27 Apr, 2009 - 10:35 AM, said:

View PostNotorion, on 27 Apr, 2009 - 10:32 AM, said:

you need to include this at the top of your code
#include <string>



how to now declare a string
string names;



Do NOT declare a string like this
string names[MAX_SIZE];




Oh even if I am declaring an array of type string?



Oh, do I declare it like this even for an array of type string?
Was This Post Helpful? 0
  • +
  • -

#5 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: ERROR IN FUNCTION INCLUDING STRINGS

Posted 27 April 2009 - 12:13 PM

No you cannot declare an array of strings because a string is already an array of characters. You have to use a vector of strings or something similar to that to get an array of strings.
Was This Post Helpful? 0
  • +
  • -

#6 TrinLH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-April 09

Re: ERROR IN FUNCTION INCLUDING STRINGS

Posted 27 April 2009 - 12:19 PM

Right. We haven't covered vectors yet. From looking at the program I am trying to write any other suggestions of how I can store the names I need to read in if I can't have an array?

Thanks for the help
Was This Post Helpful? 0
  • +
  • -

#7 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: ERROR IN FUNCTION INCLUDING STRINGS

Posted 27 April 2009 - 12:21 PM

char* names[10] = {"Bob", "Bill", ... etc };


To read in a name just use cin.getline(BUFFER, LENGTH, DELIMITER-Optional)
Was This Post Helpful? 0
  • +
  • -

#8 mikeblas  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 44
  • View blog
  • Posts: 390
  • Joined: 08-February 08

Re: ERROR IN FUNCTION INCLUDING STRINGS

Posted 27 April 2009 - 04:40 PM

View Posteker676, on 27 Apr, 2009 - 11:13 AM, said:

No you cannot declare an array of strings because a string is already an array of characters. You have to use a vector of strings or something similar to that to get an array of strings.

Of course you can! An array of strings is an array of strings. A string is, in turn, an array of characters. The declaration you provide declares an array MAX_SIZE number of strings, where each string is dynamic in length with memory managed by the std::string objects. You don't need a vector to do this. Certainly I don't think it's what the original poster intended, but it certainly is legal.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1