Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

widgetman

macrumors member
Original poster
Oct 7, 2004
39
0
hi im a newb at cocoa, and i want to use it to make a terminal application. how do i go about reading input from the user and outputting data.

thanks
 

widgetman

macrumors member
Original poster
Oct 7, 2004
39
0
if there is someone out there who knows anything about c++, is there a better version of the string class that can accept multiple words with cin?

thanks again!
 

cerfsud

macrumors newbie
Jul 29, 2004
15
0
use getline(istream i, string s). it'll get input from the first arg and put it into the second arg and it stops reading once it hits a newline (return).


example:

#include <iostream>

int main() {
string name;
cout << "your full name? ";
getline(cin, name);
cout << endl << "your name is: " << name;
return 1;
}



this is all from my head - i may be wrong on the args or how to call it, so just google for it if it doesn't work.
 

widgetman

macrumors member
Original poster
Oct 7, 2004
39
0
thats cool, but how can i check to see what the words entered are? for example, if the person enters 3 words, how can i target the first word or the second word by itself?

say i entered my name as "Joe Shmo" or anything like that...how can i see if word 1 is "Joe" but still be able to target the entire thing as a string, like see if they entered "Joe Shmo".

thanks!!
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
splitting a string into words

Code:
#include <string>
#include <vector>


/// declaration
std::vector<std::string> splitstr(
    const std::string &s, const std::string &sep = "\t\n\v\r ",
    int maxsplit = 0);


//////////////////////////////////////////////////////////////////////////////
/// \brief Non-destructive strtok() replacement
///
/// Splits a string into tokens and returns them as a vector of
/// strings.
std::vector<std::string>
splitstr(
         /// string to split
         const std::string &s,
         /// separators (default is ASCII whitespace)
         const std::string &sep,
         /// make no more than this many splits (0 = unlimited).  For
         /// example, if \a maxsplit == 1 the string will be split into no more
         /// than two tokens.
         int maxsplit
         )
{
    int
        splits = 0;

    std::string::size_type
        p,
        q;
    std::vector<std::string>
        rv;
    for (p = 0; ; splits++) {
        p = s.find_first_not_of(sep, p);
        if (p == std::string::npos)
            break;
        if (maxsplit && splits >= maxsplit) {
            rv.push_back(s.substr(p));
            break;
        }
        q = s.find_first_of(sep, p);
        rv.push_back(s.substr(p, q - p));
        p = q;
    }
    return rv;
}
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
And here is a little program showing how to use the function on the above posting.

Code:
#include <iostream>

///
///  paste in the code from the previous article here
///

int
main()
{
    std::string
        linein;
    std::vector<std::string>
        wordlist;
    std::vector<std::string>::size_type
        wordcount,
        i;

    std::cout << "? ";
    std::getline(std::cin, linein);

    wordlist = splitstr(linein);
    wordcount = wordlist.size();
    for (i = 0; i < wordcount; ++i) {
        std::cout << "wordlist[" << i << "]: " << wordlist[i] << "\n";
    }
    std::cout << "linein: \"" << linein << "\"\n";
}

And now, the big question: what has any of this to do with Cocoa? :confused:
 

widgetman

macrumors member
Original poster
Oct 7, 2004
39
0
iMeowbot said:
And now, the big question: what has any of this to do with Cocoa? :confused:

well...i really doesnt, i just was going to make this in cocoa, but then decided that c++ was easier and i already have some c++ knowledge using the iostream and std methods.

thanks!!!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.