PDA

View Full Version : cocoa io basics




widgetman
Oct 11, 2004, 05:59 PM
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
Oct 11, 2004, 06:03 PM
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
Oct 11, 2004, 07:08 PM
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.

cerfsud
Oct 11, 2004, 07:10 PM
oh and using namespace std; should be up there by that include too. sorry.

widgetman
Oct 11, 2004, 09:53 PM
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
Oct 12, 2004, 02:25 AM
#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
Oct 12, 2004, 03:05 AM
And here is a little program showing how to use the function on the above posting.


#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
Oct 12, 2004, 09:03 AM
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!!!