Thread: some questions about strings

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    7

    some questions about strings

    Hi everybody.
    I'm moving from VB to C++,and i find this language totally awesome except some concepts that are really confusing.I've pretty much got an idea of everything else except using strings.Therefore I wrote out some questions today while i was practicing with my code and I'd really appreciate if someone here could answer them:

    1- How do you input strings from a text file?Because,if the string contains a space,it is taken as a null character and the variable that I use to store the string terminates and doesnt read any more characters.

    2-Lets assume you need to input a string from a user and then compare that string against a value,for example,you ask them "Are you sure that you want to exit the program?".Then you compare the values that the user typed.If he typed ees,then you would exit,and if he typed no,you wont exit.Now..how exactly do you do it since in C++ all strings are stored as characters of arrays and you cant compare a whole array with another?

    3-If I want to convert all the alphabets in a string to lower case how would i do that?Suppose the length of the string is 100 chars.I would use the strlwr() function inside a for loop.But whenever I try doing this:

    Code:
    for (int x=0;x<100;x++)
    {
    string[x]=strlwr(string[x]);
    }
    I get an error.What am I doing wrong?

    4-Can I use the switch case statement to compare char values?

    5-I used an if statement to compare the value inside a char like this:
    Code:
    if (yn=="y")
    {
    // code block here
    }
    but I got an error saying:
    21 C:\WINDOWS\*************\
    invalid type argument of `unary *'
    could someone tell me what is the cause and how i overcome this?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    we'll need more source code to tell you exactly what is wrong

    take a look at http://faq.cprogramming.com for answers to many of these questions

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    1) No idea - it's been too long since I did file i/o in C++. Now I use C# almost all the time.

    2) You could do it the unibomber way and write a loop to compare each character of the char arrays. But in reality you'd want to use strcmp() which I believe is in string.h, but I'm not sure.

    3) I'm not familiar with that function so this is just a guess. strlwr might return an entire string instead of being able to do it with single chars. Tell us what compilet you are using and exactly what error message you are given and I'm sure someone more familiar with C++ functions will be able to pin point the problem.

    4) Yes. It's very common in menus for DOS programs.

    5) I don't see a problem there. Maybe if you post the complete code, the root of the problem could be found there.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    7
    Thanks.I'll just have a look at the FAQs.
    Last edited by ali1; 07-18-2004 at 05:25 PM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    strings are an significant part of C++. There are two basic types of strings--the raw null terminated char array (C-style string) and any of a variety of string classes, the most common of which is the STL string class.


    To make the most of C-style strings you need to use the cstring header file (which is an update of the string.h header file). This header contains functions to copy one string to another (strcpy() and others), concatenate on string onto another (strcat()), compare strings (strcpy() and others), and more.

    One of the strengths of a good string class is having all of those capabilities built right in to the string class as methods/operators. That way you can compare individual strings with the == operator and concatenate strings with the += operator and copy strings with the = operator. If you want, or need, to extract the basic C-style string embedded in a string object you can almost always use the c_str() method, available in every string class I've seen.

    To use an istream, ifstream, or fstream to read data into a string you can use >> to input data up to the first whitespace char and getline() to input data containing whitespace characters. Note that the syntax for using getline() with a C-style string and an STL string are slightly different.

    Whenever you use strings, or talk about strings, it is important to indicate what type of string you are using.

    Yes, you can use a char variable as the argument for a switch statement.
    Last edited by elad; 07-18-2004 at 05:48 PM.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    7
    Quote Originally Posted by sean_mackrory
    3) I'm not familiar with that function so this is just a guess. strlwr might return an entire string instead of being able to do it with single chars. Tell us what compilet you are using and exactly what error message you are given and I'm sure someone more familiar with C++ functions will be able to pin point the problem.
    I'm using Dev-C++ 4.9.8.0,and this is the code:
    Code:
    #include <iostream>
    #include <string.h>
    
    int main()
    {
    char string[100];
    std::cout<<"Type a string:";
    std::cin.getline(string,'\n');
    
    for (int x=0;x<100;x++)
    {
    string[x]=strlwr(string[x]);
    }
    
    std::cout<<"Your string now holds:\n";
    std::cout<<string;
    return 0;
    }
    This is the error I get:
    12 C:\WINDOWS\***************
    invalid conversion from `char' to `char*'
    highlighting the line:
    Code:
    string[x]=strlwr(string[x]);
    I know I can use the built in functions from string.h or any other include file,but I sort of want to write my own string manipulating functions from scratch,because I think that in the long run,it'll be more beneficial to get a better understanding of the whole strings stuff right now instead of using someone else's code while I have no idea how the thing is actually done

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Most of the function with str in their name take in a pointer to a character which it treats as a string. As such
    Code:
    string[x]=strlwr(string[x]);
    Should be:
    Code:
    strlwr(string);
    or for a more standard approach
    Code:
    string[x]=tolower((unsigned char)string[x]);
    To use tolower() you will have to #include <cctype>

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    to keep the header files uptodate with your use of std::cin, etc., you will want to use cstring instead of string.h, cctype instead of ctype.h, cmath instead of math.h, etc.

    cctype (or ctype.h if you must) contains functions like toupper() and tolower() to change case of a given letter.

  9. #9
    01000011 00100000 0010000
    Join Date
    Jul 2004
    Posts
    38
    1) Read the section on file Input/Output at http://www.cprogramming.com (tutorials section)

    2) Use strcmp() for example compare strcmp(1st String, 2nd String) and it will return a non 0 number if true or a 0 if false. strcmp() compares two strings on a case-sensitive basis though. If you wanted to compare two strings and the case of the letters didn't matter (i.e. upper case or lower case) use strcmpi() in the same method. strcmp() and strcmpi() both use string.h though.

    3) Yes, you would use strlwr() but just use the string, like so strlwr(string to be converted) (i.e. a useful example would be:
    Code:
    char convert_this[100] //just declaring the string
    strlwr(convert_this) //converts all 100 letters (if 100 letters are used) in the string
    ). The strlwr() also uses string.h though.

    4. It depends on the situation. Show us some coded examples.
    5. yn=='y' doesn't return a true or false value. You would have to use strcmp() or strcmpi() inside those parenthases. This is because strcmp() and strcmpi() return a non zero value if the statement is true, or a zero value if false. (i.e. if(strcmp(string1==string2)) cout<<string1<<" is equal to "<<string2<<endl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. C++ Strings under the STL?
    By laserlight in forum C++ Programming
    Replies: 9
    Last Post: 07-19-2007, 02:55 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM