Need some quick C++ help

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
i tried to make an array of apstrings... and if do somehting like array[0]=blah and then array[1]=blah it'll add the two strings instead of storing them separately.... why does it do that? i also tried using a vector and it does the same thing...
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
apstring is some class you wrote, or what?

that's odd. so you do this:

apstring array[10];
array[0]="blah";
array[1]="n;sj";

and you end up with:

array[0]="blahn;sj"
and array[1]=""

???

Are you forgetting to increment an i somewhere, and then array[ i ] is the same element over and over again, and your assignment operator for apstring is overlaoded to append strings like that?
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
apstring is the string class that the AP uses for AP cs... i'm just using it because it is convienient...
for example... i do a for loop... every time storing a different string into the array... and then i cout the first cell of the array.. and all the strings have been added together..
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
no, i didn't forget i++
here's the function:

void input(int numcandidates, apvector<apstring> & names, int votes[100], int & counter) {

apstring input,temp;

cout<<"How many entries will you have?";
cin>>counter;
cout<<"Please enter each entry in the format 'ABC4'"<<endl;
for (int k=0;k<counter;k++) {
cout<<"Please enter the entry: ";
cin>>input;
votes[k]=input[numcandidates]-48;
for (int j=0;j<numcandidates;j++)
temp+=input[j];
names[k]=temp;
cout<<names[k]<<endl;
}
return;
}
 

gwlam12

Diamond Member
Apr 4, 2001
6,946
1
71
when u do cout << array[0];
u get everything together? check to make sure the index to that variable is in there
 

gwlam12

Diamond Member
Apr 4, 2001
6,946
1
71
i dont see how ur program relates to what ur asking for.


when u cout names, u get the whole list, right?



<edit>nm, u edited it, lemme look again </edit>
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
what was it?

temp+=input[j];

appending to your temp?

It's late, I'm not reading real closely.
 

gwlam12

Diamond Member
Apr 4, 2001
6,946
1
71
yea..ur temp is getting longer and longer. and thats going into ur names


is that waht u saw? or did u see something else. or am i wrong.
 

gwlam12

Diamond Member
Apr 4, 2001
6,946
1
71
it was fun while it lasted! :)

i took cs last year in high school...now java in college. i bet u'll be doing the same if ur doing engineering. good luck on ur exams.
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76


<< it was fun while it lasted! :)

i took cs last year in high school...now java in college. i bet u'll be doing the same if ur doing engineering. good luck on ur exams.
>>



i took the ap last year... got a 5... but i haven't programmed since then... forgot a lot, making these dumb mistakes ;)
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
I suggest switching over to the string class, rather than "apstring", cause that's what the rest of the world uses. Hell, I didn't even know what apstring was.

Does this function just read user input and write strings to an array?

if so:

print "how many entries? ";
$num = <>;
for($i=0;$i<$num;$i++){
print "Entry: ";
$entries[$i] = <>;
}


I like perl :)
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
Well, if I wanted to be totally efficient I would use the string class. But this is for my discrete math class, and my teacher is just going to look at output, so I chose the quickest method possible