如何使它在C ++中工作? [英] How do I make it work in C++?

查看:72
本文介绍了如何使它在C ++中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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();
}





我的尝试:



我试图获取每个字符串的长度。当我运行程序时,我收到一个错误



无法将int转换为const char *在行c = strlen ...



有人可以帮助解决这个问题吗。



如果我选择(int j = 0,k = 0; j< a, k< a; j ++,k ++)>>

{c [k] = strlen(string [j]);

}

这会有效吗?



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?

推荐答案

嗯......没错。

你声明 string as:

Well...it's right.
You declare string as:
char string[10];

这是一个包含10个 char 值的数组。

但是你试图通过其中一个要素 strlen

Which is an array of ten char values.
But then you try to pass one of the elements to strlen:

c=strlen(string[j]);

这相当于写作:

Which is the equivalent of writing:

c=strlen('X');

因为 strlen 需要一个指向常量 char <的指针/ code> - 即指向 char 的指针 - 系统抱怨它无法转换 char 指向 char 的指针。

这段代码的意思是什么,我不确定 - 但你不能从单个字符中获取字符串的长度!

说实话,该代码没有做任何特别有用的事情:

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:

for(int j=1;j<a;j++)>
{
    c=strlen(string[j]);
}



将始终保留 c ,其中包含<$ c返回的最后一个值$ c> strlen
如果它确实有效。



我认为你需要退一步思考你想要解决的问题在您急于编写代码之前 - 您现在没有编写任何有意义的内容。


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.


string 是保留的单词并不能用作变量名。更改变量名称



以解决其他问题,使用调试器查看代码的作用。

调试器允许您关注逐行执行,检查变量,你会看到有一点它停止了你所期望的。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]
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[^]


显然,您需要一个字符串数组:

Obviously, you want an array of strings:
char string[10][10];



但这不安全,因为如果您输入超过10个字符串或任何字符串,它将失败其中包括10个字符,包括空终止符。



因此,您应该添加验证,即用户输入的数字小于数组大小。更好的是使用 std :: vector<> 代替。



然后对于每个字符串,你应该真的使用 std :: string 而不是C样式字符串。这将解决输入太多数据的问题。顺便说一句,如果您修改代码以使用 std :: string ,则可能必须将变量 string 重命名为别的东西,以避免歧义(事实上,无论如何,这是一个好主意)。



然后,如果你想输出每个字符串的长度,输出那些长度的代码应该在循环内部。此外,第二个循环应从0开始输出第一个字符串的长度。您可能还想在每个输出的数字之间添加空格或新行。



最后,如果您更新代码以使用 std :: string ,你应该调用成员函数 length()来获取长度。



此外,你应该尽可能包括标准标题,例如


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

#include <string> // Note the absence of .h suffix</string>



然后为 std使用语句添加 命名空间或手动限定所有名称。



我建议你阅读有关C ++的好书并避免将C样式代码与C ++代码混合。



关于错误

首先,您应该学会正确阅读错误消息,因为有问题的转换介于 char 之间(数组中的一个char)和 const char * (strlen的预期参数)。


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


这篇关于如何使它在C ++中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