CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2012
    Posts
    9

    [RESOLVED] Capitalize a String in C++

    I have a problem. I need to print the string called "last" in uppercase format. can you check why my program prints nothing.

    here is the important section of the code.


    if (infile.is_open()) // if file was able to open
    {

    string line;
    while (getline(infile, line))
    {
    string::size_type pos1 = line.find(' '); //pos1 is the position of the first space
    if (pos1 != string::npos)
    {
    ++pos1;
    string::size_type pos2 = line.find(',', pos1); //pos2 is the position of the comma
    string last = line.substr(pos1, pos2 - pos1); //remove everything before the first space
    }

    const int length = last.length();
    for(int i=0; i!=length ; ++i)
    {
    last[i] = std::toupper(last[i]);
    }
    cout <<last <<endl;
    outfile << last << endl;

    infile.close();
    outfile.close();
    }

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: Capitalize a String in C++

    Not really. You've only given a small part of your code and it's hard to say what's really going on, especially since this code is working with file IO.

    Why don't you try using your debugger and step through it to see what is happening.

    Also, you could simply use this to capitalize a string.

    Code:
    std::transform(last.begin(), last.end(), last.begin(), std::toupper);

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured