Need help POINTERS TO POINTERS

I need help this is a sample code my instructor gave me and I get an error saying there should be a terminator somewhere in the line wherein "for(i=0; x=2; i<3; i++; x-- )" is in place. If I'm wrong please correct me.

Anyways here's what my instructor needs.

Using array of pointers to pointers write a program that will prompt the use for a string of characters and the program should output the reverse order of the user's info.

BTW: the code below does not prompt the user it only outputs an example I just need help figuring it out. THANKS . ^_^

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
  #include<iostream>
#include <cstdlib>

using namespace std;

int main (){
	
	string students[]={"Mary","John","Joe"};
	string *ptStudents[3];
	string **ptPtStudents[3];
	int i,x;
	
	for (i=0; i<3; i++)
		cout<<*ptStudents[i];
	for(i=0; i<3; i++)
		ptStudents[i]=&*ptStudents[i];
		
	for(i=0; x=2; i<3; i++; x-- )
		ptPtStudents[i]=&ptStudents[x];
		
	for(i=0; i<3; i++)
	cout<<**ptPtStudents[i];
	
	
	return 0;
	system ("pause");
	
	
}
Last edited on
for(part A; part B; part C)
each part is separated by a semi-colon.

1
2
3
4
for(INITIALIZATION; CONDITION; INCREMENT/DECREMENT){
    // Code for the for loop's body
    // goes here.
}
Last edited on
yes i tried putting a semi colon on "x--" still returns with an error
The problem is that on line 18 you have more than three "parts"; you may want use the comma operator (,) to separate portions of, say, the increment part (e.g. i++, x--).
treid it putting on this way

for(i=0; x=2; i<3; i++, x--; )

still return with an error

also with
for(i=0; x=2; i<3; i++, x-- )

same error

for(i = 0, x = 2; i < 3; i++, x--)
Last edited on
well.. @yanson that's what's written above. it returns with an error. anyways I think the code is absolutely wrong and that it's just a reference

so can anybody help me with this..

"Using array of pointers to pointers write a program that will prompt the use for a string of characters and the program should output the reverse order of the user's info."
You seem to be going pointer mad. You are using C++ strings not C-strings so you really don't need all those pointers, something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>

using namespace std;

int main (){

	string students[]={"Mary","John","Joe"};
	string reversedStudents[sizeof(students) / sizeof(string*)];

	for (int i = 0; i < 3; i++)
		cout << students[i] << " ";
        cout << endl;

	for(int i = 0, x = 2; i<3; i++)
		reversedStudents[i] = students[x--];

        for(auto itr : reversedStudents)
           cout << itr << " ";
        cout << endl;

	return 0;
}
Last edited on
EzMdFrk wrote:
also with
for(i=0; x=2; i<3; i++, x-- )

same error
It's still wrong -> i=0; <- misplaced semicolon
well.. @yanson that's what's written above.

No it isn't. Here's Yanson's code with different spacing to highlight what's going on:
1
2
3
for(i = 0, x = 2;  // initialization part has a comma operator
     i < 3;        // condition part
     i++, x--)     // iteration part has a comma operator 
Topic archived. No new replies allowed.