We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,220 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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

2
Contributors
8
Replies
11 Hours
Discussion Span
1 Year Ago
Last Updated
9
Views
Question
Answered
avgvstvs
Newbie Poster
7 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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/

Moschops
Practically a Posting Shark
889 posts since Sep 2008
Reputation Points: 297
Solved Threads: 170
Skill Endorsements: 5

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

avgvstvs
Newbie Poster
7 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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);

Moschops
Practically a Posting Shark
889 posts since Sep 2008
Reputation Points: 297
Solved Threads: 170
Skill Endorsements: 5

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

avgvstvs
Newbie Poster
7 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

Moschops
Practically a Posting Shark
889 posts since Sep 2008
Reputation Points: 297
Solved Threads: 170
Skill Endorsements: 5

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

avgvstvs
Newbie Poster
7 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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;
}
Moschops
Practically a Posting Shark
889 posts since Sep 2008
Reputation Points: 297
Solved Threads: 170
Skill Endorsements: 5

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

avgvstvs
Newbie Poster
7 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Moschops

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0809 seconds using 2.69MB