lfnetwork.com mark read register faq members calendar

Thread: C++ Syntax
Thread Tools Display Modes
Post a new thread. Add a reply to this thread. Indicate all threads in this forum as read. Subscribe to this forum. RSS feed: this forum RSS feed: all forums
Old 08-31-2005, 02:03 PM   #1
Skinkie
Definately, Maybe
 
Skinkie's Avatar
 
Join Date: Dec 2001
Location: Eh, Wherever
Posts: 2,445
10 year veteran! Forum Veteran 
C++ Syntax

Ok I know there's a few programmers in the bunch here. This is how its going, I need to create a class to act as an array based unordered link-list. I've got no problem with the logic stuff that's no problem. My problem is they had us doing Java for a semester, then there was summer break, so my C++ syntax skills have become a bit rusty. Especially in the area of pointers and arrays. And especially since this program is suppose to use dynamic arrays which I've never used before. So I have the general

string *pname = NULL;
int *pnum = NULL;

in the private data of the class and

pname = new string[num];
pnum = new int[num];

in the constructor. so that should create the array correctly(if I'm wrong correct me).

Now my question is how can I traverse this array. can I say things like

pnum++; //Move to next item down list
pnum = pnum[firstinlist]; //move pointer to 'first' item in list
*pnum = 7; //set current spot to 7
pnum = pnum[7]; //put pointer on location 7

are all of those allowed?

Any suggestions for this are appriciated. I've got a general idea of what I can/can't do, but I'd prefer to make sure.


Just because people think what I say don't make sense, doesn't mean I don't like popsicles.
Skinkie is offline   you may: quote & reply,
Old 08-31-2005, 03:43 PM   #2
DrMcCoy
Senior Member
 
DrMcCoy's Avatar
 
Join Date: Sep 2003
Location: Braunschweig, Germany
Posts: 1,984
Current Game: nethack
10 year veteran! Forum Veteran 
Quote:
Originally Posted by Skinkie
pnum++; //Move to next item down list
*pnum = 7; //set current spot to 7
...These two are correct...

However,
pnum = pnum[7];
would set the current spot to the value of the 7th next element...
Likewise,
pnum = pnum[firstinlist];
would set the current one to the value of the firstinlistth (provided firstinlist is of a numerical type) next element...

You'd need an additional pointer just iterating:

int *pnum=NULL, *pnumi=NULL;

pnum = new int[num];
pnumi = pnum; // set pnumi to the first element
*pnumi = 5; // set the current spot to 5
pnumi[0] = 5; // ditto
pnumi[2] = 42; // the the 2nd next element to 42
*(pnumi+2) = 42; // ditto
*(2+pnumi) = 42; // ditto
2[pnumi] = 42; // ditto
pnumi++; // move to the next element;
pnumi[0] = 5; // set the currect spot (now the 2nd from the beginning) to 5
pnumi += 5; // move 5 places "down"
pnumi -= 5; // move 5 places "up"
int pos = pnumi-pnum; // get the current position
pnumi = pnum; // "ground" the iterator, it points to the begining again

Also, while doing this, you should watch that'll won't ever run into "invalid" memory, traveling beyond the boundaries of pnum, you may get a segfault there or overwriting other variables (could also lead to exploitation chances)...




if(!strcmp(nick, "DrMcCoy") && !strcmp(avatar, "Spock")) return E_INSANE;
DrMcCoy is offline   you may: quote & reply,
Old 08-31-2005, 08:01 PM   #3
Skinkie
Definately, Maybe
 
Skinkie's Avatar
 
Join Date: Dec 2001
Location: Eh, Wherever
Posts: 2,445
10 year veteran! Forum Veteran 
Thanks, this should be helpful, after like 8 months off it was sorta like "Declare a variable, what's a variable?" Probably not good since its my major.


Just because people think what I say don't make sense, doesn't mean I don't like popsicles.
Skinkie is offline   you may: quote & reply,
Old 09-13-2005, 09:37 AM   #4
Ray Jones
[armleglegarmhead]
 
Ray Jones's Avatar
 
Join Date: Jun 2003
Location: digital
Posts: 8,252
10 year veteran! LF Jester Helpful! Forum Veteran 
Man, sometimes I am almost sure the inventor of this "syntax" also invented dyslexia.


Ray Jones is offline   you may: quote & reply,
Old 09-13-2005, 10:41 AM   #5
DrMcCoy
Senior Member
 
DrMcCoy's Avatar
 
Join Date: Sep 2003
Location: Braunschweig, Germany
Posts: 1,984
Current Game: nethack
10 year veteran! Forum Veteran 
Quote:
Originally Posted by RayJones
the inventor of this "syntax"
Do you mean these two guy?


(Brian W. Kernighan and Dennis M. Ritchie)




if(!strcmp(nick, "DrMcCoy") && !strcmp(avatar, "Spock")) return E_INSANE;
DrMcCoy is offline   you may: quote & reply,
Old 09-14-2005, 04:22 AM   #6
Ray Jones
[armleglegarmhead]
 
Ray Jones's Avatar
 
Join Date: Jun 2003
Location: digital
Posts: 8,252
10 year veteran! LF Jester Helpful! Forum Veteran 
This is only one guy. One shot is taken from the right and one from the left.
From the rear would be your avatar.


Ray Jones is offline   you may: quote & reply,
Old 09-14-2005, 06:24 AM   #7
DrMcCoy
Senior Member
 
DrMcCoy's Avatar
 
Join Date: Sep 2003
Location: Braunschweig, Germany
Posts: 1,984
Current Game: nethack
10 year veteran! Forum Veteran 
Quote:
Originally Posted by RayJones
This is only one guy. One shot is taken from the right and one from the left.
What did I told you about losing your glasses?

Quote:
Originally Posted by RayJones
From the rear would be your avatar.
At least I have an avatar!




if(!strcmp(nick, "DrMcCoy") && !strcmp(avatar, "Spock")) return E_INSANE;
DrMcCoy is offline   you may: quote & reply,
Old 09-14-2005, 08:36 AM   #8
Ray Jones
[armleglegarmhead]
 
Ray Jones's Avatar
 
Join Date: Jun 2003
Location: digital
Posts: 8,252
10 year veteran! LF Jester Helpful! Forum Veteran 
Quote:
Originally Posted by DrMcCoy
What did I told you about losing your glasses?
Err.. You use it as an excuse for 'accidently' touching your gym teacher's boobs? ^^

Quote:
At least I have an avatar!
Nah, I'm currently not in need of pictures.


Ray Jones is offline   you may: quote & reply,
Old 09-14-2005, 08:57 AM   #9
DrMcCoy
Senior Member
 
DrMcCoy's Avatar
 
Join Date: Sep 2003
Location: Braunschweig, Germany
Posts: 1,984
Current Game: nethack
10 year veteran! Forum Veteran 
Quote:
Originally Posted by RayJones
Err.. You use it as an excuse for 'accidently' touching your gym teacher's boobs? ^^
Exactly! And now go and do like I told you...

Quote:
Originally Posted by RayJones
Nah, I'm currently not in need of pictures.
Ah, you're in the denial phase right now...




if(!strcmp(nick, "DrMcCoy") && !strcmp(avatar, "Spock")) return E_INSANE;
DrMcCoy is offline   you may: quote & reply,
Old 09-14-2005, 09:36 AM   #10
Ray Jones
[armleglegarmhead]
 
Ray Jones's Avatar
 
Join Date: Jun 2003
Location: digital
Posts: 8,252
10 year veteran! LF Jester Helpful! Forum Veteran 
Quote:
Originally Posted by DrMcCoy
Exactly! And now go and do like I told you...
Err.. even if I'd wear glasses, when I compare my gym teacher's boobs to those four I can touch tonight .. no.

Quote:
Ah, you're in the denial phase right now...
Paperlapapp! More the 'no need for pictures' phase.


Ray Jones is offline   you may: quote & reply,
Old 09-14-2005, 09:59 AM   #11
DrMcCoy
Senior Member
 
DrMcCoy's Avatar
 
Join Date: Sep 2003
Location: Braunschweig, Germany
Posts: 1,984
Current Game: nethack
10 year veteran! Forum Veteran 
Quote:
Originally Posted by RayJones
Err.. even if I'd wear glasses, when I compare my gym teacher's boobs to those four I can touch tonight .. no.
...So you've got double-man-boobs? O_o

Quote:
Originally Posted by RayJones
Paperlapapp! More the 'no need for pictures' phase.
*nods* Denial.




if(!strcmp(nick, "DrMcCoy") && !strcmp(avatar, "Spock")) return E_INSANE;
DrMcCoy is offline   you may: quote & reply,
Old 09-16-2005, 03:32 AM   #12
Ray Jones
[armleglegarmhead]
 
Ray Jones's Avatar
 
Join Date: Jun 2003
Location: digital
Posts: 8,252
10 year veteran! LF Jester Helpful! Forum Veteran 
Quote:
Originally Posted by DrMcCoy
...So you've got double-man-boobs? O_o
Err.. McCoy,.. boobs are mostly equal in size..
spoiler:
this: O o - is a nightmare.


spoiler:
The boobs I am talking about usually look like this: (o)(o) ..



spoiler:
and like ()(), ..



spoiler:
and again (o)(o)

*cough* Now you do the math what four of those look like. XD

Quote:
*nods* Denial.
Nah, I totally accept that I have none! Not even one single pixel. I am happy with it. Why waste tons of serverspace? I would even stop posting if it would spam the server.


Ray Jones is offline   you may: quote & reply,
Old 09-16-2005, 05:55 AM   #13
DrMcCoy
Senior Member
 
DrMcCoy's Avatar
 
Join Date: Sep 2003
Location: Braunschweig, Germany
Posts: 1,984
Current Game: nethack
10 year veteran! Forum Veteran 
Quote:
Originally Posted by RayJones
*cough* Now you do the math what four of those look like. XD
...These ones that look like my man-boobs?

Quote:
Originally Posted by RayJones
I am happy with it.
...Suuuure...

Quote:
Originally Posted by RayJones
I would even stop posting if it would spam the server.
...You mean like now?




if(!strcmp(nick, "DrMcCoy") && !strcmp(avatar, "Spock")) return E_INSANE;
DrMcCoy is offline   you may: quote & reply,
Old 09-16-2005, 06:42 AM   #14
Ray Jones
[armleglegarmhead]
 
Ray Jones's Avatar
 
Join Date: Jun 2003
Location: digital
Posts: 8,252
10 year veteran! LF Jester Helpful! Forum Veteran 
Quote:
Originally Posted by DrMcCoy
...These ones that look like my man-boobs?
How much beer have you been drinking already?

Quote:
...Suuuure...
Yes, suuure, like in The Last Crusade.

Quote:
...You mean like now?
Right now?? Nah. I'm not spamming. I'm currently flushing Ubuntu and installing Debian 3.1 stable, and am surprised, they still use the 2.4 Kernel (so I doubt it'll run out of the box on my Vaio, where I need a 2.6.9+ or somethin'..)

OK, I could have known all this before but I wanted to give it a try anyways and.. HEY!! *points* LOOK!! *runs*


Ray Jones is offline   you may: quote & reply,
Old 09-16-2005, 07:30 AM   #15
DrMcCoy
Senior Member
 
DrMcCoy's Avatar
 
Join Date: Sep 2003
Location: Braunschweig, Germany
Posts: 1,984
Current Game: nethack
10 year veteran! Forum Veteran 
Quote:
Originally Posted by RayJones
How much beer have you been drinking already?
Right now? No beer at all...

Quote:
Originally Posted by RayJones
installing Debian 3.1 stable, and am surprised, they still use the 2.4 Kernel (so I doubt it'll run out of the box on my Vaio, where I need a 2.6.9+ or somethin'..)
...You do know that you could have selected a 2.6 kernel with "bf26" at the boot menu, right? Or that you could simply compile your own one?




if(!strcmp(nick, "DrMcCoy") && !strcmp(avatar, "Spock")) return E_INSANE;
DrMcCoy is offline   you may: quote & reply,
Old 09-16-2005, 08:03 AM   #16
Ray Jones
[armleglegarmhead]
 
Ray Jones's Avatar
 
Join Date: Jun 2003
Location: digital
Posts: 8,252
10 year veteran! LF Jester Helpful! Forum Veteran 
"bf26"????????????????????????????????Yesyes, I know. I checked the ultra default default installation on purpose.

Err.. It appears to be "linux26" btw..

[EDIT]

(took the edit and made a new post.. ^^)



Last edited by RayJones; 09-16-2005 at 08:22 AM.
Ray Jones is offline   you may: quote & reply,
Old 09-16-2005, 08:15 AM   #17
DrMcCoy
Senior Member
 
DrMcCoy's Avatar
 
Join Date: Sep 2003
Location: Braunschweig, Germany
Posts: 1,984
Current Game: nethack
10 year veteran! Forum Veteran 
Quote:
Originally Posted by RayJones
Err.. It appears to be "linux26" btw..
Oh... I thought it was "bf26"... *shrug* Anyway, I know Sarge has a 2.4.x and a 2.6.x kernel, just as Woody had 2.2.x and 2.4.x...




if(!strcmp(nick, "DrMcCoy") && !strcmp(avatar, "Spock")) return E_INSANE;
DrMcCoy is offline   you may: quote & reply,
Old 09-16-2005, 08:20 AM   #18
Ray Jones
[armleglegarmhead]
 
Ray Jones's Avatar
 
Join Date: Jun 2003
Location: digital
Posts: 8,252
10 year veteran! LF Jester Helpful! Forum Veteran 
WHOA!, I joined the fun and did expert-mode install, there are like billions of kernels available.. ^^; I will however, compile my own anyways.. especially because I need one above 2.6.8. But later. But this way I do not need to bug around with the modutils..


Ray Jones is offline   you may: quote & reply,
Old 10-16-2005, 02:38 PM   #19
Skinkie
Definately, Maybe
 
Skinkie's Avatar
 
Join Date: Dec 2001
Location: Eh, Wherever
Posts: 2,445
10 year veteran! Forum Veteran 
NEW ASSIGNMENT

Alright, here we go round 2 of the syntax battle. This week it's accessing a Vector of Lists. I can feel your excitement building already.

So the basics of this project is we're given a huge text file and have to parse the words out into lists. Each list contains all words of the same length (first list is all the one letter words, second list is two letter, and so on.) But these lists have to be stored in either a vector or another list. Right now I'm trying to get it to work with a vector and it works like element 1 holds the list of one letter words, and so on. My problem is getting the iterator to work. So I'm gonna throw up some generalized code of mine in hopes that someone will point me in the proper direction of how to iterate this monster.

int main()
{
ifstream getword; //instantiates input variable
getword.open("p3.txt"); //opens input file
string tester; //string variable
char ch; //char for browsing file
vector<list <string> > wordcount(25); //Here's the vector
list<string>::iterator it; // Here's the iterator
int i;

/*Code Here that reads the text from the file (this works fine, I've tested it and it's properly reading each word and can correctly write them to a file, so no help is needed here.)
*/


wordcount[tester.length()].push_back(tester);;
/*This line I *think* is pushing the word back onto the appropriate list. If it does what I'm thinking it finds the length of string variable tester and uses that as the vecotr index and then push_back's the word onto that list. *If* I'm right*/

}
/*Here's where it gets confusing, I'm trying to cout the elements of each list, on list at a time, in order. (So again, all 1 letter words will be printed, then all 2 letter words, and so on) But the compiler throws a fit over the second for statement.(see bottom for error message) If I remove the for statement and just set it to wordcount[i].begin() then cout it compiles, but gives me a segmentation fault on runtime. So I figure there is a mistake in the iterator syntax somewhere. I've tried googling for it, and I've tried finding anything even remotely similar in our book (at some point it has to have an answer to something... right?) but have found nothing, so I figured this would be a good time to come back here looking for help. (*Noncheating Disclaimer* And again like last time, I'm looking for help finding errors in my code, and ways to fix my syntax problems, not any kind of full solution or anything like that)*/

for(i = 0; i < 25; i++)
cout << "Words with " << i << " letters";
for(it = wordcount[i].begin(); it < wordcount[i].end(); it++)
cout << *it << "\n";

}

