473,386 Members | 1,708 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,386 software developers and data experts.

comparison of char array with a structure char array

Here is the code of the program.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<iostream.h>
  3. #include<stdlib.h>
  4. main()
  5. {
  6. struct game
  7. {
  8. char c;
  9. };
  10. game a[2];
  11. char e;
  12. int i;
  13. for (i=0;i<2;i++)
  14. {
  15. cout<<"Enter Name: ";
  16. cin>>a[i].c;
  17. }
  18. cout<<"Enter First Word";
  19. cin>>e;
  20. int j;
  21. for (j=0;j<2;j++)
  22. {
  23. if (e==a[j].c)
  24. {
  25. cout<<"The Comparison Was Made Successfuly"<<a[j].c;
  26. }
  27. }
  28. system("pause");
  29. }
  30.  
When i run it it ask me to enter name if i enter only a single character like a,or b
it runs well but when i enter any stream like human it does not run plz correct this code its very needy............
Dec 23 '08 #1
4 4041
osfreak
22
The above program can compare only single character values.

Pls explain your need fully..

Are you trying to compare character values

or to compare two character strings ???
Dec 23 '08 #2
yes i want to compare two strings such "human"=="human"Plz explain....
Dec 23 '08 #3
newb16
687 512MB
You tried 'compare c strings' in google, didn't you?
Dec 23 '08 #4
Banfa
9,065 Expert Mod 8TB
#include<iostream.h>

This header is deprecated and is the pre-ANSI standard header. It is actually a bad idea to use this header because it is possible different compilers have implemented the contents in a different way which are not compatible either with each other or the standard iostream. Where as iostream is defined by the standard, so you can pretty much rely on everyone having implemented it in a standard compliant manor. Read this <iostream.h> or <iostream>? and use
#include<iostream>
#include<stdlib.h>

Don't use stdio.h in a C++ program. There is almost nothing in that file that you would need that is not provided by the C++ Standard Library (possibly the remove function and the size_t type) and you certainly haven't used anything from it in your program that I see.

However if you really really need to use this header (and that means you have a clearly stated and defensible reason) then you should be using
#include<cstdlib>

Finally you are only getting 1 character because you structures only contain a single character in them. If you want a string in C++ then you should be using the type string and the function getline all declared in the header string.
Dec 23 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
5
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin interface. While portability (from a C perspective) is...
6
by: dddddddd2444444 | last post by:
Hi,please help... It works fine when I define a 2-D array like char code. But it won't work when I try to define the array dynamically using a function. It just crashes. Does anyone know why?...
26
by: junky_fellow | last post by:
Consider the following piece of code: char *str = "Hello"; if (str = "Hello") printf("\nstring matches\n"); str is pointer to char and "Hello" is a string literal whose type is "array of...
4
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some...
2
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C#...
5
by: eagle_jyjh | last post by:
For example: the msg = temp_buf; is alwawys ok? //test_msg.cpp struct msg_head { char a01;
20
by: chutsu | last post by:
I'm trying to compare between pointer and integer in an "IF" statement how do I make this work? if(patient.id != NULL){ } Thanks Chris
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.