![]() |
Strings inside arrays
Hi,
First, Sorry if the title was bad, I really didn't know how to describe this. So, What I need to do is something like this: char somevariable[256]; // The tricky part. I don't know how to define it. somevariable[1] = "Hello"; somevariable[2] = "Goodbye"; And to print them using: printf(somevariable[1]); and printf(somevariable[2]); Hope I was describing this enough. Thanks, LinuxDuud. |
Re: Strings inside arrays
"LinuxDuud" <ng1200@gmail.com> wrote in message news:1168970189.421941.107920@51g2000cwl.googlegro ups.com... > Hi, > First, Sorry if the title was bad, I really didn't know how to describe > this. > So, > What I need to do is something like this: > char somevariable[256]; // The tricky part. I don't know how to define > it. > somevariable[1] = "Hello"; > somevariable[2] = "Goodbye"; > And to print them using: > printf(somevariable[1]); > and > printf(somevariable[2]); > > Hope I was describing this enough. > Thanks, LinuxDuud. #include <iostream> #include <string> #include <vector> using std::cout; using std::string; using std::vector; int main() { vector<string> text; text.push_back("Hello"); text.push_back("The C++ standard library has many easy to use,"); text.push_back("robust and versatile types and algorithms. Use them."); text.push_back("Goodbye"); for(vector<string>::size_type i = 0; i < text.size(); ++i) cout << text[i] << '\n'; return 0; } -Mike |
Re: Strings inside arrays
On 16 Jan 2007 09:56:29 -0800, "LinuxDuud" <ng1200@gmail.com> wrote in
comp.lang.c++: > Hi, > First, Sorry if the title was bad, I really didn't know how to describe > this. > So, > What I need to do is something like this: > char somevariable[256]; // The tricky part. I don't know how to define > it. > somevariable[1] = "Hello"; > somevariable[2] = "Goodbye"; > And to print them using: > printf(somevariable[1]); > and > printf(somevariable[2]); > > Hope I was describing this enough. > Thanks, LinuxDuud. Somebody else already showed you a way to use C++ vectors and streams to do what you want, completely ignoring the fact that you might have a good reason to use C style strings and the printf() function. And you should prefer vectors unless there is some reason that you cannot. But to answer your original question: const char *somevariable [256] = { "Hello", "Goodbye", /* up to 254 more string literals */ }; -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://c-faq.com/ comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html |
Re: Strings inside arrays
Mike Wahler wrote: > "LinuxDuud" <ng1200@gmail.com> wrote in message > news:1168970189.421941.107920@51g2000cwl.googlegro ups.com... > > Hi, > > First, Sorry if the title was bad, I really didn't know how to describe > > this. > > So, > > What I need to do is something like this: > > char somevariable[256]; // The tricky part. I don't know how to define > > it. > > somevariable[1] = "Hello"; > > somevariable[2] = "Goodbye"; > > And to print them using: > > printf(somevariable[1]); > > and > > printf(somevariable[2]); > > > > Hope I was describing this enough. > > Thanks, LinuxDuud. > > #include <iostream> > #include <string> > #include <vector> > > using std::cout; > using std::string; > using std::vector; > > int main() > { > vector<string> text; > > text.push_back("Hello"); > text.push_back("The C++ standard library has many easy to use,"); > text.push_back("robust and versatile types and algorithms. Use them."); > text.push_back("Goodbye"); > > for(vector<string>::size_type i = 0; i < text.size(); ++i) > cout << text[i] << '\n'; With such a message I would think your loop might look more like this: std::copy(text.begin(), text.end(), std::ostream_iterator<std::string>(cout, "\n")); > > return 0; > } > > -Mike |
Re: Strings inside arrays
"Noah Roberts" <roberts.noah@gmail.com> wrote in message news:1168972950.084890.242350@11g2000cwr.googlegro ups.com... > > Mike Wahler wrote: >> >> for(vector<string>::size_type i = 0; i < text.size(); ++i) >> cout << text[i] << '\n'; > > With such a message I would think your loop might look more like this: > > std::copy(text.begin(), text.end(), > std::ostream_iterator<std::string>(cout, "\n")); :-) Actually, I indeed almost did exactly that. But I thought better of it, since OP seems to obviously be a C++ beginner. I Didn't want to overload (pun intended) him with too much info right away. -Mike |
Re: Strings inside arrays
Jack Klein wrote: > Somebody else already showed you a way to use C++ vectors and streams > to do what you want, completely ignoring the fact that you might have > a good reason to use C style strings and the printf() function. And > you should prefer vectors unless there is some reason that you cannot. I think so, that printf() can be used in some cases if you well know the function, but you can get wrong output, if you will mix "std::stdout" and "std::cout" streams. To avoid backside effects use "printf" and "<<" into separate streams, for example, do printf into stderror or file and << into cout. |
Re: Strings inside arrays
On Tue, 16 Jan 2007 21:19:35 -0800, Grizlyk wrote:
> Jack Klein wrote: > >> Somebody else already showed you a way to use C++ vectors and streams >> to do what you want, completely ignoring the fact that you might have >> a good reason to use C style strings and the printf() function. And >> you should prefer vectors unless there is some reason that you cannot. > > I think so, that printf() can be used in some cases if you well know > the function, but you can get wrong output, if you will mix > "std::stdout" and "std::cout" streams. To avoid backside effects ....please adjust your diet ;) Seriously though, not quite sure what you intended there. > use "printf" and "<<" into separate streams, for example, do printf into > stderror or file and << into cout. Is that necessary? I think a call to std::ios_base::sync_with_stdio(true) should synchronise the standard C I/O facilities (stdout, etc.) with their standard C++ equivalents (std::cout, etc.). (Indeed it used to be [still is?] recommended that if you are using one or the other of C or C++ I/O facilities then it might be a good idea to call sync_with_stdio(false), since synching I/O modes might entail a substantial speed/size overhead). -- Lionel B |
Re: Strings inside arrays
Lionel B wrote:
> Is that necessary? I think a call to std::ios_base::sync_with_stdio(true) > should synchronise the standard C I/O facilities (stdout, etc.) with their > standard C++ equivalents (std::cout, etc.). They are sync'd by default; you would only call that function if you wanted to unsync them. |
Re: Strings inside arrays
On Wed, 17 Jan 2007 14:12:29 -0800, Old Wolf wrote:
> Lionel B wrote: >> Is that necessary? I think a call to std::ios_base::sync_with_stdio(true) >> should synchronise the standard C I/O facilities (stdout, etc.) with their >> standard C++ equivalents (std::cout, etc.). Yep, sorry, that's the wrong way round... > They are sync'd by default; you would only call that function if you > wanted to unsync them. For that reason it was often recommended that you explicitly *un*-sync them with std::ios_base::sync_with_stdio(false) for better I/O performance if you were not intending to mix C and C++ style I/O in the same program. -- Lionel B |
| All times are GMT. The time now is 03:36 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.