Strings inside arrays

L

LinuxDuud

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.
 
M

Mike Wahler

LinuxDuud said:
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 << '\n';

return 0;
}

-Mike
 
J

Jack Klein

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 */
};
 
N

Noah Roberts

Mike said:
LinuxDuud said:
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 << '\n';


With such a message I would think your loop might look more like this:

std::copy(text.begin(), text.end(),
 
M

Mike Wahler

Noah Roberts said:
Mike Wahler wrote:
for(vector<string>::size_type i = 0; i < text.size(); ++i)
cout << text << '\n';


With such a message I would think your loop might look more like this:

std::copy(text.begin(), text.end(),
std::eek:stream_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
 
G

Grizlyk

Jack said:
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.
 
L

Lionel B

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).
 
O

Old Wolf

Lionel said:
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.
 
L

Lionel B

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top