Dec 2, 2016 at 9:18pm UTC
This is for an assignment. How do I split up the first array (names) into the two other strings (last, first) without initializing the latter two?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
#include <string>
using namespace std;
string names[6] = {
"fuzz butt" ,
"jerkus mclurkus" ,
"acidface boy" ,
"banana hands" ,
"crawl space" ,
"praying ghost"
};
static const size_t npos = -1;
string last (size_t pos = ' ' , size_t len = npos);
string first (size_t pos = 0, size_t len = ' ' );
int main(int argc, char ** argv) {
for (int i=0;i<6;i++) {
cout << names[i] << "\n" ;
}
for (int a=0;a<6;a++) {
cout << last << ", " << first << "\n" ;
}
}
Last edited on Dec 2, 2016 at 9:18pm UTC
Dec 3, 2016 at 12:04am UTC
First, what is the purpose of lines 17-19?
The names is an array of 6 objects, and each of them seem to contain two "words" separated by whitespace.
If 'last' is one object, what should it contain? One word? Concatenation of several words?
Dec 3, 2016 at 3:01am UTC
Splits up the names in the array in half.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <iostream>
#include <string>
int main()
{
std::string names[6] =
{
"fuzz butt" ,
"jerkus mclurkus" ,
"acidface boy" ,
"banana hands" ,
"crawl space" ,
"praying ghost"
};
constexpr unsigned short MID =
(sizeof (names) / sizeof (std::string)) / 2;
std::string first[MID], last[MID];
for (std::string::size_type i = 0; i != MID; ++i)
first[i] = names[i];
for (std::string::size_type i = MID; i != MID * 2; ++i)
last [i - MID] = names[i];
for (const std::string &s : names)
std::cout << s << '\n' ;
std::cout << "\n=======================\n\n" ;
for (const std::string &s : first)
std::cout << s << '\n' ;
std::cout << '\n' ;
for (const std::string &s : last)
std::cout << s << '\n' ;
return 0;
}
fuzz butt
jerkus mclurkus
acidface boy
banana hands
crawl space
praying ghost
=======================
fuzz butt
jerkus mclurkus
acidface boy
banana hands
crawl space
praying ghost
Last edited on Dec 3, 2016 at 3:08am UTC
Dec 3, 2016 at 3:08am UTC
I truly don't know what those lines are meant to do. It was something I found on the web that sounded somewhat similar to what my aims are. I'm trying to get "first" to contain the first names and "last" to contain the last names without initializing. My professor hasn't really taught us what to do and has just told us to google it. I've stared at this code and dozens of forum pages and tutorials for about 12 hours now and I'm no closer to understanding what to do. I know it shouldn't be this hard but man...
Last edited on Dec 3, 2016 at 3:08am UTC
Dec 3, 2016 at 3:47am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <iostream>
#include <string>
int main()
{
std::string names[6] =
{
"fuzz butt" ,
"jerkus mclurkus" ,
"acidface boy" ,
"banana hands" ,
"crawl space" ,
"praying ghost"
};
for (const std::string &s : names)
std::cout << s << '\n' ;
std::cout << '\n' ;
for (const std::string &s : names)
std::cout << s.substr(0, s.find(' ' )) << ' ' << std::flush;
std::cout << "\n\n" ;
for (const std::string &s: names)
std::cout << s.substr(s.find(' ' ) + 1) << ' ' << std::flush;
std::cout << '\n' ;
return 0;
}
fuzz butt
jerkus mclurkus
acidface boy
banana hands
crawl space
praying ghost
fuzz jerkus acidface banana crawl praying
butt mclurkus boy hands space ghost
Last edited on Dec 3, 2016 at 3:49am UTC
Dec 3, 2016 at 3:48am UTC
Is your program supposed to do something like this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <iostream>
#include <string>
int main(int argc, char ** argv)
{
std::string names[6] =
{
"fuzz butt" ,
"jerkus mclurkus" ,
"acidface boy" ,
"banana hands" ,
"crawl space" ,
"praying ghost"
};
size_t array_size = sizeof (names)/sizeof (std::string);
std::string first;
std::string last;
size_t npos = 0;
for (size_t i = 0; i < array_size; i++)
{
npos = names[i].find(' ' );
first = names[i].substr(0, npos);
last = names[i].substr(npos +1 );
std::cout << last << ", " << first << '\n' ;
}
}
butt, fuzz
mclurkus, jerkus
boy, acidface
hands, banana
space, crawl
ghost, praying
Program ended with exit code: 0
Last edited on Dec 3, 2016 at 8:53am UTC
Dec 3, 2016 at 3:49am UTC
Wow! 3 solutions in 3 minutes, the OP is so spoilt!!!
Dec 3, 2016 at 3:56am UTC
Must be a slow day for helpers. Excellent choice in using stringstream.
Dec 3, 2016 at 4:14am UTC
@gunnerfunner
4 solutions, actually.
Last edited on Dec 3, 2016 at 6:50am UTC
Dec 3, 2016 at 4:26am UTC
Thank you all for the help, truly appreciated. I'm going to study these and learn where I can. Have a great day/night, people!
Kemort definitely came the closest to what I was looking for. Thank you!
Last edited on Dec 3, 2016 at 4:30am UTC
Dec 3, 2016 at 6:52am UTC
crawlspace wrote:Kemort definitely came the closest to what I was looking for.
Closest to what you were looking for or what you understand the most? What you understand may or may not be the best way.
Last edited on Dec 3, 2016 at 6:52am UTC
Dec 3, 2016 at 7:20am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <string>
int main(int argc, char ** argv)
{
std::string names[] =
{
"fuzz butt" ,
"jerkus mclurkus" ,
"acidface boy" ,
"banana hands" ,
"crawl space" ,
"praying ghost"
};
for ( std::string s : names){ std::cout << s.substr(s.find(' ' ) +1 ) << ", " << s.substr(0, s.find(' ' )) << '\n' ;}
}
Last edited on Dec 3, 2016 at 8:52am UTC
Dec 3, 2016 at 8:04am UTC
Well done. Fixed.
E&OE
Last edited on Dec 3, 2016 at 8:06am UTC
Dec 3, 2016 at 2:16pm UTC
If any of you guys would have looked at my code, you would of seen that I was the only one added 1 to s.find(' ') , haha.
Last edited on Dec 3, 2016 at 2:19pm UTC
Dec 3, 2016 at 2:33pm UTC
Well done. jlborges did too by the look of it, but nevertheless.
Dec 3, 2016 at 2:35pm UTC
I did not notice that in JLBorges code just like you, haha.