473,320 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

doubt in strcmp

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 ;
}

Jan 19 '06 #1
13 2241
* Sameer:

#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.
}


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?
Jan 19 '06 #2
Sameer wrote:
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


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.
Jan 19 '06 #3
> #include <iostream.h>
#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


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.

Jan 19 '06 #4

Shark wrote:
#include <iostream.h>
#include <string.h>


these headers are deprecated. You should #include <iostream> and
#include<string> instead of the ones you have


<iostream.h> is not deprecated. As far as C++ is concerned,
<iostream.h> does not exist. You are correct though that the OP wants
<iostream> instead.

But the OP's use of <string.h> is entirely correct. They are using
strcmp from the C string library which is declared in <string.h>.
<string> is the header for std::string which is completely different.
You could argue that the OP should prefer <cstring> to the deprecated
<string.h> but unfortunately I believe that many compilers do not
implement the contents of <cxxx> headers correctly so you might as well
stick with <xxx.h>

Gavin Deane

Jan 19 '06 #5
Gavin Deane wrote:
Shark wrote:
#include <iostream.h>
#include <string.h>
these headers are deprecated. You should #include <iostream> and
#include<string> instead of the ones you have


<iostream.h> is not deprecated. As far as C++ is concerned,
<iostream.h> does not exist. You are correct though that the OP wants
<iostream> instead.


Yes you are correct.

But code that includes iostream.h compiles none the less :) and these
headers are usually marked "deprecated" on gnu systems....so I thought
I'd use that word. :D
But the OP's use of <string.h> is entirely correct. They are using
strcmp from the C string library which is declared in <string.h>.
<string> is the header for std::string which is completely different.
You could argue that the OP should prefer <cstring> to the deprecated
<string.h> but unfortunately I believe that many compilers do not
implement the contents of <cxxx> headers correctly so you might as well
stick with <xxx.h>


hahaha. Well, I was looking at <string.h> right by <iostream.h>, so you
can't blame me for screwing up the context!

Jan 19 '06 #6
Sameer wrote:
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.

What's "Plz" ?

strcmp takes a const char* as its first parameter, you a passing a char.

Why not just use symbol == 'B'?

Ian

--
Ian Collins.
Jan 19 '06 #7
* Shark:

But code that includes iostream.h compiles none the less :)


With some compilers.

--
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?
Jan 19 '06 #8
Alf P. Steinbach wrote:
* Shark:

But code that includes iostream.h compiles none the less :)


With some compilers.


yea, but "most" of those "some" compilers are used to compile "most" of
the code. gcc & vc
:p (ok, hasty generalization on my part)

iostream.h and like will linger on for some time.

Jan 19 '06 #9
* Shark:
Alf P. Steinbach wrote:
* Shark:

But code that includes iostream.h compiles none the less :)


With some compilers.


yea, but "most" of those "some" compilers are used to compile "most" of
the code. gcc & vc


Note that modern versions of the vc compiler, from version 7.0 and
onwards (I think it was), do not provide <iostream.h>.

--
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?
Jan 19 '06 #10

"Shark" <cp*******@yahoo.com> skrev i meddelandet
news:11*********************@g49g2000cwa.googlegro ups.com...

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.


On the other hand, std::cin is by default tie()'d to std::cout, so it
will do this automagically anyway.
cin >> scanin ;

Bo Persson

Jan 19 '06 #11
Alf P. Steinbach wrote:
* Sameer:
cout << "Value\t" ;
cin >> scanin ;

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


Could you please provide the reason for the above statement?
It seems that this was really meant as a local variable in the function
below.

rng = 0.2 ;
cout << "Low range\t" << rng << endl ;


Don't do output in a function that computes something.


Why shouldn't we? What might be the ill-effect of doing so?

thanks,
--Wg-

Jan 20 '06 #12
* WittyGuy:
* Alf P. Steinbach:
* Sameer:
cout << "Value\t" ;
cin >> scanin ;

}
cout << low << endl ;
}
int rng ;


Don't use global variables, especially not uninitialized ones.


Could you please provide the reason for the above statement?


There is a conundrum. For the newbie, who most of all needs to avoid
global variables, nearly at any cost, the reasons (note: plural) to
avoid them are extremely hard to fathom. On the other hand, experienced
programmers who are able to understand the reasons usually do understand
them and are already avoiding use of globals, and need no explanation.

And we, or at least I, fear that most newbies who ask about this or ask
similar questions of a general nature will strenously object to any
reasons offered. It's the same as with use of 'goto'. Or any other
feature that is technically OK, and exists for very good reasons, but is
disastrous as general practice -- only meant for very special things.

Thirdly, it's not a question that's very much specific to C++; it
applies to any programming language, and so is mostly off-topic in
[comp.lang.c++].

