3 Replies - 665 Views - Last Post: 11 October 2012 - 07:07 AM Rate Topic: -----

#1 mati199537  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-October 12

Convert 'const char *' to 'char &' or declaration

Posted 10 October 2012 - 02:26 PM

Hi
I'm Polish, and my English is so poor, but i hope that You help me. I'm trying to write a code for euler-project to reversing and comparing strings. I know, that i can do that by substr, but i'd like to do this on other way.

That's my code:
#include "stdafx.h"
#include "iostream"
#include "String"
#include "sstream"

using namespace std;

int main()
{

string input;
int i, j;
int najwyzsza;
najwyzsza = 0;
ostringstream wynik_s;

for ( int k = 10 ; k < 100 ; k++ )
{
	for ( int t = 10 ; t < 100 ; t++)
	{
		int wynik = t*k;

		
		wynik_s << wynik;
		string str = wynik_s.str();

		input = str;

		const char* p = str.c_str();
		char dst[10];

		strcpy(dst, p);


cin.get(p);

string output(input.length(), ' ');

for ( i = 0 , j = input.length() - 1 ; i < input.length() ; i++ , j--)
{
output[i]=input[j];
}

	if (input == output)
	{
cout<<input;
	}

cout << "Reversed string = " << output;

	}

}
cin.get();
return 0;
}




I'm using MS VS 2010, and i have 2 errors.
First about declaration of 'strcpy',
Second about conversing 'const char *' to 'char &'

I looked a lot about it in the Internet, but i couldn't find anything what could helps me.
Thank you in advance for help,

mati199537

Is This A Good Question/Topic? 0
  • +

Replies To: Convert 'const char *' to 'char &' or declaration

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,235
  • Joined: 23-August 08

Re: Convert 'const char *' to 'char &' or declaration

Posted 10 October 2012 - 03:14 PM

Proper includes:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>


System headers should be included in <>s. Your headers are included in ""s.

cin.get(p);


p is a const char *, you can't get into a const.
Was This Post Helpful? 0
  • +
  • -

#3 mati199537  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-October 12

Re: Convert 'const char *' to 'char &' or declaration

Posted 11 October 2012 - 12:54 AM

Ok, I fixed headers, but still don't know what can I do to make my program working. I understand that's const char * , but if I can't use It there, how can I enter my data ?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3846
  • View blog
  • Posts: 11,771
  • Joined: 25-December 09

Re: Convert 'const char *' to 'char &' or declaration

Posted 11 October 2012 - 07:07 AM

Why are you trying to mix C++ strings with C-strings? Since you seem to know about C++ strings use them for everything. Instead of getting your input using loops and a single character get the entire string using either the extraction operator>> or getline(). See this link for basic input and output in C++.

Also remember C/C++ are case sensitive so String is not equal to string.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1