Hello, i have to write a program using C++ that will store 3 characters into 3 variables, and using pointers i have to rearrange the order in a function. so if c1, c2, c3 have the value 2,4,6....using pointers i have to switch the values of c1, c2, and c3 to 6,4,2. Here is what i have written so far, but it wont compile correctly, can anyone help?


#include <iostream>

int order_chars( char*, char*, char* );
int main()
{

char c1 = 5;
char c2 = 10;
char c3 = 15;
char* c1ptr;
char* c2ptr;
char* c3ptr;

c1ptr = &c3;
c2ptr = &c2;
c3ptr = &c1;

order_chars ( &c1, &c2, &c3 )


return 0;
}


int order_chars( char c3ptr, char c2ptr, char c1ptr )
{
std::cout << c1ptr << c2ptr << c3ptr << std::endl;

return 0;
}

the only problem i have with the program i wrote above is that the compiler says there a problem, can anyone help me find what is wrong with this program? the program matches the directions above, but theres a problem. please help

Well, for one thing order_chars is declared twice, once with char* and once with char parameters.

It would be helpful to tell us what the compiler actually SAYS rather than "it says there a problem".

Compare this code to your code and it will give you a hint why yours does not work.

#include <iostream>

int order_chars( int*, int*, int* );


int main()
{
  int c1 = 5;
  int c2 = 10;
  int c3 = 15;
  int* c1ptr;
  int* c2ptr;
  int* c3ptr;

  c1ptr = &c3;
  c2ptr = &c2;
  c3ptr = &c1;

  order_chars (c3ptr, c2ptr, c1ptr);

  std::cin.get();  //wait
  return 0; 
}


int order_chars( int* c3ptr, int* c2ptr, int* c1ptr )
{
  std::cout << *c1ptr << *c2ptr << *c3ptr << std::endl;

  return 0;
}

ok heres my new code, but it still says on the compiler that there is a syntax error on line before return i think in the main function.
new code...


#include <iostream>

int order_chars( char*, char*, char* );
int main()
{

char c1 = 5;
char c2 = 10;
char c3 = 15;
char* c1ptr;
char* c2ptr;
char* c3ptr;

c1ptr = &c3;
c2ptr = &c2;
c3ptr = &c1;

order_chars ( c3ptr, c2ptr, c3ptr )


return 0;
}


int order_chars( char* c3ptr, char* c2ptr, char* c1ptr )
{
std::cout << c1ptr << c2ptr << c3ptr << std::endl;

return 0;
}

yea, the syntax error is in function main, before return.

All i see in your code that could give a syntax error is the lack of a semicolon after the function call order_chars.

alright, say i enter a 1,2,3 when my program asks me, when im printing out the contents of the pointers in the function order_chars, it prints 1,2,1....instead it should print 3,2,1....cause the function reorders the characters i entered around. heres my code, can you see the problem?


#include <iostream>

int order_chars( char*, char*, char* );
int main()
{

char c1;
char c2;
char c3;

std::cout << "Enter three characters\n";
std::cin >> c1 >> c2 >> c3;

char* c1ptr;
char* c2ptr;
char* c3ptr;

c1ptr = &c3;
c2ptr = &c2;
c3ptr = &c1;

order_chars ( c3ptr, c2ptr, c3ptr );


return 0;
}


int order_chars( char* c3ptr, char* c2ptr, char* c1ptr )
{
std::cout << * c1ptr << * c2ptr << * c3ptr << std::endl;

return 0;
}

alright, say i enter a 1,2,3 when my program asks me, when im printing out the contents of the pointers in the function order_chars, it prints 1,2,1....instead it should print 3,2,1....cause the function reorders the characters i entered around. heres my code, can you see the problem?

order_chars ( c3ptr, c2ptr, c3ptr );

i have to change my code above to use reference variables instead, what does this mean?

i have to change my code above to use reference variables instead, what does this mean?

It means your prototype and function would take references instead of pointers.

alright, i think i changed the above program to call by reference, here it is to just make sure though.


#include <iostream>

int order_chars( char*, char*, char* );
int main()
{

char c1;
char c2;
char c3;

std::cout << "Enter three characters\n";
std::cin >> c1 >> c2 >> c3;

char* c1ptr;
char* c2ptr;
char* c3ptr;

order_chars ( &c1, &c2, &c3 );


return 0;
}


int order_chars( char* c3ptr, char* c2ptr, char* c1ptr )
{
std::cout << * c1ptr << * c2ptr << * c3ptr << std::endl;

return 0;
}

int order_chars( char*, char*, char* );

This is not call by reference in the C++ sense. It would be like this.

int order_chars( char&, char&, char& );

And you wouldn't use the & on each variable as it is being passed to the function. And in your function you wouldn't dereference the (non) pointer with * in the cout statement.

alright i made those changes, but the compiler gives me a error, it says
/tmp/ccpTbg2D.o(.text+0x69): In function `main':
: undefined reference to `order_chars(char&, char&, char&)'
collect2: ld returned 1 exit status

heres the new code:


#include <iostream>

