🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #309

Re: char array compare

#309
From: 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;
>    }
>   }
> }
>
>
Technical Details
Message-ID: <gzg58.21043$l93.3779942@newsb.telia.net>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeeds.belnet.be!news.belnet.be!opentransit.net!proxad.net!proxad.net!freenix!newsfeed.rt.ru!news-stob.telia.net!news-sto.telia.net!194.22.194.4.MISMATCH!masternews.telia.net.!newsb.telia.net.POSTED!not-for-mail
References: <a2s339$ngo3@imsp212.netvigator.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f8d38,70272e41ccb8f8b6
X-Google-Attributes: gidf8d38,public
X-Google-Thread: 109fba,70272e41ccb8f8b6
X-Google-Attributes: gid109fba,public
X-Google-Thread: 114592,70272e41ccb8f8b6
X-Google-Attributes: gid114592,public
X-Google-ArrivalTime: 2002-01-28 10:18:36 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeeds.belnet.be!news.belnet.be!opentransit.net!proxad.net!proxad.net!freenix!newsfeed.rt.ru!news-stob.telia.net!news-sto.telia.net!194.22.194.4.MISMATCH!masternews.telia.net.!newsb.telia.net.POSTED!not-for-mail
From: "Jonas" <v123@telia.com>
Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.language,comp.lang.c++
References: <a2s339$ngo3@imsp212.netvigator.com>
Subject: Re: char array compare
Lines: 101
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
Message-ID: <gzg58.21043$l93.3779942@newsb.telia.net>
Date: Mon, 28 Jan 2002 18:15:40 GMT
NNTP-Posting-Host: 195.67.229.136
X-Complaints-To: abuse@telia.com
X-Trace: newsb.telia.net 1012241740 195.67.229.136 (Mon, 28 Jan 2002 19:15:40 CET)
NNTP-Posting-Date: Mon, 28 Jan 2002 19:15:40 CET
Organization: Telia Internet