return 0;
}



/*Compiler Error

g++ test.cpp
test.cpp: In function `int main()':
test.cpp:49: no match for `_List_iterator<basic_string<char,string_char_trai ts<c
har>,__default_alloc_template<false,0> >,basic_string<char,string_char_traits<ch
ar>,__default_alloc_template<false,0> > &,basic_string<char,string_char_traits<c
har>,__default_alloc_template<false,0> > *> & < _List_iterator<basic_string<char
,string_char_traits<char>,__default_alloc_template <false,0> >,basic_string<char,
string_char_traits<char>,__default_alloc_template< false,0> > &,basic_string<char
,string_char_traits<char>,__default_alloc_template <false,0> > *>'
*/


Just because people think what I say don't make sense, doesn't mean I don't like popsicles.
Skinkie is offline   you may: quote & reply,
Old 10-16-2005, 03:25 PM   #20
DrMcCoy
Senior Member
 
DrMcCoy's Avatar
 
Join Date: Sep 2003
Location: Braunschweig, Germany
Posts: 1,984
Current Game: nethack
10 year veteran! Forum Veteran 
Quote:
Originally Posted by Skinkie
wordcount[tester.length()].push_back(tester);;
/*This line I *think* is pushing the word back onto the appropriate list. If it does what I'm thinking it finds the length of string variable tester and uses that as the vecotr index and then push_back's the word onto that list. *If* I'm right*/
That line will only work if the vector has at least tester.length()+1 elements, if not, that'll either throw an exception or segfaults... You should do a
wordcount.resize(maximumwordsize)
before doing any operation on the wordcount... Or do
if(tester.length() >= wordcount.size()) wordcount.resize(test.length()+1);
before each push_back()...
(The latter is more time costly, since it often copies the whole memory each time... If there is a maximum word length, use the former)...

...

As for counting the lengths, you can't do it that way for lists... Since a list is a double-linked-list, its iterators don't have a < operator, so you can't use the for-loop... Do something like this instead:
Code:
for(unsigned int i=0;i<wordcount.size();i++)
{
  cout << "Words with " << i << " letters:" << endl;
  it = wordcount[i].begin();
  while(it != wordcount[i].end())
  {
    cout << "  " << *it << endl;
    it++;
  }
}
You could also use a vector of a vector of strings instead of a vector of a list of strings... A list is normally used when you have to insert and delete stuff from the middle of it... Since they are double-linked list, the only thing that has to be done is updating a few pointers, instead of coping lots and lots of memory if you'd do that with vectors... On the downside, you can't access random elements of a list like you can with vectors, you'd always of to traverse them from one end (which is, of course, slower)... Furthermore, in lists, each element takes up slightly more memory, thanks to the pointers...
And depending on what you do, you could also use deques instead...




if(!strcmp(nick, "DrMcCoy") && !strcmp(avatar, "Spock")) return E_INSANE;
DrMcCoy is offline   you may: quote & reply,
Old 10-16-2005, 06:44 PM   #21
Skinkie
Definately, Maybe
 
Skinkie's Avatar
 
Join Date: Dec 2001
Location: Eh, Wherever
Posts: 2,445
10 year veteran! Forum Veteran 
Thanks McCoy, you're always good in a pinch, and yeah I realize that list's are not neccessarily the best way to go about this, but it's required for the assignment we use them. It's supposed to be practice implementing them and stuff. You know teachers.


