在strcmp中有疑问 [英] doubt in strcmp

查看:112
本文介绍了在strcmp中有疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我是C ++初学者。我正在使用g ++编译器。下面是我的代码

,它给出的错误是从''char''转换为''char char'''enlid转换为
..Plz帮我解决这个问题。


#include< iostream。 h>

#include< string.h>

int low_range(字符号);


int main(int argc ,char ** argv)

{

char scanin;

浮动范围;


浮动low = 0.0;

浮动高= 1.0;


cout<< 输入符号\t ;

cin>> scanin;

while(scanin!=''Z'')

{

range = high-low;


low =低+范围* low_range(scanin);


cout<< " Value\t" ;

cin>> scanin;


}

cout<<低<<结束;

}

int rng;

int low_range(字符号)

{

if(strcmp(symbol," B")== 0)//这是行

给出错误

rng = 0.2;

cout<< 低范围。 << rng<< endl;

}

Hi friends,
I am beginner in C++. I am using g++ compiler. below is my code
which gives error as " invlid conversion from ''char'' to ''const char*''
..Plz help me with this.

#include <iostream.h>
#include <string.h>
int low_range(char symbol) ;

int main(int argc, char **argv)
{
char scanin ;
float range ;

float low = 0.0 ;
float high = 1.0 ;

cout << "Enter symbol\t" ;
cin >> scanin ;
while(scanin != ''Z'')
{
range = high-low ;

low = low + range * low_range(scanin) ;

cout << "Value\t" ;
cin >> scanin ;

}
cout << low << endl ;
}
int rng ;
int low_range(char symbol)
{
if(strcmp(symbol,"B")==0) //This is the line
which gives error
rng = 0.2 ;
cout << "Low range\t" << rng << endl ;
}

推荐答案

* Sameer:

#include< iostream.h>


这不是标准标题;它不适用于所有编译器。


相反,使用标准


#include< iostream>


#include< string.h>
int low_range(char符号);

int main(int argc,char ** argv)
{< br。> char scanin;
浮动范围;

浮动低= 0.0;
浮动高= 1.0;

cout<< 输入符号\t ;


当使用标准< iostream>时,你需要在这里写std :: cout

(或者,可能导致错误,有某处使用声明。


cin>> scanin;
while(scanin!=''Z'')
{
range = high-low;

low = low + range * low_range(scanin) ;

cout<< " Value\t" ;
cin>> scanin;

}
cout<<低<< endl;
}
int rng;


不要使用全局变量,特别是不是未初始化的变量。


看来这真的是作为一个局部变量功能

以下。


int low_range(字符号)
{
if(strcmp(symbol," B")= = 0)//这是给出错误的行


你要比较一个''char''和一个指向char的指针。


可能你的意思是


if(symbol ==''B'')


rng = 0.2;
cout << 低范围。 << rng<<结束


不要在计算某事的函数中输出。

}

#include <iostream.h>
This is not a standard header; it''s not available with all compilers.

Instead, use standard

#include <iostream>

#include <string.h>
int low_range(char symbol) ;

int main(int argc, char **argv)
{
char scanin ;
float range ;

float low = 0.0 ;
float high = 1.0 ;

cout << "Enter symbol\t" ;
When using standard <iostream>, you''ll need to write std::cout here
(or, likely to lead to bugs, have a ''using'' declaration somewhere).

cin >> scanin ;
while(scanin != ''Z'')
{
range = high-low ;

low = low + range * low_range(scanin) ;

cout << "Value\t" ;
cin >> scanin ;

}
cout << low << endl ;
}
int rng ;
Don''t use global variables, especially not uninitialized ones.

It seems that this was really meant as a local variable in the function
below.

int low_range(char symbol)
{
if(strcmp(symbol,"B")==0) //This is the line which gives error
You''re comparing a ''char'' with a pointer to char.

Possibly you meant

if( symbol == ''B'' )

rng = 0.2 ;
cout << "Low range\t" << rng << endl ;
Don''t do output in a function that computes something.
}




在这里你没有''return''语句来提供函数的结果

值。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



Here you lack a ''return'' statement to provide the function''s result
value.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Sameer写道:
嗨朋友们,
我是C ++的初学者。我正在使用g ++编译器。下面是我的代码
,它给出的错误是从''char''到''const char''的invlid转换。
.Plz帮我解决这个问题。


您正在比较C字符串(char数组)和char。

int low_range(char符号)
{
if(strcmp(symbol," B")== 0)//这是行
Hi friends,
I am beginner in C++. I am using g++ compiler. below is my code
which gives error as " invlid conversion from ''char'' to ''const char*''
.Plz help me with this.
You''re comparing a C string (array of char) with a char.
int low_range(char symbol)
{
if(strcmp(symbol,"B")==0) //This is the line




试试:

if (''B''==符号)


BTW - 我不确定你的代码应该怎么做所以我不确定

这是您的代码的意图。



try:
if ( ''B'' == symbol )

BTW - I''m not sure what your code is supposed to do so I''m not sure that
this is the intent of your code.


> #include< iostream.h>
> #include <iostream.h>
#include< string.h>


不推荐使用这些标头。你应该#include< iostream>和

#include< string>而不是你拥有的那些

int low_range(字符号);

int main(int argc,char ** argv)
{
char scanin;
浮动范围;

浮动低= 0.0;
浮动高= 1.0;

cout<< 输入符号\t ;


小问题:你应该使用std :: endl而不是\t来刷新

输出流。

cin>> scanin;
while(scanin!=''Z'')
{
range = high-low;

low = low + range * low_range(scanin) ;

cout<< " Value\t" ;
cin>> scanin;

}
cout<<低<< endl;
}
int rng;
int low_range(char符号)
{
if(strcmp(symbol," B")== 0)//这是产生错误的行
#include <string.h>
these headers are deprecated. You should #include <iostream> and
#include<string> instead of the ones you have
int low_range(char symbol) ;

int main(int argc, char **argv)
{
char scanin ;
float range ;

float low = 0.0 ;
float high = 1.0 ;

cout << "Enter symbol\t" ;
small issue here: you should use std::endl instead of \t to flush the
output stream.
cin >> scanin ;
while(scanin != ''Z'')
{
range = high-low ;

low = low + range * low_range(scanin) ;

cout << "Value\t" ;
cin >> scanin ;

}
cout << low << endl ;
}
int rng ;
int low_range(char symbol)
{
if(strcmp(symbol,"B")==0) //This is the line
which gives error




是的。基本上你试图比较char

类型的符号到B。这是一个c弦。 strcmp的签名是:

int strcmp(const char * string1,const char * string2); //使用两个

c风格的字符串


编译器希望第一个参数是const char *类型,但是

你给它的第一个参数是一个char。所以编译器说:

duh!你给了我一个char但我需要一个const char *!错误!!!


基本上当你将参数传递给函数时,C ++编译器需要

以确保它们与参数的类型相同。如果它们不是
,则它会隐式地调用复制构造函数。 (查一查,

虽然我可能错了)。如果复制构造函数无法完成其工作,那么没有解决方案,因此编译器会发出错误。


另外,要使用strcmp,你需要#包括< CString的GT;因为它是一个

c函数,而不是C ++



Yes. basically you are trying to compare symbol which is of type char
to "B" which is a c-string. The signature of strcmp is:
int strcmp ( const char * string1, const char * string2 ); //uses two
c-style strings

The compiler wants the first argument to be of type const char*, but
the first argument you are giving it is a char. So the compiler says:
duh! you gave me a char but I need a const char*! ERROR!!!

Basically when you pass arguments to functions, the C++ compiler wants
to make sure that they are of the same type as the parameters. If they
are not, it calls the copy constructor "implicitly" (look it up,
although I may be wrong). If the copy constructor fails to do its job,
there is no solution and thereupon the compiler spits out errors.

Also, to use strcmp you need to #include<cstring> as it is a
c-function, not C++
hope this helps.


这篇关于在strcmp中有疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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