top_left top_right
bottom_left
Next Event: Unknown | Forum Rules | QGL Website | Event Registration
openFolder AusForums.com
iconwatfolderLineopenFolder LANs
iconwatfolderLineopenFolder QGL
iconwatfolderLineopenFolder QGL Forum
Author
Topic: does anyone here know c/c++ ?
Goa`uld!
Posts: 510
Location: Brisbane, Queensland

Ok I'm doing this thing... and I need to read a list of words into an array.. and each word has to have it's own index, so I can go like
if (wordArray[2] == secondWord) etc...

But as far as I'm aware you can't have an array of type String in c. Only char.

So is there anyway around this?
system
--
Skitza
Posts: 63
Location: Brisbane, Queensland

f*** arrays :)
Goa`uld!
Posts: 511
Location: Brisbane, Queensland

hmm interesting concept...
maxe
Posts: 2809
Location: Brisbane, Queensland

i think i might have had a similar prob before.

there was some kind of magic command to fix it.

but then im failing C++ and theres every likelyhood i have no idea what im on about. lemme look.
PeaceKeeper
Posts: 772
Location: Brisbane, Queensland

yay for maxe, i failed c :D
KidZ0r
Posts: 1034
Location: Brisbane, Queensland

Does it have anything to do with Structures ??
have a structure with a string in it nasd compare structures not strings ???
been a while cannot remember to much hehe
maxe
Posts: 2810
Location: Brisbane, Queensland

ummm, i dont understand what you really want, but try looking for a command in the help called "atoi". it converts something to something else, which sounds kinda like what you want.
Goa`uld!
Posts: 512
Location: Brisbane, Queensland

I think atoi converts a character into an integer...
log
Posts: 92
Location: Brisbane, Queensland

you could just say its an array of character pointers
eg char * wordArray[] , but i dont know if this will give you a pointer exception

or you could just create a word struct and then make the array an array of pointers to the struct
eg :
struct wordStruct {
char* word;
};

typedef struct wordStruct word;
word wordArray[] ;
wordArra[x]->word = "whatever"

log
Posts: 93
Location: Brisbane, Queensland

umm, if you use ure malloc statments correctly option 1 is the way to go
PsykoNit
Posts: 6
Location: Brisbane, Queensland

My c is a bit rusty, but i know you can't do a straight comparison between strings. you'll need to do use strcmp (or something like that) to do the comparison. As for storing strings in an array: you can setup an array of strings as a two dimensional array:

eg. char strings[10][4];

is the declaration for 10 strings of 3 chars each... (or 4 strings of 9 chars each, i can't remember).
Goa`uld!
Posts: 513
Location: Brisbane, Queensland

yeah i tried the multidimensional array but it just didn't work.
...
Posts: 140
Location: Brisbane, Queensland

pfftt c get some vb in ya
Ferret
Posts: 105
Location: Sydney, New South Wales

larf maxe

log and psychonit are right

too bad i don't understand the question any more than any one else here did:)

i eat breathe and s*** c/C++ code
and drink VB dammit like everyone else
Goa`uld!
Posts: 514
Location: Brisbane, Queensland

ok I have a problem with a code I wrote. What happens is it freezes, like it's gone into a continuous loop


char findInRecord(char findThis[], int findThisLength, char inThis[], int inThisLength)
{
int inThisPosition = 0;
int boolean;
int findThisPosition;
int wordCount = 1;
char *number;

printf("in the findInRecord Function ");
while (inThisPosition < inThisLength) { /* Go through the whole array */
printf("in the outer loop ");
boolean = 1;
findThisPosition = 0;
while ((inThis[inThisPosition] != ' ') && (inThisPosition < inThisLength)) { /* go through a word */
printf("going through a word ");
while((findThisPosition if(inThis[inThisPosition] != findThis[findThisPosition]) {
boolean = 0;
inThisPosition++;
findThisPosition++;
}
}
if (findThisPosition == findThisLength) {
while (inThis[inThisPosition] != ' ') {
inThisPosition++;
}
}
if (inThis[inThisPosition] == ' ') { /* if the last character was a space */
inThisPosition++;
wordCount++;
}
}
if (boolean == 1) { /* if all chars match, return which word it is */
sprintf(number, "%d", wordCount); /* convert int to char */
return(number);
}
}

return('0'); /* return 0 if no instance found */
}


can anyone find why it might be doing that?
Goa`uld!
Posts: 515
Location: Brisbane, Queensland

