473,888 Members | 1,583 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
13 2276

"Shark" <cp*******@yaho o.com> skrev i meddelandet
news:11******** *************@g 49g2000cwa.goog legroups.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 responsibilitie s, 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 responsibilitie s. 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.programmin g].

--
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
12494
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
17144
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
5894
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
1791
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
5283
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
3225
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
2193
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
3042
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
2098
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
9961
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
11180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9597
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
7990
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
7148
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
5817
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4642
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

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.