1.11M Members

find_last_of problem in c++?even with an if() check it's not working

 
0
 
cout<<"\n\n Please Input The Drive Letter Of Your Input Device \n";
    cin>>DRL;
    string str(DRL);
    string str2(":");
    string src;
    string hi;
    src=str.find_last_of(DRL);
    cout<<src;
    if(src!=str2)

    {
        hi=string(DRL)+string(str2);
        cout<<hi;
        }


else*/
cout<<hi;

[What am i dong wrong.i am trying to enure a colon input in the drive letter regardless of user input.well it does work if i don't input a semicolon,but if i do it adds a semicolon regardless.What's wrong

 
0
 

The find_last_of function takes more than one parameter. You're trying to feed it just one parameter.

Here are the find_last_of functions. Pick one.
http://www.cplusplus.com/reference/string/string/find_last_of/

I'm a newbie and that link doesn't bode well with me.would you be kind enough to provide me with a relevant example in my case

 
0
 

That page gives the four variations of the function that you can use. Note that in three of them, there is a default value provided for pos so you can feed it just one parameter if you need you, and accept the default value for the unspecified parameter.

All four of them return an object of type size_t. Generally, this is a number.

When you try to do this:

src=str.find_last_of(DRL);

where src is a string, you're telling the compiler that you want to use the find_last_of function that returns a string. There is no such function. Try

int position;
position=str.find_last_of(DRL);

 
0
 

That page gives the four variations of the function that you can use. Note that in three of them, there is a default value provided for pos so you can feed it just one parameter if you need you, and accept the default value for the unspecified parameter.

All four of them return an object of type size_t. Generally, this is a number.

When you try to do this:

src=str.find_last_of(DRL);

where src is a string, you're telling the compiler that you want to use the find_last_of function that returns a string. There is no such function. Try

int position;
position=str.find_last_of(DRL);

That you for solving that bit
However it opened up a new one
in

if(src==str2)

i get
error: no match for 'operator==' in 'src == str2'

What frustrates me even more is that This is a program i wrote in c, tried and tested,but have been assigned to rewrite in c++.I am thankful for your help though.
not this colon checker bit though

 
0
 

error: no match for 'operator==' in 'src == str2'

Whatever types src and str2 are, the compiler does not know how to carry out the operator == on them. What types are they?

 
0
 

error: no match for 'operator==' in 'src == str2'

Whatever types src and str2 are, the compiler does not know how to carry out the operator == on them. What types are they?

What i'm trying to do is check if DVL has a colon.I not then add a colon.If my way is not the right way ,please enlighten me on a more sensible and/or feasible way.

DVL is a char[2]
src is an int
str2 is a sring

 
0
 

it is rightfully not sensible to compare an int and a string to see if they're identical.

If you want to check if the string DVL contains a colon, try this:

#include<iostream>
#include<string>


int main()
{

  std::string DVL("something with:in the middle");

  if (-1 != DVL.find(':'))
  {
    std::cout << "Found a colon";
  }
  else
  {
    std::cout << "Did not find" << std::endl;
  }

 return 0;
}
 
0
 

it is rightfully not sensible to compare an int and a string to see if they're identical.

If you want to check if the string DVL contains a colon, try this:

#include<iostream>
#include<string>


int main()
{

  std::string DVL("something with:in the middle");

  if (-1 != DVL.find(':'))
  {

Thanks Man you are 
    std::cout << "Found a colon";
  }
  else
  {
    std::cout << "Did not find" << std::endl;
  }

 return 0;
}

Thank you very much
That worked and solved my problem

Question Answered as of 2 Years Ago by Moschops
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: