🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.lang.learn.c-c++
5 messages
5 total messages Started by EL Fri, 25 Jan 2002 17:10
char array compare
#301
Author: EL
Date: Fri, 25 Jan 2002 17:10
46 lines
1754 bytes
Help!

How can i compare a char array from defined structure with input char?
I can't get the expected result from the following coding. Pls help!


struct mystr
{
char id[7], name[21];
};

mystr s[50];

void main()
{
  int i, j ;
  char vid[7];

  for (i=0; i<2 ; i++)
  {
   cout << "Stud ID : ";
   cin >> s[i].id;endl;
   cout << "Name    : ";
   cin >> s[i].name;endl;
  }

  for (i=0; i<2 ; i++)
  {
   cout << "ID: " << s[i].id << "\t" << "Name: " << s[i].name << endl;
  }

  cout << "\nEnter search id: ";
  cin >> vid;endl;

  for (i=0; i<2 ; i++)
  {
   if (vid == s[i].id)
   {
   cout << "\nFound id: " << s[i].id;
   }
   else
   {
   cout << "\nNot found you id " << vid;
   }
  }
}
Re: char array compare
#302
Author: Victor Bazarov
Date: Fri, 25 Jan 2002 17:32
10 lines
1545 bytes
"EL" <lameddie@yahoo.com> wrote...
> How can i compare a char array from defined structure with input char?
> I can't get the expected result from the following coding. Pls help!

Either use strcmp() (for char[]) or switch to using std::string
and use '=='.

Victor
--
Please remove capital A's from my address when replying by mail
Re: char array compare
#303
Author: Bart van Ingen Schenau
Date: Fri, 25 Jan 2002 17:55
46 lines
2083 bytes
On Sat, 26 Jan 2002 01:10:24 +0800, "EL" <lameddie@yahoo.com> wrote:

>Help!
>
>How can i compare a char array from defined structure with input char?
>I can't get the expected result from the following coding. Pls help!
>
>
>struct mystr
>{
>char id[7], name[21];
>};
>
>mystr s[50];
>
>void main()

According to the definitions of the C and C++ languages should main
return an int, so
  int main()
>{
>  int i, j ;
>  char vid[7];
>
<snip>
>   if (vid == s[i].id)

There you are comparing the *addresses* of vid and s[i].id. To compare
the contents of the char arrays, use strcmp():
  if (strcmp(vid, s[i].id) == 0) /* they are equal */

>   {
>   cout << "\nFound id: " << s[i].id;
>   }
>   else
>   {
>   cout << "\nNot found you id " << vid;
>   }
>  }
>}
>
Bart v Ingen Schenau
--
Remove NOSPAM to mail me directly
FAQ for clc: http://www.eskimo.com/~scs/C-faq/top.html
FAQ for acllc-c++: http://snurse-l.org/acllc-c++/faq
Re: char array compare
#304
Author: Randy Charles Morin
Date: Sat, 26 Jan 2002 02:35
103 lines
2885 bytes
Use STL string class
--
Randy Charles Morin
http://www.kbcafe.com

Feel free to contact me by private email or messenger
morin_randy@hotmail.com
randy@kbcafe.com

#include <iostream>

struct mystr
{
char id[7], name[21];
};

mystr s[50];

void main()
{
  int i, j ;
  char vid[7];

  for (i=0; i<2 ; i++)
  {
   std::cout << "Stud ID : ";
   std::cin >> s[i].id;
   std::cout << "Name    : ";
   std::cin >> s[i].name;
  }

  for (i=0; i<2 ; i++)
  {
   std::cout << "ID: " << s[i].id << "\t" << "Name: " << s[i].name << std::endl;
  }

  std::cout << "\nEnter search id: ";
  std::cin >> vid;

  for (i=0; i<2 ; i++)
  {
   if (std::string(vid) == std::string(s[i].id))
   {
   std::cout << "\nFound id: " << s[i].id;
   }
   else
   {
   std::cout << "\nNot found you id " << vid;
   }
  }
}



"EL" <lameddie@yahoo.com> wrote in message news:a2s339$ngo3@imsp212.netvigator.com...
> Help!
>
> How can i compare a char array from defined structure with input char?
> I can't get the expected result from the following coding. Pls help!
>
>
> struct mystr
> {
> char id[7], name[21];
> };
>
> mystr s[50];
>
> void main()
> {
>   int i, j ;
>   char vid[7];
>
>   for (i=0; i<2 ; i++)
>   {
>    cout << "Stud ID : ";
>    cin >> s[i].id;endl;
>    cout << "Name    : ";
>    cin >> s[i].name;endl;
>   }
>
>   for (i=0; i<2 ; i++)
>   {
>    cout << "ID: " << s[i].id << "\t" << "Name: " << s[i].name << endl;
>   }
>
>   cout << "\nEnter search id: ";
>   cin >> vid;endl;
>
>   for (i=0; i<2 ; i++)
>   {
>    if (vid == s[i].id)
>    {
>    cout << "\nFound id: " << s[i].id;
>    }
>    else
>    {
>    cout << "\nNot found you id " << vid;
>    }
>   }
> }
>
>
Re: char array compare
#309
Author: Jonas
Date: Mon, 28 Jan 2002 18:15
99 lines
3394 bytes
This code solves the problem (in c++), but probably not in an ideal way.
However there is many different ways to read in the chars:

#include <iostream>
#include <iomanip>

bool StrCmp (char* first, char* second)
{
 for (int i = 0 ; first[i] == second[i] ; i++)
  if (first[i] == 0) return true ;
 return false ;
}

int main ()
{
 char vid[ ] = "MYNAM" ;
 struct mystruct
 {
  char id[7] ;
  /*........*/
 } mys ;

//  7 - 1 chars are transferred, termination
// character is appended onto char array

std::cin.getline (mys.id,7) ;

// Skip leading witespaces, set width to 7, end at white space or 7-1
// characters, append termination charachter

std::cin >> std::ws >> std::setw(7) >> mys.id ;

 if (StrCmp (vid, mys.id))
  std::cout << "Strings equal: "
   << vid << "    " << mys.id
   << std::endl ;
 else
  std::cout << "Strings Do Not Match: "
   << vid << "    " << mys.id
   << std::endl ;
 return 0 ;
}

--
----------------------------------------------------------------------------
----------
Jonas, email: v123@telia.com
----------------------------------------------------------------------------
----------
"EL" <lameddie@yahoo.com> wrote in message
news:a2s339$ngo3@imsp212.netvigator.com...
> Help!
>
> How can i compare a char array from defined structure with input char?
> I can't get the expected result from the following coding. Pls help!
>
>
> struct mystr
> {
> char id[7], name[21];
> };
>
> mystr s[50];
>
> void main()
> {
>   int i, j ;
>   char vid[7];
>
>   for (i=0; i<2 ; i++)
>   {
>    cout << "Stud ID : ";
>    cin >> s[i].id;endl;
>    cout << "Name    : ";
>    cin >> s[i].name;endl;
>   }
>
>   for (i=0; i<2 ; i++)
>   {
>    cout << "ID: " << s[i].id << "\t" << "Name: " << s[i].name << endl;
>   }
>
>   cout << "\nEnter search id: ";
>   cin >> vid;endl;
>
>   for (i=0; i<2 ; i++)
>   {
>    if (vid == s[i].id)
>    {
>    cout << "\nFound id: " << s[i].id;
>    }
>    else
>    {
>    cout << "\nNot found you id " << vid;
>    }
>   }
> }
>
>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads