?
Solved

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

Posted on 2003-02-23
17
Medium Priority
?
206 Views
Last Modified: 2010-04-01
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
Comment
Question by:Sphinx5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
17 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 8003587
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
 

Author Comment

by:Sphinx5
ID: 8003616
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
 

Author Comment

by:Sphinx5
ID: 8003630
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.

 
LVL 9

Expert Comment

by:tinchos
ID: 8003643
remove the

#include <iostream>

line
0
 

Author Comment

by:Sphinx5
ID: 8003647
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
 
LVL 9

Expert Comment

by:tinchos
ID: 8003660
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
 
LVL 9

Expert Comment

by:tinchos
ID: 8003679
I do not have a compiler right here, but i guess that should be working
0
 

Author Comment

by:Sphinx5
ID: 8003691
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
 
LVL 9

Expert Comment

by:tinchos
ID: 8003699
wait, lets make it clear

what is it the source of the data and what is it you want to obtain?
0
 
LVL 9

Expert Comment

by:tinchos
ID: 8003736

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
 
LVL 9

Expert Comment

by:tinchos
ID: 8003737
if that's the case

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

or

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

0
 
LVL 9

Expert Comment

by:tinchos
ID: 8003741
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
 

Author Comment

by:Sphinx5
ID: 8004357
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
 
LVL 9

Expert Comment

by:tinchos
ID: 8004374
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
 

Author Comment

by:Sphinx5
ID: 8004427
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
 
LVL 9

Accepted Solution

by:
tinchos earned 300 total points
ID: 8004454
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
 

Author Comment

by:Sphinx5
ID: 8004484
You're magic, thanks for all your help!!! :D

Sphinx
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question