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();
}

Recommended Answers

All 9 Replies

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().

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();
}

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)?

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();
}

It's because you are using an unsigned int. When the unsigned value is decremented by 1 beyond zero it goes to the maximum value of the unsigned int (see http://en.wikipedia.org/wiki/Signed_number_representations). Using regular ints it works just find since after 0 is -1.

oooo

oooo

I take it that means it's taken care of... blink twice if no.

;)

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.