Thread: string name = 'JOHN';

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    string name = 'JOHN';

    Hi

    I have seen someone using string in this way. Perhaps I'm using wrong syntax. Please tell me where I'm going wrong. Thanks a lot.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    
    int main()
    
    {
    string name = 'JOHN';
    
    cout << name;
    
    system ("pause");
    
    }
    Last edited by jackson6612; 04-27-2011 at 03:15 AM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Registered User inequity's Avatar
    Join Date
    Nov 2010
    Location
    Seattle, Washington
    Posts
    59
    For any null terminated strings, which is what you're setting the string to match, you want to use double quotes. Single quotes are generally used for single characters.

    This syntax would work
    Code:
    string name = "JOHN";

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by inequity View Post
    For any null terminated strings, which is what you're setting the string to match, you want to use double quotes. Single quotes are generally used for single characters.

    This syntax would work
    Code:
    string name = "JOHN";
    Thanks a lot, inequity. It does work. I don't even need the header file #include<string>. By the way, could you please tell me what the difference is between normal string and 'null terminated' string? Thank you.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    C++ automatically adds a terminating null byte to the end of "text" for example. In this case a normal and null terminated string is the same, although you should be aware that strings and character arrays like "text", are not the same.

    Btw, you should include <string> always when you use std::string.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jackson6612 View Post
    I don't even need the header file #include<string>.
    Yes you do. Your code is not guaranteed to work without it. It could, for example, break next time you update your compiler.

    With your current compiler and library you have just gotten lucky - one of the other header files you are #include'ing happens to #include <string>. That is not forbidden, but it cannot be relied on.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by threahdead View Post
    C++ automatically adds a terminating null byte to the end of "text" for example. In this case a normal and null terminated string is the same, although you should be aware that strings and character arrays like "text", are not the same.

    Btw, you should include <string> always when you use std::string.
    Thank you, threahdead, grumpy. Thanks for pointing this out to always include the header file. What is the purpose of adding null byte at the end? Could you please tell me?

    Although this may sound a stupid question, I'm having difficulty understanding how to enter "JOHN" in such a way that it could be displayed (or, couted) several times using the for loop?

    I tried to enter it this way:
    Code:
    char v;
    cout << "Enter the name: ";
    cin >> v; // v is entered as 'JOHN'
    cout << v << endl; //only 'J' is displayed.
    Please help me.

    Best wishes
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jackson6612 View Post
    What is the purpose of adding null byte at the end?
    It is a marker (also called a sentinel) that identifies the end of the string.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    You read the input into a single byte (one chararacter).
    Replace char with string and then it should work as desired.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by threahdead View Post
    You read the input into a single byte (one chararacter).
    Replace char with string and then it should work as desired.
    Thanks both of you, grumpy, threahdead.

    I haven't learned of the string so far. I only know of this because I remember some member used it. But today the instructor asked us to write a code so that your name in capital letters is printed a certain number of times using the for loop. I was wondering how this could be done without using the declaration string because the instructor didn't teach us anything about the string so far. Please tell me. Thanks.
    Last edited by jackson6612; 04-27-2011 at 06:57 AM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  10. #10
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Without strings you will have to use character arrays.

    You have to figure out, how to read from the standard input to a char array.

    For to upper case output you have to find an offset in the ascii table for every character. (Assumed only lower case letters will be read)
    Have a look at Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion to figure out the offset yourself .

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot. Well the funny thing is that he hasn't taught us anything on character arrays too. So how do I do it using some 'elementary' approach?
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  12. #12
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Using character arrays is the basic approach

  13. #13
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    You can print them character by character:
    Code:
    for (int i = 0; i < 10; ++i)
    {
        std::cout << 'J' << 'O' << 'H' << 'N' << std::endl;
    }

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by kmdv View Post
    You can print them character by character:
    Code:
    for (int i = 0; i < 10; ++i)
    {
        std::cout << 'J' << 'O' << 'H' << 'N' << std::endl;
    }
    Thanks, threahdead, kmdv.

    kmdv, your suggestions solved the problem and I'm sure this is what the instructor wanted from us.

    Best regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by threahdead View Post
    Using character arrays is the basic approach
    I would argue the elementary approach in C++ is to use std::string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's up John, got a new xbox?
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-08-2007, 07:08 PM
  2. John Carmack on Facebook?
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-02-2006, 04:29 PM
  3. Revelations in the John
    By IfYouSaySo in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-18-2005, 12:58 AM
  4. John Edward - Crossing Over
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-28-2004, 09:27 AM
  5. John Carmack used goto
    By Silvercord in forum Game Programming
    Replies: 22
    Last Post: 02-06-2003, 09:21 AM