string help again

This is a discussion on string help again within the C++ Programming forums, part of the General Programming Boards category; I have to ensure that a string entered by the user is all lowercase... here is the code... // String.cpp ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    20

    Question string help again

    I have to ensure that a string entered by the user is all lowercase...

    here is the code...

    // String.cpp

    #include <iostream>
    #include <stdlib.h>
    #include <ctype.h>
    #include "string.h"

    using namespace std;

    void String::setString()
    {
    cout << "Enter a description of the appointment ===>";
    cin.getline(ch, 100);
    }
    int String::StringToLower(char *str)
    {
    while(*str)
    {
    *str = tolower(*str);
    str++;
    }
    return 0;
    }
    void String::Print()
    {
    cout << "Appointment description: ";
    StringToLower(str);
    cout << str;
    cout << endl;
    }
    int main()
    {
    String strn;

    strn.setString();
    strn.Print();

    return 0;
    }

    // Header file String.h

    #ifndef _STRING_H_
    #define _STRING_H_

    class String
    {
    private:
    char ch[100];
    char str[100];

    public:
    void setString();
    int StringToLower(char *str);
    void Print();
    };

    #endif

    It compiles... but this is the output

    Enter a description of the appointment ===>This is a test
    Appointment description: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠
    ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ └..↕
    Press any key to continue

  2. #2
    Unregistered
    Guest
    int String::StringToLower(char *str)
    {
    while(*str)
    {
    *str = tolower(*str);
    str++;
    }
    return 0;
    }


    in the above you appear to pass the string to convert by passing it as a pointer because that way when you change the value of things in the string in the function it will change the string whereever it was sent from. However, by changing the value of str doing this: str++; you loose your anchor point back where you started and introduce the error. I would try this:

    while(int i < strlen(str))
    {
    str[i++] = tolower(str[i]);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 03:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21