Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Strings

Posted on 2000-05-01
8
Medium Priority
?
513 Views
Last Modified: 2008-03-03
I am having problems converting a string that has uppercase letters to all lower case. this is my program. it is for a cgi application:




#include<iostream>

#include<string>

#include<fstream>



//ADDED

#include "libcgi++.h"

// this is a good cgi form parsing class, compliant with the program
and Unix systems





//This program is in charge of the search done by location.

//What it does is that if the user enters the name of the town

//it open that town file,if that file exists it outputs all

//the info in that file.[ Complexity is O(1)]







void main()

{


//ADDED

cout << "Content-type: text/html\n\n"; // because we will generate HTML

cout << "<HTML><HEAD></HEAD><BODY>"; // start of the HTML page

Cgi *input = new Cgi();

// it uses exceptions

try

{

  input = new Cgi();

}

catch (Cgi::CgiException& e)

{

   cerr << "Error while decoding" << endl << "(maybe called from
command-line?)." << endl;

   // do some other error processing

}

// here: instead of using cin, now you read from the form

// LOOK at the access to input[]: IT MEANS THAT THE VARIABLE NAME IN
FORM.HTML IS "town"



string townName = (*input)["town"];



string x("mayaguez"), y("aguada"), z("aguadilla"), a("rincon"), b("cabo
rojo"), c("san juan");





getline(cin,townName);



strlwr( (char *)townName.c_str());



if
(townName==x||townName==y||townName==z||townName==a||townName==b||townName==c)

{



townName+=string(".txt");





fstream in(townName.c_str(),ios::in);

    if(!in)

    {

       //if this happens the user should be sent to a page

       //that tells to hit back button and start search again



       cout<<"Hit back button"<<"<BR>";

           cout<<"Place not found"<<"<BR>";

       return 0;

    }

    string read;

    while(!in.eof())

    {

       getline(in,read);



       //This are the results of this search which have to

       //be output to the screen.If possible a link should be

       //provided to a page with restaurant menu*/

       

        // Ok, here I have put the "<BR>" linebreak instead of "\n" because we
are writing HTML!!!

        cout<<read<<"<BR>";

        // if you want to have a link, just output this commented line instead

        // note that I have to double the HTML quotes (to get them in the
HTML)

        //cout << "<a href=""http://www.yourlink.here"">" << read << 
"</a><BR>";

    }

}



cout << "</BODY></HTML>"; // ends the HTML page properly



}



I am using this to change it to lowercase but I am getting and error when I compile it in g++. I am using Unix:


strlwr( (char *)townName.c_str());

what I am doing wrong?
0
Comment
Question by:milalik
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2766107
The code

strlwr( (char *)townName.c_str());

is very dangerious.  The pointer returned by c_str() is a constant character pointer.  That is, it is a pointer to characters that must not be changed!  The reason you have to perform that cast (the "(char *)") is that the compiler won't allow you to change those characters because it is unsafe.

A quick fix is to just do

townName = strlwr(townName.c_str());
0
 
LVL 22

Expert Comment

by:nietod
ID: 2766110
Opps that fix is not good.  No you have to use tolower().  But I see Alex has already answered....
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:milalik
ID: 2768803
std::transform(s.begin(), s.end(), s.begin(), ::tolower); ???

what does that means? and where should it be placed?
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2769265
instead of
>>strlwr( (char *)townName.c_str());
set
std::transform(townName.begin(), townName.end(), townName.begin(), ::tolower);

That meebs: make some function (tolower)
from begin to end and store result
from begin od townName

Next is text from BC Help:

transform

      Algorithm

Applies an operation to a range of values in a collection and stores the result.

Syntax

#include <algorithm>
template <class InputIterator,

          class OutputIterator,
          class UnaryOperation>
OutputIterator transform (InputIterator first,
                          InputIterator last,
                          OutputIterator result,

                          UnaryOperation op);

template <class InputIterator1,

          class InputIterator2,
          class OutputIterator,
          class BinaryOperation>
OutputIterator transform (InputIterator1 first1,
                          InputIterator1 last1,
                          InputIterator2 first2,
                          OutputIterator result,

                          BinaryOperation binary_op);

Description

The transform algorithm has two forms. The first form applies unary operation op to each element of the range [first,last), and sends the result to the output iterator result. For example, this version of transform, could be used to square each element in a vector. If the output iterator (result) is the same as the input iterator used to traverse the range, transform, performs its transformation inplace.
The second form of transform applies a binary operation, binary_op, to corresponding elements in the range [first1, last1) and the range that begins at first2, and sends the result to result. For example, transform can be used to add corresponding elements in two sequences, and store the set of sums in a third. The algorithm assumes, but does not check, that the second sequence has at least as many elements as the first sequence. Note that the output iterator result can be a third sequence, or either of the two input sequences.

Formally, transform assigns through every iterator i in the range [result, result + (last1 - first1)) a new corresponding value equal to:

op(*(first1 + (i - result))

or

binary_op(*(first1 + (i - result), *(first2 + (i - result)))

transform returns result + (last1 - first1). op and binary_op must not have any side effects. result may be equal to first in case of unary transform, or to first1 or first2 in case of binary transform.
Exactly last1 - first1 applications of op or binary_op are performed.

Example

 #include<functional>

 #include<deque>
 #include<algorithm>

 #include<iomanip.h>
 using namespace std;

 int main()

 {
   //Initialize a deque with an array of ints
   int arr1[5] = {99, 264, 126, 330, 132};
   int arr2[5] = {280, 105, 220, 84, 210};

   deque<int> d1(arr1, arr1+5), d2(arr2, arr2+5);

   //Print the original values

   cout << "The following pairs of numbers: "
        << endl << "     ";
   deque<int>::iterator i1;
   for(i1 = d1.begin(); i1 != d1.end(); i1++)
      cout << setw(6) << *i1 << " ";
   cout << endl << "     ";
   for(i1 = d2.begin(); i1 != d2.end(); i1++)

      cout << setw(6) << *i1 << " ";

   // Transform the numbers in the deque to their

   // factorials and store in the vector
   transform(d1.begin(), d1.end(), d2.begin(),

             d1.begin(), times<int>());

   //Display the results

   cout << endl << endl;
   cout << "Have the products: " << endl << "     ";
   for(i1 = d1.begin(); i1 != d1.end(); i1++)

     cout << setw(6) << *i1 << " ";

   return 0;

 }

Rogue Wave Standard C++ Library User's Guide and Tutorial
0
 

Author Comment

by:milalik
ID: 2771424
thanx
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 200 total points
ID: 2771939
If the discussion helps you, i am very glad.
Alex
0
 

Author Comment

by:milalik
ID: 2772856
Yes it does help.
=)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

604 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