8 Replies - 1220 Views - Last Post: 12 May 2013 - 10:58 AM Rate Topic: -----

#1 Ambitious  Icon User is online

  • D.I.C Head

Reputation: 5
  • Posts: 132
  • Joined: 08-May 13

Conversion error?

Posted 10 May 2013 - 04:39 PM

Heres my c++ code:

#include <iostream>
#include <string.h>
using namespace std;
char reverse(char *word);

int main()
{
    char words[100];
    char result[100];
    cout << "Enter a word.\n";
    cin.getline(words, 99);
    result[100] = reverse(words);
    cout << "Result: " << result;
    return 0;
}
char reverse(char *word)
{

    char output[100];
    int slength = strlen(output);
    for(signed int x = slength; x!=-1;x--)
    {
        strcat(output, word[x]);
    }
    return *output;

}


[/b]I get the following errors:
C:\Users\Ambitious\Documents\C++ Projects\WordReverse\main.cpp||In function 'char reverse(char*)':|
C:\Users\Ambitious\Documents\C++ Projects\WordReverse\main.cpp|23|error: invalid conversion from 'char' to 'const char*' [-fpermissive]|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\string.h|41|error: initializing argument 2 of 'char* strcat(char*, const char*)' [-fpermissive]|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|[/b]

Help?

Is This A Good Question/Topic? 0
  • +

Replies To: Conversion error?

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Conversion error?

Posted 10 May 2013 - 05:36 PM

The second argument to strcat is a const char* not a single char as you're doing now.

There's a few other errors as well such as using an invalid index (a size 100 array's last valid index is 99, 0-99) and using char in place of char* when you're attempting to reverse a string.
Was This Post Helpful? 0
  • +
  • -

#3 Skyla Blue  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 01-May 13

Re: Conversion error?

Posted 11 May 2013 - 02:45 PM

Haha ah you finally got around to the challenge I gave you, but it looks like you're doing it in C. Here's the c++ solution without using vector :(

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

string reverse(string myword)
{
	stringstream out;
	for(int x = myword.size(); x > 0; x--)
    {
		out << myword[x-1];
    }
	return out.str();
}

int main()
{
    string word;
    cout << "Enter a word.\n";
    cin >> word;

	cout << reverse(word);
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 Ambitious  Icon User is online

  • D.I.C Head

Reputation: 5
  • Posts: 132
  • Joined: 08-May 13

Re: Conversion error?

Posted 11 May 2013 - 11:31 PM

Oh okay, thank you very much guys!
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3573
  • View blog
  • Posts: 11,112
  • Joined: 05-May 12

Re: Conversion error?

Posted 11 May 2013 - 11:40 PM

Just out of curiosity, why are you guys taking the expensive route of building up a string using strcat(), or a stringstream? Why not mere swap the characters around within the same string?
Was This Post Helpful? 1
  • +
  • -

#6 Zereo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 108
  • Joined: 08-November 12

Re: Conversion error?

Posted 12 May 2013 - 04:29 AM

If you need to have the original word stay the same for some reason you can do it like below, but if you don't need to keep the original string I would recommend doing what Skydiver suggested and just swapping the characters around in the original string.

#include <string>
#include <iostream>

using namespace std;

int main()
{
    string word;
    cout << "Enter a word: ";
    cin >> word;

    cout << "The word reversed is: " << string(word.rbegin(), word.rend());
    return 0;
}



The code above is quite simple actually. The part that does the work of reversing the word is here
string(word.rbegin(), word.rend());

It is using the string object's constructor to create a new string. Remember one of the way we can create a string is by passing it iterators for a word or part of a word we want to create. So if we pass it two reverse iterators of our original word it will create that word reversed.

Here is the documentation on these if you would like to check them out.

string::rbegin
string::rend
string constructors

This post has been edited by Zereo: 12 May 2013 - 04:37 AM

Was This Post Helpful? 1
  • +
  • -

#7 pryious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 13

Re: Conversion error?

Posted 12 May 2013 - 09:06 AM

I tried out the code above but I don't understand how it works... how is it reversing the string? The way I always did it was copy. The old string character by character from the last character into a new string... but the way above seems so much simpler but I just don't understand it. Could someone maybe explain it a bit more? Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#8 Zereo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 108
  • Joined: 08-November 12

Re: Conversion error?

Posted 12 May 2013 - 10:33 AM

If you are talking about the code I posted, it basically doing the same thing as you said you doing but with a little less code. It is copying character by characters starting at the end of the string.

To help understand this more I will give a brief description of what each parameter in the string constructor is used for and also explain what rbegin() and rend() do.


First parameter in the constructor (Where we placed rbegin())

The first parameter in the string constructor we used (There are several of them) takes a iterator that points to the first character in the string we want to copy. The copy will include this character when we create the new string.


Second parameter in the constructor (Where we placed rend())

The second parameter in the string constructor we used take a iterator that points to 1 past the last character we want to copy to the new string. It is very important to remember that the new string that we create won't include whatever the second parameters iterator points to. Meaning the string will contain [First, Last) or everything from first to 1 before last.


string.rbegin()

string.rbegin() will return a reverse iterator(pointer) to the last character in the string. So when we pass it to the first parameter in the the strings constructor we are saying that our starting point is the last character in the string. What makes reverse iterator different then normal iterators is as you probably guessed their functionality is reversed. On a normal iterator that points to the first character in a string if we did ++iterator it would then point to the second character in the string. But reverse iterators are different instead of going to the next character in the string it goes to the previous character when we do ++.


string.rend()

This will return a iterator that theoretically points to 1 before the first character in the string. So remember that our strings constructor doesn't include whatever we pass to the second parameter. So when we pass string.end() to the second parameter we saying copy everything till the first character.


Hope that helps a little bit, I'm not really sure how clear I was on the explanation (Not really strong point and I am tired as heck right now so its probably hard to follow ;p)

Iterators can be hard to understand at first but are a very useful part of C++. Here is a tutorial on them if you want to learn more about them. I would also recommend checking out the documentation on them to.

And as always if you have any questions about anything just let me know and I would be more then willing to help out.
Was This Post Helpful? 1
  • +
  • -

#9 pryious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 13

Re: Conversion error?

Posted 12 May 2013 - 10:58 AM

Thank you. I think I kind of get how it works though still am struggling with some parts like what iterators are and how they work. Ill check out that link you gave and maybe that will help. Thanks again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1