So in explaining this we often resort to the old authority argument,
which is a fallacy; or we vaguely indicate that globals will rise up and
bite you in an area in front of your behind, at every opportunity,
without mentioning much how globals are able to do these gymnastic feats
(this is the approach of the FAQ, see the end of FAQ item 39.8 about
for-loop scoping rules, and also my approach above); or we keep silent.

However, having said A, I feel obliged to say B, when that B is asked
for, so, first about global variables in general, which you should not
think of as an exhaustive list, just what bubbled to the surface of my
mind first:

* Global variables are accessible from anywhere, which means you can't
rely on a global variable having the value you just put there, and
means you can generally have a hard time tracking down where a value
came from. And which means unclear responsibilities, in general.
All that in turn means hard to understand code, and it means bugs,
and it means hard to debug.

* Global variables are not thread-safe.

* Global variables are not even recursion-safe (an important special
case of the first point).

* Global variables in C++ are not exception safe, in the sense that
there's no portable way to catch an exception arising from the
initialization of a global variable.

* Global variables in C++ are not initialization-order safe; it's
possible to use a global variable before it's been initialized, even
when that global is declared with an initializer.

* Global variables lead to name clashes and inadvertent usage.

* Global variables may constrain your code to a single object or call
chain, which typically surfaces as an unsurmountable problem long
after you have written thousands of lines of code depending on the
global-variable-based code.

And so on.

Oh, I forgot, my comment about "especially not uninitialized ones".

Using an uninitialized variable is Undefined Behavior in C++, but that
does not apply directly to a global variable, because a global variable
with no initializer is zero-initialized. So except for the
initialization unsafety of globals (possible use before initialization)
there's not really such a thing as an uninitialized global. What my
comment referred to was lack of _explicit_ initialization.

Often that means that some function is supposed to be called before that
global is otherwise used, where that function will do the
initialization.

Tracking down which function that is can be hard; ensuring that the
global is not used before that function is called can be hard; and
ensuring that that function is actually called, only once, at the proper
time and in proper sequence with other functions, especially other such
functions, can be hard -- it's very unnecessary complexity.

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

rng = 0.2 ;
cout << "Low range\t" << rng << endl ;


Don't do output in a function that computes something.


Why shouldn't we? What might be the ill-effect of doing so?


Effects, plural. First and foremost, consider making a GUI (window)
version of the program. Can that function be used unchanged?

Secondly, consider reuse in general: can that function be reused
somewhere (even within the same program) where that exact output, or any
output, is undesired?

Third, consider the effect on data flow, and thus design. Data is
passed down into functions, but results never come back up: they're just
displayed. That leads to very control-flow oriented design, with an
all-important main function somewhere coordinating it all, taking on
more and more responsibilities. Which is incompatible with OO and is a
well known anti-pattern, known as "the blob" or "the octopus", IIRC.

Note again, that this is not very C++-specific, and so is largely
off-topic in [comp.lang.c++].

Follow-ups therefore set to [comp.programming].

--
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?
Jan 20 '06 #13

Shark wrote:
#include <iostream.h>
#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.


Nitpick: std::endl and \t will result in totally different output. You
can't use std::endl in place of \t. If you need a flush at this point
use flush.

I believe you expected to see \n.

Jan 20 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: muser | last post by:
The following error appears: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'. I've already tried using single quotations. the header file only contains the struct contents....
3
by: jl_post | last post by:
Hi, I recently wrote two benchmark programs that compared if two strings were equal: one was a C program that used C char arrays with strcmp(), and the other was a C++ program that used...
11
by: Eirik | last post by:
Shouldn't this code work? If not, why shouldn't it? #include <stdio.h> int main(void) { char yesno; char *yes = "yes";
8
by: junky_fellow | last post by:
what would be the output for the following piece of code ? if ( "hello" == "hello" ) printf("True\n"); else printf("False\n"); What is the reason for that ?
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
36
by: Chuck Faranda | last post by:
I'm trying to debug my first C program (firmware for PIC MCU). The problem is getting serial data back from my device. My get commands have to be sent twice for the PIC to respond properly with...
0
by: noobcprogrammer | last post by:
#include "IndexADT.h" int IndexInit(IndexADT* word) { word->head = NULL; word->wordCount = 0; return 1; } int IndexCreate(IndexADT* wordList,char* argv)
47
by: fishpond | last post by:
One way I've seen strcmp(char *s1, char *s2) implemented is: return immediately if s1==s2 (equality of pointers); otherwise do the usual thing of searching through the memory at s1 and s2. Of...
2
by: thungmail | last post by:
There is partial code in C typedef struct message { int messageId; char *messageText; struct message *next; }message; ..... ..... ..... /* Get a node before a node */
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.