Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

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!
0
Sphinx5
Asked:
Sphinx5
  • 10
  • 7
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
Sphinx5Author Commented:
You're magic, thanks for all your help!!! :D

Sphinx
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now