473,813 Members | 3,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(scani n) ;

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

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

Jan 19 '06 #1
13 2272
* 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(scani n) ;

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(symbo l,"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(symbo l,"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(scani n) ;

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

}
cout << low << endl ;
}
int rng ;
int low_range(char symbol)
{
if(strcmp(symbo l,"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<cstrin g> 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

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

Similar topics

6
12485
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. The whole program is part of an example found in my course work. Does strcmp only compare two sets of strings or can it be used to determine the end of the string as well? #include<iostream>
3
17134
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 std::strings with operator==(). In both programs, the first string consisted of one million characters (all the letter 'a'). The second string was always one character longer than the first string (with the letter 'a' for all the
11
5888
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
1788
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
5279
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 w1 and w2 make up the first string and, w3 and w4 make up the second string. I do not want to allocate memory, then put the words together to create a string only to facilitate strcmp() comparison. My question; Does anyone know how to get the...
36
3215
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 the needed data. Any ideas? Here's the code in question, see any reason why a command would not trigger the 'kbhit' the first time a serial command is sent?: Thanks! Chuck **************************************************** while(1) //...
0
2189
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
3033
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 course the reason for doing this is to save time in case equal pointers are passed to strcmp. But it seems to me that this could create an inconsistency in the degenerate case when s1 points to memory that is not null-terminated, i.e. by some freak...
2
2093
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 */
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9607
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10407
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9222
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5705
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3885
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.