btw
the params it accepts are

char findThis[]

which is an array like "h,e,l,l,o"

int findThisLength

which is a number that is the length of 'findThis'

char inThis[]

is a record of words like
"h,e,l,l,o, ,t,h,i,s, ,i,s, ,a,n, ,a,r,r,a,y"


int inThisLength

is the length of inThis.
Goa`uld!
Posts: 516
Location: Brisbane, Queensland

damn forum took out tab spaces :(
Leon Trotsky
Posts: 127
Location: Brisbane, Queensland

thx gao'uld - if i was doing taht comp2301 project i'd copy offa you :)

fortunately, i chose assignment 2, which i haven't started but should get through (i hope)

With yer code, from what I can see (considerin i know what you wanna do here)

while((findThisPosition if(inThis[inThisPosition] != findThis[findThisPosition]) {
boolean = 0;
inThisPosition++;
findThisPosition++;
}

your saying WHILE (true if.....)
i think you've found your infinite loop - if I'm reading this wrong (and i might be i think) then I suggest you just do breakpoints, or step throughs and inspect the value of different variables (tutors showed me how)

But yeh, taht while statement seems weird to me!

gl man - if you get desperate you can have my code :D
Toll Booth Willy
Posts: 1027
Location: Brisbane, Queensland

ERKK!

Thats gota look crappy whrn you run it.

for your debug statments put a /n in it, so it looks like this, "hello world /n" The /n makes a newline.

you could make an Array of [x][y] x words, y are the characters in the word.
so [1][0] is H, [1][1] is E, [1][2] is L, ..., [1][4] is O to make HELLO .

And for finding errors in your code its good to space things out a bit, and comment will always help other people looking at your code. Comments wont usually help you, but it helps others ;)
klayman
Posts: 608
Location: Brisbane, Queensland

i thought khel was some c god, or does he just like to pretend he knows it? :)
Unleaded
Posts: 166
Location: Brisbane, Queensland

is this qgl ?

or UQgl ?

LOL!
Lash
Posts: 131
Location: Brisbane, Queensland

If you want an array like you frist stated, you'd need to have array with a series of char points to strings...like

char *wordArray[no_of_words];

and then for each entry in the array you'd have to malloc the area required for the string and free it once you're done.

wordArray[wordnumber] = malloc(string_length, sizeof(char);

Malloc will give you a pointer to some free memory space. You can then set the string using strcpy(). You have to then derefence the pointer to the string with free. This is overly complicated.

Or, do what PsykoNit said and have a multidimensional array, so

char wordArray[no_of_words][max_length_of_words+1];

(the max length of word has to plus one as the string has to be null terminated) and then you can copy a string into an array using strcpy -

strcpy('Some string here', wordarray[word_number]);

This makes sense as a string can be easily considered an array of chars. All that code succeeds in doing is really copying a string to an array of strings. This all seems really pointless though with what it looks like you want which seems to be to step through and seach for a certain string that might be contained in a series of strings and return the first entry that it was found in. Here's a hint - check the string libraries. Or to do it yourself, you want to find a string in a string like

for (int myPos = 0; (myPos + string1Length) <= string2Length; myPos++) {
count = 0;
while ((*(string1 + count) == *(string2 + myPos + count)) && (count != string1length))
count++;
if (count == string1length)
return 1;
}
return 0;

Is COMP2301 the old CS229 subject taken by Schulzy? I did that subjects a few years back, but I think it's changed a bit since then.

edit:

And, if you can't do string1 == string2 either if that's what you want to do...
You'd need to use strcmp (I think that's it from memory - string compare) :)
trog
Posts: 3946
Location: Brisbane, Queensland

lash, you're making that up
Toll Booth Willy
Posts: 1031
Location: Brisbane, Queensland

you dont need pointers if you dun wanna.

Sounds like they havnt learnt about pointer yet, have you?
Goa`uld!
Posts: 519
Location: Brisbane, Queensland

