11 Replies - 698 Views - Last Post: 20 May 2011 - 06:55 PM Rate Topic: -----

#1 Simply_Savvy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 20-May 11

Temporary conversion of string using transform function

Posted 20 May 2011 - 02:37 PM

Hey there,

So I am having some trouble with creating a variable that will hold a temporary string conversion. I am getting compiler errors telling me that no conversion is possible between a std::string iterator and a std:: basic string. This is my pertinent code thus far.

 
#include <algorithm>
#include <deque>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

#include <cctype>

class toLower {public: char operator()(char c) const {return tolower(c);}};

struct Email
{
	string email;
};

bool duplicateCheck(deque<Email>& email, Email anEmail)
{
	bool duplicate;
	Email tempA;
	Email tempB;
	
	tempA.email = transform(anEmail.email.begin(), anEmail.email.end(), anEmail.email.begin(), toLower());
	
	for (int i = 0; i < email.size(); i++)
	{
	  tempB.email = transform(email[i].email.begin(), email[i].email.end(), email[i].email.begin(), toLower());
	  
          if (tempA.email == tempB.email) duplicate = true;
          else
	    duplicate = false;
	}
	return duplicate;
}



This is for my final project in a C++ class and I am reading an input file, extracting valid emails, and then testing them to make sure there are no duplicates before being added to an output file. I can transform them PERMANENTLY, but that is going to get me docked points and I can't figure out what variable would hold the transformed text. I'm not an expert in c++ (yet! :) ) and from what I've searched, I can't come up with a solution that helps. Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Temporary conversion of string using transform function

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3104
  • View blog
  • Posts: 10,902
  • Joined: 08-August 08

Re: Temporary conversion of string using transform function

Posted 20 May 2011 - 02:43 PM

It looks to me like 'c' is a char and not a string, but 'email' is a string and not a char.
Was This Post Helpful? 0
  • +
  • -

#3 Simply_Savvy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 20-May 11

Re: Temporary conversion of string using transform function

Posted 20 May 2011 - 03:04 PM

AHA! You are a genius, thank you my good man. It worked perfectly. I also set the temp variables to their respective counterparts before trying to transform them, but even that didn't work until I changed the char to string. Again thank you
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Temporary conversion of string using transform function

Posted 20 May 2011 - 03:05 PM

Transform does not return a string, it returns a string iterator pointing to the element immediately after the last element handled in the transform. You can't use transform to create a temporary; it acts on the actual string passed in.
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Temporary conversion of string using transform function

Posted 20 May 2011 - 03:36 PM

View PostSimply_Savvy, on 20 May 2011 - 05:04 PM, said:

It worked perfectly.

Is your definition of "worked perfectly" == the compiler error message went away?

All you did was introduce a few new errors that the compiler can't detect -- such as that tolower() won't do anything to a string.

I don't know what this means

Quote

I also set the temp variables to their respective counterparts
but it doesn't sound as if you corrected the error that JackOfAllTrades pointed out.
Was This Post Helpful? 0
  • +
  • -

#6 Simply_Savvy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 20-May 11

Re: Temporary conversion of string using transform function

Posted 20 May 2011 - 03:47 PM

You are correct, I thought it was working just because the compiler didn't give me errors, but when I test with a text file that contains the same email address just with random case differences, it still outputs them all.

What I meant by setting the variables to their respective counterparts is:

tempA.email = anEmail.email;
transform(tempA.email.begin(), tempA.email.end(), tempA.email.begin(), toLower());

tempB.email = email[i].email;
transform(tempB.email.begin(), tempB.email.end(), tempB.email.begin(), toLower());



This is what I did that got rid of the compiler errors. So, JackofAllTrades, you are saying there is no way to store the converted text in a temporary variable? If that is the case I must email my Professor immediately.
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Temporary conversion of string using transform function

Posted 20 May 2011 - 03:56 PM

He didn't say there was no way, just not the way you were trying.

Read the documentation for transform.

first1 and last1 are the beginning and end of the source string, so you had those right to begin with. The result iterator is the one that has to point to the destination, so if you want a destination other than the original string, you need to make result point to your temporary.

But as things stand now your temporary is an empty string of 0 length. If you want to use it as the destination for transform you have to resize it to match the length of the input string.
Was This Post Helpful? 0
  • +
  • -

#8 Simply_Savvy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 20-May 11

Re: Temporary conversion of string using transform function

Posted 20 May 2011 - 04:14 PM

Oh, alright. You helped to make sense of the transform documentation (I read it before but didn't really understand it).

transform(anEmail.email.begin(), anEmail.email.end(), tempA.email.begin(), toLower());



Is that the proper use of the result? If it is, I'm still lost because the output did not change. Also, other then setting the temp variable equal to anEmail.email, how would I resize the temp without knowing what the input string's length is ahead of time?
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Temporary conversion of string using transform function

Posted 20 May 2011 - 04:20 PM

You can find out the size of a string as soon as you get the string. Refer to cplusplus.com's online documentation for string.

A suggestion: instead of trying to dive right in using algorithms and containers that you're not thoroughly familiar with, and file I/O operations all at once, why don't you first write a really simple program to try your functions out on test strings & make sure they do what you want before trying to combine them into your actual application?
Was This Post Helpful? 0
  • +
  • -

#10 Simply_Savvy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 20-May 11

Re: Temporary conversion of string using transform function

Posted 20 May 2011 - 04:52 PM

I took your advice and wrote a simple program that just inputs a string from the keyboard and transforms it. However, I guess I don't understand what you mean by resizing the temp variable to match that of the input string. How would I go about predefining the length of an empty string?
Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Temporary conversion of string using transform function

Posted 20 May 2011 - 06:32 PM

Unless this is the last program you will ever write, you have to learn to use language reference material. Please click on the string link and just scan down the list of member functions until you find something interesting, and if the first one that you read isn't exactly what you need, look a little further.
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Temporary conversion of string using transform function

Posted 20 May 2011 - 06:55 PM

Why don't you just clone the original string? Create a new string, initialized with the old string. Then transform the new string.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1