Just because people think what I say don't make sense, doesn't mean I don't like popsicles.
Skinkie is offline   you may: quote & reply,
Old 10-16-2005, 06:58 PM   #22
DrMcCoy
Senior Member
 
DrMcCoy's Avatar
 
Join Date: Sep 2003
Location: Braunschweig, Germany
Posts: 1,984
Current Game: nethack
10 year veteran! Forum Veteran 
Quote:
Originally Posted by Skinkie
Thanks McCoy
...np...

Quote:
Originally Posted by Skinkie
you're always good in a pinch
...Err, I'm good in a what/where?

Quote:
Originally Posted by Skinkie
and yeah I realize that list's are not neccessarily the best way to go about this, but it's required for the assignment we use them. It's supposed to be practice implementing them and stuff. You know teachers.
...Yes, unfortunatly I do...




if(!strcmp(nick, "DrMcCoy") && !strcmp(avatar, "Spock")) return E_INSANE;
DrMcCoy is offline   you may: quote & reply,
Post a new thread. Add a reply to this thread. Indicate all threads in this forum as read. Subscribe to this forum. RSS feed: this forum RSS feed: all forums
Go Back   LucasForums > Network > Monkey Island Discussion > Random Fun > Ye Olde Harbour > C++ Syntax

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:35 PM.

LFNetwork, LLC ©2002-2011 - All rights reserved.
Powered by vBulletin®
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.