ok I'm doing this anymore.
I'm doing a different assignment.
It's a simple search utility where I can use strstr() without having to go through the darn array one character at a time like this.
Goa`uld!
Posts: 520
Location: Brisbane, Queensland

I haven't learnt anything. Simple fact is, I don't know c. Yet they make me do assignments in it... bastards :(
Hyper
Posts: 5
Location: Australian Capital Territory


arrr.. a few questions here..

"and I need to read a list of words into an array.. and each word has to have it's own index, so I can go like if (wordArray[2] == secondWord) etc..."

Right. Anything else?

About now I'm feeling kinda sad. In 3rd year uni you'll learn all sorts of neat stuff to do things like this damn quick.. and then forget them withing 6 months.

There are built in C++ handlers for creation of arrays etc. Sorry can't remember their names/calls.. it's been 2 years.

But: (to clarify):
Are you comparing only 2 strings.. or more?
Do you need to compare the words in a single string against each other?

ie: I have string 1.. does it exist in string 2?
Or: Does this word "blerg" exist in the string?

Wondering..


Hyper
Posts: 6
Location: Australian Capital Territory



Ok.
Does this help?

http://www.cse.fau.edu/~cot3002l/stringsC++/stringsC++.html

Nice overview of c++: http://www.cee.hw.ac.uk/~pjbk/pathways/cpp1/cpp1.html


What's really nice of some people is when they dump their year's worth of code on a web page somewhere and forget it. Google is such a nice search engine, isn' tit?

The funny thing is.. no matter what your assignment is.. it's pretty much guarenteed that someone's done it before. (with a 50/50 chance of it being on the web somewhere).

So.. want to learn something.. or just pass that damn subject? :)

Jim
Posts: 1379
Location: Brisbane, Queensland

No you can't have an array of type string in C - there is no type string in C. But you can effectively compare two 'strings' using pointers to two arrays of chars to see if they're the same.

Just keep moving *'s and &'s around everywhere until it sort of works, and then wait until Nats comes along to fix it for you.

And definitely don't format it the gay way Lash suggested, with those faget trog-like variableNamesLikeThisHowHomoIsIt - this is really important, especially for your assignment marks. If your lecturer sees gayTrogFormating he'll go 'haha, this guy fails without me even checking the rest cos this code has gayTrogFormat code'

Toll Booth Willy
Posts: 1033
Location: Brisbane, Queensland

I dont see whats wrong with the double array.

cause it dosnt use pointers so it no too complex.

A simple double loop is all it takes to search it?
Nathan
Posts: 1020
Location: Brisbane, Queensland

be sure to comment out all the gayTrogMiniVote() code
arcx33
Posts: 21
Location: Brisbane, Queensland

I think you will find that the comp2301 newsgroup could be very useful for your inquiry.

Anyway,

in C an array of chars is a string as long as the last char in the array is a 0. I dont mean a '0' (character zero), i mean a null 0, as in
str[5] = 0;

you might notice that *argv[] (or **argv) is an array of C strings.

- Ryan Krumins aka arcx33
plok
Posts: 85
Location: Brisbane, Queensland

1st) Get a firm understanding of pointers and how they work.

2nd) With the help of (1), get a firm understanding of C strings, which are simply a null-terminated array of chars.

3rd) Look at the standard C libraries. In your case, strtok spings to mind as being potentially very helpful.

4th) Just when you are on the cusp of an epiphany of enlightenment, fire up doom for an hour then start again at step 1.
Decane
Posts: 116
Location: Brisbane, Queensland

come to qgl forums,
chat to people,
make new friends,
learn c++ by reading other peoples problems!
Jim
Posts: 1389
Location: Brisbane, Queensland

man strtok

[snip]

BUGS
Never use this function. This function modifies its first argument. The identity of the delimit-
ing character is lost. This function cannot be used on constant strings.

[/snip]
system
--
Not a new post since your last visit.
New Post Since your last visit
Back To Forum
Advertise with Us | Privacy Policy | Contact Us
© Copyright 2001-2026 AusGamers Pty Ltd. ACN 093 772 242.
Hosted by Mammoth Networks - Australian VPS Hosting
Web development by Mammoth Media.