Click here to Skip to main content
15,886,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
	clrscr();
	int a,c;
	char string[10];
	cout<<"\nEnter the numbers:";
	cin>>a;
	for(int i=0;i<a;i++)>
	{
		cin>>string[i];
	}
	for(int j=1;j<a;j++)>
	{
		c=strlen(string[j]);
	}
	cout<<c;
	getch();
}


What I have tried:

I am trying to get the length of each string. When I run the program, I get an error

"Cannot convert int to const char *" on line c=strlen...

Can someone help resolve this issue.

If I put for(int j=0,k=0;j<a,k<a;j++,k++)>>
{c[k]=strlen(string[j]);
}
will this work?
Posted
Updated 24-Jul-16 7:07am
v5
Comments
jeron1 22-Jul-16 13:36pm    
What is it you are trying to do exactly and what is 'char string[10];' supposed to represent?
OriginalGriff 22-Jul-16 14:08pm    
It's a C style array of ten char values, called "string"
jeron1 22-Jul-16 14:19pm    
I know that, but I don't believe the OP does. I was trying to get them to describe thier thought process behind that statement, so it could be corrected.
Philippe Mori 22-Jul-16 14:02pm    
Bad idea to name a variable string as in C++, this is the name of a predefined class.
Philippe Mori 22-Jul-16 14:08pm    
Since that code does not make much sense, it would be useful if you would tell us what you are trying to do.
You could split the line on which you got the error into 2 lines if you are not able to figure out what's the problem. By the way, the problem would be more obvious with the complete error message.
What kind of input, do you want to read (characters or strings)?
What value do you want to output? length of last string, total length of all string?

Well...it's right.
You declare string as:
C++
char string[10];
Which is an array of ten char values.
But then you try to pass one of the elements to strlen:
C++
c=strlen(string[j]);
Which is the equivalent of writing:
C++
c=strlen('X');
Since strlen expects a pointer to a constant char - i.e. a pointer to a char effectively - the system complains that it can't cast the char to a pointer to a char.
Quite what that code is meant to do, I'm not sure - but you can't get a length of a string from a single character!
And to be honest, that code doesn't do anything particularly useful:
C++
for(int j=1;j<a;j++)>
{
    c=strlen(string[j]);
}

would always leave c containing the last value returned by strlen if it did work.

I think you need to step back and think about the problem you are trying to solve for a bit before you rush into code - you aren't writing anything that makes much sense at the moment.
 
Share this answer
 
Comments
Member 11735960 22-Jul-16 21:53pm    
Well...my aim is to input all the strings and display each string's length as output one by one. How can this be done?
OriginalGriff 23-Jul-16 5:46am    
As I said: step back and think about it for a couple of minutes.
If you want to store multiple strings, what do you need to store them in?
If you want to output multiple values, where do you need to do the output?

This isn't difficult, not really - it just seems like it because you are trying to rush it instead of thinking first, then coding. Give it a try!
Member 11735960 23-Jul-16 7:16am    
I'll give it a try and reach back.
string is a reserved word and can't be used as a variable name. Change the variable name

for other problems you have, use the debugger to see what your code do.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 
Obviously, you want an array of strings:
C++
char string[10][10];

But this is not safe as it would fails if either you enter more than 10 strings or if any of those are longer that 10 characters including null terminator.

So you should add validation that the number entered by the user is less than the array size. Better yet would be to use std::vector<> instead.

Then for each string, you should really use std::string instead of C style string. That would fix the problem of entering entering too much data. By the way, if you modify the code to use std::string, you might have to rename variable string to something else to avoid ambiguity (in fact, it would be a good idea to do it anyhow).

Then, if you want to output the length of each string, the code for outputting those length should be inside the loop. Also, second loop should start at 0 to output the length of the first string. You probably also want to add either a space or a new line between each outputted number.

And finally, if you update the code to use std::string, you should call member function length() to get the length.

Also, you should really include standard headers whenever possible like
C++
#include <string> // Note the absence of .h suffix</string>

And then either add a using statement for std namespace or qualify all names manually.

I would recommand you to read good books on C++ and to avoid mixing C style code with C++ code.

About the error
First of all, you should learn to correctly read error message as the problematic conversion is between char (a single char from the array) and const char *(the expected parameter of strlen).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900