Results 1 to 2 of 2

Thread: String Variable in C++

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2008
    Posts
    474

    String Variable in C++

    We can define the variable as string data type or not.

    I have read by including the header file string.h, you can declare string type variable.
    But my program is having errors:- UNDEFINED SYMBOL STRING
    Code:
    #include<iostream.h>
    #include<string.h>
    int main()
    {
    string Name;
    name="sonia";
    cin.get();
    return 0;
    }
    How can we declare the string variable??

  2. #2
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,170

    Re: String Variable in C++

    In ansi c++ the way to write this program is
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
    string Name;
    Name="sonia";
    cin.get();
    return 0;
    If this produces compile errors then the compiler you are using is NOT ansi c++ compliant and I would strongly suggest you change the compiler you are using to one that is ansi compilant.

    Note that in your code you have string Name but use name="sonia". In c++ Name and name are different variables.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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