Hi, I have written a program for reversing a digit but I get "error C2664: 'strcpy' : cannot convert parameter 2 from 'std::string' to 'const char *'

Which I don't get since strcpy is supposed to change a string to a char array.

Any clues for whats going on here?

#include <iostream>
#include <iomanip>
#include <string>
#include <numeric>
#include <sstream>

using namespace std;

string itoa(long n);
int reverseDigit(int x);

int main ()
{	
	int number;

	cout << "Enter number to be reversed: " << endl;
	cin >>  number;
	cout << endl;
	cout << "Reversed number is: " << reverseDigit(number) << endl;

	system ("pause");
	return 0;
}
	
int reverseDigit(int x)
{
	/*char reverse[100];*/
	char reverse2[100];
	string reverse1;
	string reverse3;
	int revdig;

	reverse1 = itoa(x);
	/*reverse1.assign(reverse);*/
	unsigned int i;

	for (i = reverse1.size() ; i = 0; i--)
	{
		reverse3.push_back(reverse1[i]);
	}
	strcpy(reverse2, reverse3);
	revdig = atoi(reverse2);
	return revdig;
}

string itoa(long n)
{
	stringstream stream;
	stream << n;
	return stream.str();
}

Which I don't get since strcpy is supposed to change a string to a char array.

It does, if you use it properly.

...I get "error C2664: 'strcpy' : cannot convert parameter 2 from 'std::string' to 'const char *'

Look up the function std::string::c_str().

Edited 6 Years Ago by Fbody: n/a

Ok so now the program runs, there is a logic error now. I tried stepping through it, but it skips over the for loop during stepping. Is that the correct way for running a for loop if you're going from big number down to 0? This is what Ihave now.

#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <numeric>
#include <sstream>

using namespace std;

string itoa(long n);
int reverseDigit(int x);

int main ()
{	
	int number;

	cout << "Enter number to be reversed: " << endl;
	cin >>  number;
	cout << endl;
	cout << "Reversed number is: " << reverseDigit(number) << endl;

	system ("pause");
	return 0;
}
	
int reverseDigit(int x)
{
	
	char reverse2[100];
	string reverse1;
	string reverse3;
	int revdig;
	unsigned int i;

	reverse1 = itoa(x);
	
	for (i = reverse1.size() ; i = 0; i--)
	{
		reverse3.push_back(reverse1[i]);
	}

	char cstr[100];
	strcpy(cstr, reverse3.c_str());
	revdig = atoi(cstr);
	return revdig;
}

string itoa(long n)
{
	stringstream stream;
	stream << n;
	return stream.str();
}

Edited 6 Years Ago by riotburn: n/a

Change i=0 on line 37 to i>=0 (i=0 resolves to false so your loop condition is false and the for loop is skipped)

Also, what's the index of the last element of your string ( reverse1.size() would go one past the end)?

Edited 6 Years Ago by jonsca: Reread code

You can also use modular arithmetic and division for that purpose after getting the input as integer. I mean, think about first iteration you can take x%10 which will give you last number the user entered, after that if you divide the number by 10 then the last number will disappear.Example number is 123:

1st iteration:
x%10 = 3
x /= 10; //then x will be 12

2nd iteration:
x%10 = 2;
x /= 10; //x will be 1

3rd iteration:
x%10 = 1;
x /= 10; // x will be 0 which means that you're done!

-h

Thanks jonsca. Got it to work with some magic. I had tried doing i >= 0, but got an assert: subscript out of range. Good catch on the reverse.size() having to be -1 because of the delimiter. When I would set i > 0 it worked except would leave out the last [0] digit so i just pushed it in after the loop.

Thanks hniza also, I had found a program with that method but since this was hw, and the reversing an array was the idea I thought up in my head. Though I didn't think it would be this much work involved.

Heres the final code.

#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <numeric>
#include <sstream>

using namespace std;

string itoa(long n);
int reverseDigit(int x);

int main ()
{	
	int number;

	cout << "Enter number to be reversed: " << endl;
	cin >>  number;
	cout << endl;
	cout << "Reversed number is: " << reverseDigit(number) << endl;

	system ("pause");
	return 0;
}
	
int reverseDigit(int x)
{
	string reverse1;
	string reverse3;
	int revdig;
	unsigned int i;

	reverse1 = itoa(x);
	
	for (i = (reverse1.size()-1) ; i > 0; i--)
	{
		reverse3.push_back(reverse1[i]);
	}
	reverse3.push_back(reverse1[0]);
	char cstr[100];
	strcpy(cstr, reverse3.c_str());
	revdig = atoi(cstr);
	return revdig;
}

string itoa(long n)
{
	stringstream stream;
	stream << n;
	return stream.str();
}

Edited 6 Years Ago by riotburn: n/a

This question has already been answered. Start a new discussion instead.