Conversion problem, apparently.

This is a discussion on Conversion problem, apparently. within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <cstdlib> #include <cstring> using namespace std; char output[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; char reverseorder(const char *input) { for(int ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    Conversion problem, apparently.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    
    char output[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    
    char reverseorder(const char *input)
    {
    
    for(int a = strlen(input); a>=0; a--)
    {
    strcat(output, input[a]); //what the hell, man? 
    input--;                   //invalid
    }                         //conversion from `const char' to `const char*'
    return *output;
    }
    
    
    int main()
    {
    
    char input[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    
    cout << "Put in something of less than /n 20 characters, chap" << endl;
    cin >> input;
    cout << reverseorder(input) << endl;
    
    return 0;
    
    }
    "invalid conversion from `const char' to `const char*"

    What's up with that?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >strcat(output, input[a]); //what the hell, man?
    strcat takes a string as the second parameter but input[a] is just a single character. Try this instead:
    Code:
    char *reverseorder(const char *input)
    {
    
      int i = 0;
      for(int a = strlen(input) - 1; a>=0; a--)
      {
        output[i++] = input[a];
      }
      return output;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    18
    Thanks a lot. I modified
    Code:
    output[i++] = input[a];
    to

    Code:
    output[i] = input[a];
    i++;
    and i got somewhat of the desired result . . . except, when I run the program then, it goes up until the first whitespace and then it prints no more. How can I include whitespace?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    to include whitespace in the variable input you need to accept user entry using getline() function, not >> operator.

    cin.getline(input, 20, '\n');

    would be a typical syntax though

    cin.getline(input, 20);

    would be essentially the same, as the last parameter defaults to newline char anyway.

    You need to be careful when using both >> and getline() in the same program, however, as there is an issue regarding different methods of handling input under those circumstances. You can look up the topic by searching the board.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    18
    Thank you very much. It worked. I'll search the board more, though. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 06:20 AM
  4. simple currency conversion problem
    By sweetgem in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:41 PM
  5. Replies: 2
    Last Post: 02-07-2002, 09:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21