www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/09/29/16:45:12

From: Thomas8675309 AT yahoo DOT com (Tom)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: help with strings
Date: 29 Sep 2002 13:36:55 -0700
Organization: http://groups.google.com/
Lines: 87
Message-ID: <7b68d58f.0209291236.42a8c9af@posting.google.com>
References: <F16Y0emNoQgUtc2Ar7Y0000658b AT hotmail DOT com>
NNTP-Posting-Host: 63.72.148.162
X-Trace: posting.google.com 1033331815 7782 127.0.0.1 (29 Sep 2002 20:36:55 GMT)
X-Complaints-To: groups-abuse AT google DOT com
NNTP-Posting-Date: 29 Sep 2002 20:36:55 GMT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"Timothy M Boronczyk" <bluephoenixbeta AT hotmail DOT com> wrote in message news:<F16Y0emNoQgUtc2Ar7Y0000658b AT hotmail DOT com>...

[Snip]

> Now, my project calls for me to implement strings.  
> But I'm left scratching my head between the tutorials and fidling around; 
> what I think should work either errors out during compilation, or gets stuck 
> in an endless loop.
> 
> I've tried:
> 
> char string_var[20];
> cin.getline(string_var, 20, '\n');
> 
> I've even tried:
> 
> char string_var[20];
> int count=0;
> 
> do
> {
>   cin>>char[count];
>   count++;
> } while ((count<20)!!(string_var[count-1]=='\n'))
> 
> As always, any help would be greatly appreciated.
> 
> -Tim
> 

Tim, you haven't given enough information to explain what it is you're
trying to accomplish, nor have you posted compilable code (because
you've left stuff out), both of which make it harder to help you. 
But, you should be aware that the standard way to work with strings in
standard C++ is using the string class from the standard library.  For
example:

#include <iostream>
#include <string>

int main()
{
    using std::cin;
    using std::cout;
    using std::string;

    cout << "Enter first string: ";
    string s1;
    std::getline(cin, s1);  // Read a line into s1,
                            // stopping at '\n'
    cout << "Enter second string: ";
    string s2;
    cin >> s2;              // Read a string into s2, 
                            // stopping at first white space
    string s3 = s1 + s2;
    cout << "Together: " << s3;
    return 0;
}

The string class is much safer and much more versatile than using
character arrays (AKA "C-strings"), as you appear to do.  There are
exceptions, of course, but when in doubt, use a std::string.

Also as Sinan points out, your question really has nothing to do with
DJGPP specifically, but rather with C++ in general.  You'll get
faster/more responses posting those kinds of questions to
comp.lang.c++.moderated or to comp.lang.c++.  But before posting to
either, or more C++ questions here, you should take a look at the
comp.lang.c++ FAQs, which you can find at
http://www.parashift.com/c++-faq-lite .  You'll find that if answers a
lot of your basic questions.

Finally, you mentioned www.cprogramming.com and Bruce Eckel's Thinking
in C++.  You should stay away from the former - it's teaching an
archaic form of C++ that is no longer standard, and in the process is
teaching you bad habits, as evidenced by the message you posted.  The
latter is excellent, but it assumes a prior knowledge of C.  If you're
willing to spend a few bucks, I recommend "Accelerated C++," by Andrew
Koenig and Barbara Moo.  It's an excellent, and very fast-paced,
tutorial that will teach you proper habits from the start.  The
authors are colleagues of Bjarne Stroustrup, the creator of C++, and
have worked on developing and teaching the language since almost its
beginning.  It lists for about 40 bucks, but you can find it cheaper.

Best regards,

Tom

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019