Question:
Programming with C and C++?
Phani
2012-05-12 01:25:50 UTC
I made a C program using DevC++ in which it consists of the "code of the book" and the "book name" i.e.
if i type the code i will get the book name.

for ex:

#include
main()
{
int c;
printf("code: ");
scanf("%d",&c);
if(c==123) // if code is 123 book name is XYZ
printf("book name is: XYZ");
getch();
}
output is
code: 123
book name is: XYZ

This is done successfully

Now i want to enter the "book name" and get the "code" as output.

For ex:

#include
main()
char bname; // where bname is Book name
printf("enter book name: ");
gets(bname);
if(bname==XYZ) // the error goes here: XYZ undeclared
printf("code: 123);
getch();
}
This is not happening.

Here the compiler says "XYZ is undeclared"
so what should i do to get the required output.
please help me guys...
please suggest me a program for such situations
please guys...
Seven answers:
Casper
2012-05-12 02:17:48 UTC
your problems were

1 . char bname ; // you declared a character variable , it can hold a single character only. for example X in XYZ will be stored in bname. so YZ wont be there



solution is create an array like this . char bname [10];

2 . if(bname==XYZ)

you just given XYZ . while you provide string literals you have to put them between quotes " " . so change it to if(bname=="XYZ") but this wont work. because when you provide bname what you giving is starting address of bname array. so an address being provided. instead of the content. so solution is to use the header string.h and use the string comparing function strcmp. which will return 0 if both strings are equal . else the difference of those strings







try this..



#include

#include

int main()

{

char bname[10] ;

printf("enter book name: ");

gets(bname);

if(strcmp(bname,"XYZ")==0) ;

printf("code: 123");

return 0;

}



@henni : there is header file called stdio.h. don't talk about things that you are not sure about . ok

getch () exist in c and c++ . there are lots of compilers and libraries available buddy. the first and basic C and C++ have both facilities. use Turbo or ANSI C . you will come to know that.
peteams
2012-05-12 08:43:22 UTC
You are trying to compare the content of bname with the value XYZ and are getting an error when you write:



if (bname == XYZ)



That's because XYZ looks like the name of some data, like bname and the compiler does not know what XYZ is.



In many languages you would write that as:



if (bname == "XYZ")



The quotes around XYZ tell the compiler it is a value not a name. However in C that still will not work, instead you have to write the following:



if (strcmp(bname, "XYZ)) == 0)



That uses a function called strcmp to compare the value in bname with the value XYZ, if they are the same it returns 0, which is tested for with the == 0 clause.



Additionally bname need to be declared as follows:



char bname[100];



That says bname has room for 100 characters (I chose 100 fairly randomly). The value XYZ is actually four characters long! "XYZ" is represented by the three letter characters X, Y and Z plus an ending character called a null.
anonymous
2012-05-12 08:41:03 UTC
Firstly, this is C; C++ doesn't have a header file called "stdio.h" (although it does provide a header file with the same functionality, called "cstdio".)



Here are a few important things to know:



"getch();" - This function doesn't exist in C. Try running your program from cmd.exe or configuring Dev-C++ so that it runs the "pause" program after your program finishes execution.



"gets();" - This function was deprecated in C11 (for a very good reason - it causes buffer overflows). Implementations of C are no longer required to provide it.



So your goal is to read a book name and if it compares equal to "XYZ", you will print "code: 123". The problem with this is that you're trying to use a single character of storage to read a string. You need to store a string in an array of char. Try the following:



#include

#include



static int readline(char *s, size_t size)

{

        int c;



        size--;

        while ((c = getchar()) != EOF && c != '\n')

                if (size) {

                        *s++ = c;

                        size--;

                }

        *s = '\0';

        return c == EOF;

}



int main(void)

{

        char buf[4];



        if (readline(buf, sizeof buf))

                return 0;

        if (!strcmp(buf, "XYZ"))

                printf("Code: 123\n");

        return 0;

}



Don't focus too much on the implementation of readline - think of it as a function like printf. Learn how to use it first, then worry about how it's implemented. The standard C library doesn't provide input functions which discard output, so you have to write your own (or use ones provided by your implementation.)



strcmp is defined in string.h. It's used for comparing two strings. In C, strings are nul-terminated arrays of char. You can read about them in a C reference like "The C Programming Language (Second Edition)" by Kernighan and Ritchie.



Finally, buf needs to be at least 4 characters long, so that it can store the sequence { 'X', 'Y', 'Z', '\0' }.



edit:



@Yalamati: Modifying a string literal has undefined behaviour. I hear that running your code under a standard compliant compiler can cause demons to fly out of your nose. Use it with caution.



@Mohit: C has no "Char" type.



edit#2:



@Casper: Learn basic literacy skills then get a copy of the C11 standard draft: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf



Look at section 7 (Library). Is "getch" defined anywhere in there? No. I said that C didn't provide a "getch" function and it doesn't. Compilers are free to provide a function called getch, but they needn't (and most don't) because it isn't a function defined in the C standard.



Also, just to clarify, I said that _C++_ doesn't provide a header file called "stdio.h" - it has an alternative called "cstdio". You can verify this by getting a copy of the latest C++ standard draft. C, being a completely different language to C++, provides a header file called "stdio.h".
AgilePro
2012-05-12 10:37:11 UTC
Check

1) Is XYZ is defined anywhere? It should be defined as a char array

2) You are comparing the String not in a proper way.



Use the strcmp() function instead.



if(strcmp(bname,XYZ)==0)

{

printf("The code is %d", code);

}
Laks
2012-05-12 08:34:16 UTC
we do not directly give the book name here



before, if(bname==XYZ)



declare it...



#include

main()

char bname;

printf('Enter book name: ");

gets("bname");

if(bname==XYZ);

printf("code: 123);

getch();

}
anonymous
2012-05-12 08:39:32 UTC
int n;

Char bname[10];

char s1[10]='name of book';

printf("\nenter book name: ");

gets(bname);

n=(s1,bname);

if(n==0)

{

printf("book cod :%d",variable);

puts(bname);

}

getch();
Kunal
2012-05-12 08:37:19 UTC
For strings you need to use strcmp or strncmp

Check their declarations here

http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

http://www.cplusplus.com/reference/clibrary/cstring/strncmp/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...