What is going on??? (this question is taking the mickey for difficulty)

Hiya all,

#include <iostream>
using namespace std;

int main()
{
     string temp;
     int strn;

     cout << "Please enter something for temp: " << endl;
     cin >> temp;

     cout << "Please enter starting number: " << "\n";
     cin >> strn;

     cout << "The result is: " << temp.substr(strn) << "\n";

     return 0;
}

I'm getting:

Line 10:

error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)


Line 15:

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)


Anybody deal with this pig???



But this works:



#include <iostream>
using namespace std;

int main()
{
   int age;
   cout << "My Second C++ Program" << endl;
   cout << "Please enter your age in years"
        << " followed by <enter>: ";
   cin >> age;
   cout << "\nYou are " << age << " years old." << endl;
   return 0;
}

Thank you!
Sphinx5Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tinchosCommented:
try changing the cout and endl from the iostream library for the std cout and endl

sometimes, the mixing of strings and the iostream cout doesnt work as it should, but it is corrected by using the std::cout

in this way

#include <string>

int main()
{
    string temp;
    int strn;

    std::cout << "Please enter something for temp: " << std::endl;
    std::cin >> temp;

    std::cout << "Please enter starting number: " << std::endl;
    std::cin >> strn;

    std::cout << "The result is: " << temp.substr(strn) << std::endl;

    return 0;
}

(The namespace std is for clarification purposes, you can use the using namespace std; and remove them)
0
Sphinx5Author Commented:
I've tried that source code you put but I've got about 14 errors here and 2 warnings :(:

error C2065: 'string' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'temp'
error C2065: 'endl' : undeclared identifier
error C2039: 'cin' : is not a member of 'std'
error C2297: '<<' : illegal, right operand has type 'char [31]'

a small sample...

0
Sphinx5Author Commented:
I've just tried the following:

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

int main()
{
     string temp;
     int strn = 0;

     cout << "Please enter something for temp: " << endl;
     cin >> temp;

     cout << "Please enter starting number: " << "\n";
     cin >> strn;

     cout << "The result is: " << temp.substr(strn) << "\n";

     return 0;
}


Which works a treat!

Request for question to be made into a PAQ?

Thanks
Sphinx
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

tinchosCommented:
remove the

#include <iostream>

line
0
Sphinx5Author Commented:
Actually if you could fix this I'll give you the points:

I'm wanting something similar to:



function (char *inputter)
{
     string temp;
     strcpy(nfn,inputter);
}

Can you make this happen?

Thanks again,
Sphinx
0
tinchosCommented:
if n is the number of characters from the beginning

for instance

if N = 4 and inputter = "HELLO WORLD"
result = "O WORLD"


what about
string temp( inputbuffer[nfn] );
0
tinchosCommented:
I do not have a compiler right here, but i guess that should be working
0
Sphinx5Author Commented:
I'm getting:

error C2664: 'strcpy' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called


inputter already has something in it which is set up in the function that calls 'function'.
0
tinchosCommented:
wait, lets make it clear

what is it the source of the data and what is it you want to obtain?
0
tinchosCommented:

function (char *inputter)
{
    string temp;
    strcpy(nfn,inputter);
}


the idea is that in temp you want to obtain a substring from inputter, being nfn the position from where to begin?

for example

inputter = "Hello World"
nfn = 2

then you want temp = "llo World"

?????
0
tinchosCommented:
if that's the case

function (char *inputter)
{
   string temp( inputter + nfn );
}

or

function (char *inputter)
{
   string temp( inputter[nfn] );
}

0
tinchosCommented:
otherwise i'm not understanding you, so tell me what is it you are trying to acomplish and i'll try to help you
0
Sphinx5Author Commented:
Hiya,

Right....Its a function that takes char * as part of its argument. Inside the function whatever is passed is simply made the contents of the string i.e:

int main()
{
   char val[25];
   funccall(val);

   return 0;
}

void funccall(char *inputter)
{
  string temp;

  //I want to make temp equal to the contents of the argument inputter.
}
0
tinchosCommented:
ok

in that case the code is like the one above
being nfn the offset you want from the begining of inputter


function (char *inputter)
{
  string temp( inputter + nfn );
}

or

function (char *inputter)
{
  string temp( inputter[nfn] );
}

ie:
inputter = "Hello World"
nfn = 3
temp = "lo World"

case in which the string takes the all value of inputter

function (char *inputter)
{
  string temp( inputter );
}

ie:
inputter = "Hello World"
temp = "Hello World"
0
Sphinx5Author Commented:
That'll do me, I'll accept that comment, but before I do could you also answer this (I've upped the points don't worry I'm not ripping you off! :))

In that code that I was given you we put the contents of a char* variable into a string variable, but how do I do it the other way around?
0
tinchosCommented:
in order to go from a string to a char*

string temp = "Hello World";
char *var;

var = temp.c_str();

Result:
temp = "Hello World"
var = "Hello World"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sphinx5Author Commented:
You're magic, thanks for all your help!!! :D

Sphinx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.