7 Replies - 3425 Views - Last Post: 26 November 2012 - 10:44 PM Rate Topic: -----

#1 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 08-October 12

cannot convert parameter 1 from 'std::string' to 'char *&#

Posted 19 November 2012 - 07:59 PM

Basically, I'm unable to call the function wordCount(), and I receive the error code posted in the thread title. Here is my code:

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

const char SPACE = ' ';

//Function Prototypes
int wordCount(char *); //This function counts and returns the number of words in the string passed into str.

int main()
{
	int strCount;
	string *str;

	cout << "Enter a string of 80 or fewer characters:" << endl;
	getline(cin, *str);

	strCount = wordCount(*str);
	cout << "The number of words in that string: " << strCount;
	
	system ("pause");
	return 0;
}

int wordCount(char *str)
{
	int count = 0;

	for (int i = 0; i<80; i++)
	{
		while (count == 0)
		{
			if (str[i] == NULL)
			{
				return count;
			}
			else if (str[i] != SPACE)
			{
				count=count+1;
			}
			else if (str[i] == SPACE)
			{
				i++;
			}
		}
	}	
}


I've been trying everything I could think of for the last 90 minutes, I've looked all around, and nothing seems to work. Any advice would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: cannot convert parameter 1 from 'std::string' to 'char *&#

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4234
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: cannot convert parameter 1 from 'std::string' to 'char *&#

Posted 19 November 2012 - 09:10 PM

Your error seems fairly self explanatory to me.

Quote

cannot convert parameter 1 from 'std::string' to 'char *


Your variable str is a pointer to a std::string not a character string (char *).

You need to decide whether you will be using std::string (recommended) or C-strings, and then either alter your functions to use std::string or use C-strings, there is a big difference between these two types of variables.

Also where are allocating memory for the pointer (*str)?

Jim
Was This Post Helpful? 2
  • +
  • -

#3 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 08-October 12

Re: cannot convert parameter 1 from 'std::string' to 'char *&#

Posted 19 November 2012 - 09:27 PM

Hmm, this is my assignment:

Quote

Write a function that accepts a pointer to a C-string as an argument and returns the number of words contained in the string. Demonstrate the function in a program that asks the user to input a string and then passes it to the function. The program should display the results.


I also need to use the function int wordCount(char *). So, I need to use c-strings and I believe I'm allocating memory for the pointer *str to save the user input. I'm very new to pointers, but part of the requirements is to accept a pointer to a c-string as an argument and return the number of words, which would be count. So far I haven't finished my function prototype, I'm just trying to actually call the function properly before I invest the time to make the prototype work. I'm searching through my textbook right now without much luck trying to find the correct syntax to point to a c-string and then to convert that if I need to.
Was This Post Helpful? 0
  • +
  • -

#4 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: cannot convert parameter 1 from 'std::string' to 'char *&#

Posted 19 November 2012 - 11:03 PM

First of all, 'str' doesn't need to be a pointer, it can just be a string object in this case and be perfectly fine. You can retrieve the C-style string from an std::string using the c_str() method.

e.g.
string str;
const char *cstr = str.c_str();


Also, it's very important to understand pointers and memory allocation before using them. For example, if the following code doesn't "ring alarm bells", you need to read up on pointers.

int *p;
*p = 10;

This post has been edited by Aphex19: 19 November 2012 - 11:08 PM

Was This Post Helpful? 2
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4234
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: cannot convert parameter 1 from 'std::string' to 'char *&#

Posted 20 November 2012 - 06:24 AM

Since your requirements state you need to use C-strings stick with them for the entire assignment. Don't confuse std::string with character strings (C-strings) there are major differences.

The following:
    string *str;

Creates an pointer to a string but doesn't actually allocate memory for this string. As already pointed out by Aphex19 you don't need a pointer when working with std::strings, just use a "normal" instance:
    string str;


However you probably should start using C-strings instead of the std::string.
char str[1024]; // Create character array to hold your information.

This will also necessitate changing your getline() to use the correct version of this program that is meant to work with this type of string.

You may also want to study the following links: Arrays, Character Sequences, and Pointers

Jim
Was This Post Helpful? 1
  • +
  • -

#6 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 08-October 12

Re: cannot convert parameter 1 from 'std::string' to 'char *&#

Posted 26 November 2012 - 08:41 PM

Alright, I'm calling the function now but every time I run the program the output I'm getting is the maximum size of the array (80). Also, after I receive this output, the program freezes. Here is my code:
 #include <iostream>
#include <string>
using namespace std;

const char SPACE = ' ';

//Function Prototypes
int wordCount(char *); //This function counts and returns the number of words in the string passed into str.

int main()
{
	char str[80]; //Store the user entered string.
	int strCount; 

	cout << "Enter a string of 80 or fewer characters:" << endl;
	cin.getline (str,80); //Take in the user entered string.
	
	strCount = wordCount(str);
	cout << "The number of words in that string: " << strCount;
	
	delete str;

	system ("pause");
	return 0;
}

int wordCount(char *str)
{
	int count = 0;

	for (int i = 0; i<80; i++)
	{
		while (count == 0)
		{
			if (str[i] == NULL)
			{
				return count;
			}
			else if (str[i] != SPACE)
			{
				count=count+1;
			}
			else if (str[i] == SPACE)
			{
				i++;
			}
		}
	}	
} 


And here is the output I'm getting:

Quote

Enter a string of 80 or fewer characters:
hello how are you
The number of words in that string: 80


So, am I calling the function incorrectly, storing the data incorrectly, or is the issue with the code within the function wordCount()? Thanks for the help so far!
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: cannot convert parameter 1 from 'std::string' to 'char *&#

Posted 26 November 2012 - 08:48 PM

You are not returning anything from your wordCount function. You need to return count from your function.

Link on return: http://www.cplusplus...rial/functions/

Quote

every time I run the program the output I'm getting is the maximum size of the array (80).

The 80 is not due to the maximum size of the array. You are actually getting lucky and you are pulling the temporary memory from your iterator "i" off the stack frame. This is happening due to the lack of a return statement.

delete str;


Get rid of this, you never explicitly delete anything off of the stack.

This post has been edited by jjl: 26 November 2012 - 08:57 PM

Was This Post Helpful? 1
  • +
  • -

#8 shadok  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 08-October 12

Re: cannot convert parameter 1 from 'std::string' to 'char *&#

Posted 26 November 2012 - 10:44 PM

Got it, and got the program working perfectly. Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1