int order_chars( char&, char&, char& );
int main()
{

char c1;
char c2;
char c3;

std::cout << "Enter three characters\n";
std::cin >> c1 >> c2 >> c3;

char* c1ptr;
char* c2ptr;
char* c3ptr;

order_chars ( c1, c2, c3 );


return 0;
}


int order_chars( char * c3ptr, char * c2ptr, char * c1ptr )
{
std::cout << c1ptr << c2ptr << c3ptr << std::endl;

return 0;
}

The compiler might be kindly telling you that if you change the function declaration, you ought to also change the function definition so that they both indeed refer to they same function.

Otherwise, it might assume you are telling it of the existance of one function that you do not define or provide; while at the same time providing it with a function that the linker does not use.

so how can i fix this problem?

[Added code tags and highlighting to your quoted material.]

alright i made those changes, but the compiler gives me a error, it says

/tmp/ccpTbg2D.o(.text+0x69): In function `main':
: undefined reference to `order_chars(char&, char&, char&)'
collect2: ld returned 1 exit status

heres the new code:

#include <iostream>

int order_chars( char&, char&, char& );
int main()
{

char c1;
char c2;
char c3;

std::cout << "Enter three characters\n";
std::cin >> c1 >> c2 >> c3;

char* c1ptr;
char* c2ptr;
char* c3ptr;

order_chars ( c1, c2, c3 );


return 0;   
}


int order_chars( char * c3ptr, char * c2ptr, char * c1ptr )
{
std::cout << c1ptr << c2ptr << c3ptr << std::endl;

return 0;
}

See how you only changed the declaration but not the definition.

The compiler might be kindly telling you that if you change the function declaration, you ought to also change the function definition so that they both indeed refer to they same function.

Make the function definition match your prototype.

alright, got it...thanks!

ok, im reading about how you can change this program using an array as an argument, i think i should know how to do this, just in case we use it in the class im taking. so far i initialized an array, how can you use an array variable as an argument?
heres my code:

#include <iostream>

int order_chars( &char, &char, &char );
int main()
{

char c1;
char c2;
char c3;

std::cout << "Enter three characters\n";
std::cin >> c1 >> c2 >> c3;

char array[ 3 ] = { c1, c2, c3 };

char* c1ptr;
char* c2ptr;
char* c3ptr;

order_chars ( &c1, &c2, &c3 );


return 0;
}


int order_chars( char* c3ptr, char* c2ptr, char* c1ptr )
{
std::cout << * c1ptr << * c2ptr << * c3ptr << std::endl;

return 0;
}

ok, im reading about how you can change this program using an array as an argument

#include <iostream>

void order_chars ( const char (&array) [ 3 ] )
{
   for ( int i = sizeof array - 1; i >= 0; --i )
   {
      std::cout << array [ i ];
   }
   std::cout << std::endl;
}

int main()
{
   char c [ 3 ];
   std::cout << "Enter " << sizeof c << " characters: ";
   for ( int i = 0; i < sizeof c; ++i )
   {
      std::cin >> c [ i ];
   }
   order_chars ( c );
   return 0;
}

/* my input/output
Enter 3 characters: abc
cba
*/

???

[Jul, please tell me where I have erred. Thanks.]
:)

alright i recieved my first homework assignment, and there are 5 programs due, i got all done but this one, i cant figure it out, can someone give me a helping start. i have to write this code to use pointers only, no global variables. heres the code i have to modify.

MAIN
CALL NUM_PLAYERS
FOR x = 1 TO number
CALL INPUT
CALL COMPUTE
END FOR
CALL OUTPUT
END MAIN

FUNCTION NUM_PLAYERS
READ number -> You must make sure that number entered is >= 0
END NUM_PLAYERS

FUNCTION INPUT -> You must make sure that all numbers entered are >= 0
READ at_bats, singles, doubles, triples, homers
END INPUT

FUNCTION COMPUTE
batting_ave = (singles + doubles + triples + homers)/at_bats
slugging_pct = (singles + doubles*2 + triples*3 + homers*4)/at_bats
team_at_bats = team_at_bats + at_bats
team_hits = team_hits + singles + doubles + triples + homers
PRINT "Player #", x, batting_ave, slugging_pct
END COMPUTE

FUNCTION OUTPUT
team_batting_average = team_at_bast / team_hits
PRINT team_batting_average
END OUTPUT

What is the question? How to pass the pointers without using globals?

in your main you would have, say,

int numberOfPlayers;
int at_bats, singles, doubles, triples, homers;

Then you can call NUM_PLAYERS:

numberOfPlayers = NUM_PLAYERS(); // no parameter at all, but maybe thats cheating?
-- or --
NUM_PLAYERS( &numberOfPlayers ); // with a pointer

and NUM_PLAYERS would be defined as:

int NUM_PLAYERS() // in the first example
--or--
void NUM_PLAYERS( int* numberOfPlayers ) // in the second example.

Likewise INPUT could be called as:

INPUT( &at_bats, &singles, &doubles, &triples, &homers );

and the routine could be defined as:

void INPUT( int* at_bats, int*singles, int* doubles, int* triples, int *homers )

(or maybe return a 'bool' return in case there is an error, like the user refuses to enter valid data. Then you could return 'true' for got valid data and 'false' for user wants to quit or something)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.