string not working in c++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gaurav

    string not working in c++

    Hello,

    I am using visual c++ 6 and i am having problems with string to work.

    ******** Here is the program project.cpp**** *****
    #include <iostream.h>
    #include <string>

    #include "stdafx.h"

    // This program just inverts the tickers.csv files execpt first line
    int main(){

    string ticker ;

    return 0;
    }
    *************** ************

    on compiling i get this error message
    *************** ***********
    Compiling...
    StdAfx.cpp
    Compiling...
    Project.cpp
    C:\Windows\Desk top\Project\Pro ject.cpp(17) : error C2065: 'string' :
    undeclared identifier
    C:\Windows\Desk top\Project\Pro ject.cpp(17) : error C2146: syntax error
    : missing ';' before identifier 'ticker'
    C:\Windows\Desk top\Project\Pro ject.cpp(17) : error C2065: 'ticker' :
    undeclared identifier
    Error executing cl.exe.

    Project.exe - 3 error(s), 0 warning(s)
    *************** *************

    can someone tell me whats wrong here ?

    thank you in advance.

    Gaurav
  • WW

    #2
    Re: string not working in c++

    Gaurav wrote:[color=blue]
    > #include <iostream.h>[/color]

    Drop the .h!
    #include <iostream>
    [color=blue]
    > #include <string>
    >
    > string ticker ;[/color]

    std::string ticker;

    --
    WW aka Attila


    Comment

    • Mike Wahler

      #3
      Re: string not working in c++

      "Gaurav" <bansal2425@hot mail.com> wrote in message
      news:42983cb.03 10071125.33b7f6 54@posting.goog le.com...[color=blue]
      > Hello,
      >
      > I am using visual c++ 6 and i am having problems with string to work.
      >
      > ******** Here is the program project.cpp**** *****[/color]
      [color=blue]
      > #include <iostream.h>[/color]

      No such header in standard C++. The header which
      declares the standard streams is <iostream> (no .h)
      You're not using anything from it anyway, so you
      can simply omit this line.
      [color=blue]
      > #include <string>[/color]

      [color=blue]
      > #include "stdafx.h"[/color]

      A Microsoft specific header, not part of standard C++.
      The code you've posted doesn't need it anyway, so
      you can omit this line.
      [color=blue]
      > // This program just inverts the tickers.csv files execpt first line[/color]

      Looks to me like all it does (tries to do) is define a string
      object, then terminate.
      [color=blue]
      > int main(){
      >
      > string ticker ;[/color]

      std::string ticker;
      [color=blue]
      >
      > return 0;
      > }[/color]

      Applying changes I point out, we get:

      #include <string>

      int main()
      {
      std::string ticker;
      return 0;
      }

      which will successfully compile without a diagnostic
      on a compliant implementation, as well as with VC++ v6.0

      [color=blue]
      > *************** ************
      >
      > on compiling i get this error message
      > *************** ***********
      > Compiling...
      > StdAfx.cpp
      > Compiling...
      > Project.cpp
      > C:\Windows\Desk top\Project\Pro ject.cpp(17) : error C2065: 'string' :
      > undeclared identifier[/color]

      Accurate diagnostic message.
      [color=blue]
      > C:\Windows\Desk top\Project\Pro ject.cpp(17) : error C2146: syntax error
      > : missing ';' before identifier 'ticker'[/color]

      An artifact of the first message.
      [color=blue]
      > C:\Windows\Desk top\Project\Pro ject.cpp(17) : error C2065: 'ticker' :
      > undeclared identifier[/color]

      An artifact of the first message.
      [color=blue]
      > Error executing cl.exe.[/color]

      Yup. :-)
      [color=blue]
      >
      > Project.exe - 3 error(s), 0 warning(s)
      > *************** *************
      >
      > can someone tell me whats wrong here ?[/color]

      See above.

      -Mike


      Comment

      • Gaurav

        #4
        Re: string not working in c++

        "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message news:<KKEgb.169 4$dn6.1630@news read4.news.pas. earthlink.net>. ..[color=blue]
        > "Gaurav" <bansal2425@hot mail.com> wrote in message
        > news:42983cb.03 10071125.33b7f6 54@posting.goog le.com...[color=green]
        > > Hello,
        > >
        > > I am using visual c++ 6 and i am having problems with string to work.
        > >
        > > ******** Here is the program project.cpp**** *****[/color]
        >[color=green]
        > > #include <iostream.h>[/color]
        >
        > No such header in standard C++. The header which
        > declares the standard streams is <iostream> (no .h)
        > You're not using anything from it anyway, so you
        > can simply omit this line.
        >[color=green]
        > > #include <string>[/color]
        >
        >[color=green]
        > > #include "stdafx.h"[/color]
        >
        > A Microsoft specific header, not part of standard C++.
        > The code you've posted doesn't need it anyway, so
        > you can omit this line.
        >[color=green]
        > > // This program just inverts the tickers.csv files execpt first line[/color]
        >
        > Looks to me like all it does (tries to do) is define a string
        > object, then terminate.
        >[color=green]
        > > int main(){
        > >
        > > string ticker ;[/color]
        >
        > std::string ticker;
        >[color=green]
        > >
        > > return 0;
        > > }[/color]
        >
        > Applying changes I point out, we get:
        >
        > #include <string>
        >
        > int main()
        > {
        > std::string ticker;
        > return 0;
        > }
        >
        > which will successfully compile without a diagnostic
        > on a compliant implementation, as well as with VC++ v6.0
        >
        >[color=green]
        > > *************** ************
        > >
        > > on compiling i get this error message
        > > *************** ***********
        > > Compiling...
        > > StdAfx.cpp
        > > Compiling...
        > > Project.cpp
        > > C:\Windows\Desk top\Project\Pro ject.cpp(17) : error C2065: 'string' :
        > > undeclared identifier[/color]
        >
        > Accurate diagnostic message.
        >[color=green]
        > > C:\Windows\Desk top\Project\Pro ject.cpp(17) : error C2146: syntax error
        > > : missing ';' before identifier 'ticker'[/color]
        >
        > An artifact of the first message.
        >[color=green]
        > > C:\Windows\Desk top\Project\Pro ject.cpp(17) : error C2065: 'ticker' :
        > > undeclared identifier[/color]
        >
        > An artifact of the first message.
        >[color=green]
        > > Error executing cl.exe.[/color]
        >
        > Yup. :-)
        >[color=green]
        > >
        > > Project.exe - 3 error(s), 0 warning(s)
        > > *************** *************
        > >
        > > can someone tell me whats wrong here ?[/color]
        >
        > See above.
        >
        > -Mike[/color]

        *************** *****
        thanx

        It works fine now. i have another problem using vector<string>



        #include "stdafx.h"
        #include <fstream>
        //#include <ofstream.h>
        #include <string>
        #include <vector>

        using namespace std;

        // This program just inverts the tickers.csv files execpt first line
        int main(){


        //string ticker, line;
        //string input,output;
        //ifstream Tickers( "tickers.tx t", ios::in);
        //ifstream Input_File;
        //ofstream Output_File;
        vector<string> temp;
        /*
        while(Tickers>> ticker){
        input = "tempdata/" + ticker + ".csv";
        output = "tempdata/" + ticker + "1.csv";
        Input_File.open ( input.c_str(), ios::in);
        Output_File.ope n ( output.c_str(), ios::app);
        while ( Input_File >> line ){
        temp.push_back( line);
        }
        Output_File << temp[0] << endl;
        for ( unsigned i = ( temp.size() - 1 ); i > 0; i--)
        Output_File << temp[i] << endl;
        temp.clear();
        Input_File.clos e();
        Output_File.clo se();

        }
        */

        return 0;
        }


        Here effectively i just have vector<string> temp in main, everything
        else is commented.

        If i compile this program i get following warnings

        *************** ******
        Compiling...
        project.cpp
        C:\Windows\Desk top\project\pro ject.cpp(43) : warning C4786:
        'std::reverse_i terator<std::ba sic_string<char ,std::char_trai ts<char>,std::a llocator<char>[color=blue]
        > const *,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char>
        >,std::basic_st ring<ch[/color]
        ar,std::char_tr aits<char>,std: :allocator<char > > const
        &,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char> >
        const *,int>' : identifier was truncated to '255' characters in the
        debug information
        C:\Windows\Desk top\project\pro ject.cpp(43) : warning C4786:
        'std::reverse_i terator<std::ba sic_string<char ,std::char_trai ts<char>,std::a llocator<char>[color=blue]
        > *,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char>
        >,std::basic_st ring<char,std[/color]
        ::char_traits<c har>,std::alloc ator<char> >
        &,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char> >
        *,int>' : identifier was truncated to '255' characters in the debug
        information
        c:\program files\microsoft visual studio\vc98\inc lude\vector(39) :
        warning C4786: 'std::vector<st d::basic_string <char,std::char _traits<char>,s td::allocator<c har>[color=blue]
        >,std::allocato r<std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char>[color=green]
        > >[/color]
        >::vector<std:: basic_string<ch ar,std::char_tr aits<char>,std: :allocator<char >
        >,std::allocato r<std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char>[color=green][color=darkred]
        > > >' : identifier was truncated to '255' characters in the debug[/color][/color][/color]
        information
        c:\program files\microsoft visual studio\vc98\inc lude\vector(60) :
        warning C4786: 'std::vector<st d::basic_string <char,std::char _traits<char>,s td::allocator<c har>[color=blue]
        >,std::allocato r<std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char>[color=green]
        > >[/color]
        >::~vector<std: :basic_string<c har,std::char_t raits<char>,std ::allocator<cha r>
        >,std::allocato r<std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char>[color=green][color=darkred]
        > > >' : identifier was truncated to '255' characters in the debug[/color][/color][/color]
        information
        Linking...

        project.exe - 0 error(s), 4 warning(s)
        *************** ********

        how can i get rid of these ?

        Also if i remove all the comments, i have problems with temp.clear()

        All this program is doing is inverting about 40 files, whose names are
        in tickers.txt.

        thank you in advance.

        Gaurav

        Comment

        • hongky

          #5
          Re: string not working in c++

          in the top of the codes
          u can do it as following, bcoz not namespace:):
          ....
          #include <string>
          ....
          using namespace std;

          void fun()
          {
          std::string str;
          }

          "Gaurav" <bansal2425@hot mail.com> wrote in message
          news:42983cb.03 10071125.33b7f6 54@posting.goog le.com...[color=blue]
          > Hello,
          >
          > I am using visual c++ 6 and i am having problems with string to work.
          >
          > ******** Here is the program project.cpp**** *****
          > #include <iostream.h>
          > #include <string>
          >
          > #include "stdafx.h"
          >
          > // This program just inverts the tickers.csv files execpt first line
          > int main(){
          >
          > string ticker ;
          >
          > return 0;
          > }
          > *************** ************
          >
          > on compiling i get this error message
          > *************** ***********
          > Compiling...
          > StdAfx.cpp
          > Compiling...
          > Project.cpp
          > C:\Windows\Desk top\Project\Pro ject.cpp(17) : error C2065: 'string' :
          > undeclared identifier
          > C:\Windows\Desk top\Project\Pro ject.cpp(17) : error C2146: syntax error
          > : missing ';' before identifier 'ticker'
          > C:\Windows\Desk top\Project\Pro ject.cpp(17) : error C2065: 'ticker' :
          > undeclared identifier
          > Error executing cl.exe.
          >
          > Project.exe - 3 error(s), 0 warning(s)
          > *************** *************
          >
          > can someone tell me whats wrong here ?
          >
          > thank you in advance.
          >
          > Gaurav[/color]


          Comment

          • Mark Warren

            #6
            Re: string not working in c++

            "Gaurav" <bansal2425@hot mail.com> wrote in message
            news:42983cb.03 10072233.3ed2cb 33@posting.goog le.com...[color=blue]
            >
            > It works fine now. i have another problem using vector<string>
            >
            >
            >
            > #include "stdafx.h"
            > #include <fstream>
            > //#include <ofstream.h>
            > #include <string>
            > #include <vector>
            >
            > using namespace std;
            >
            > // This program just inverts the tickers.csv files execpt first line
            > int main(){
            >
            >
            > //string ticker, line;
            > //string input,output;
            > //ifstream Tickers( "tickers.tx t", ios::in);
            > //ifstream Input_File;
            > //ofstream Output_File;
            > vector<string> temp;
            > /*
            > while(Tickers>> ticker){
            > input = "tempdata/" + ticker + ".csv";
            > output = "tempdata/" + ticker + "1.csv";
            > Input_File.open ( input.c_str(), ios::in);
            > Output_File.ope n ( output.c_str(), ios::app);
            > while ( Input_File >> line ){
            > temp.push_back( line);
            > }
            > Output_File << temp[0] << endl;
            > for ( unsigned i = ( temp.size() - 1 ); i > 0; i--)
            > Output_File << temp[i] << endl;
            > temp.clear();
            > Input_File.clos e();
            > Output_File.clo se();
            >
            > }
            > */
            >
            > return 0;
            > }
            >
            >
            > Here effectively i just have vector<string> temp in main, everything
            > else is commented.
            >
            > If i compile this program i get following warnings
            >
            > *************** ******
            > Compiling...
            > project.cpp
            > C:\Windows\Desk top\project\pro ject.cpp(43) : warning C4786:
            >[/color]
            'std::reverse_i terator<std::ba sic_string<char ,std::char_trai ts<char>,std::a l
            locator<char>[color=blue][color=green]
            > > const[/color][/color]
            *,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char>[color=blue][color=green]
            > >,std::basic_st ring<ch[/color]
            > ar,std::char_tr aits<char>,std: :allocator<char > > const
            > &,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char> >
            > const *,int>' : identifier was truncated to '255' characters in the
            > debug information
            > C:\Windows\Desk top\project\pro ject.cpp(43) : warning C4786:
            >[/color]
            'std::reverse_i terator<std::ba sic_string<char ,std::char_trai ts<char>,std::a l
            locator<char>[color=blue][color=green]
            > > *,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char>
            > >,std::basic_st ring<char,std[/color]
            > ::char_traits<c har>,std::alloc ator<char> >
            > &,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char> >
            > *,int>' : identifier was truncated to '255' characters in the debug
            > information
            > c:\program files\microsoft visual studio\vc98\inc lude\vector(39) :
            > warning C4786:[/color]
            'std::vector<st d::basic_string <char,std::char _traits<char>,s td::allocator<c h
            ar>[color=blue]
            >
            >,std::allocato r<std::basic_st ring<char,std:: char_traits<cha r>,std::allocat o[/color]
            r<char>[color=blue][color=green][color=darkred]
            > > >[/color][/color]
            >
            >::vector<std:: basic_string<ch ar,std::char_tr aits<char>,std: :allocator<char >
            >
            >,std::allocato r<std::basic_st ring<char,std:: char_traits<cha r>,std::allocat o[/color]
            r<char>[color=blue][color=green][color=darkred]
            > > > >' : identifier was truncated to '255' characters in the debug[/color][/color]
            > information
            > c:\program files\microsoft visual studio\vc98\inc lude\vector(60) :
            > warning C4786:[/color]
            'std::vector<st d::basic_string <char,std::char _traits<char>,s td::allocator<c h
            ar>[color=blue]
            >
            >,std::allocato r<std::basic_st ring<char,std:: char_traits<cha r>,std::allocat o[/color]
            r<char>[color=blue][color=green][color=darkred]
            > > >[/color][/color]
            >
            >::~vector<std: :basic_string<c har,std::char_t raits<char>,std ::allocator<cha r
            >
            >
            >,std::allocato r<std::basic_st ring<char,std:: char_traits<cha r>,std::allocat o[/color]
            r<char>[color=blue][color=green][color=darkred]
            > > > >' : identifier was truncated to '255' characters in the debug[/color][/color]
            > information
            > Linking...
            >
            > project.exe - 0 error(s), 4 warning(s)
            > *************** ********
            >
            > how can i get rid of these ?[/color]

            These can be safely ignored. If you want to hide them look in the MS
            documentation for the #pragma method to do this. #pragma is not part of
            standard C++, so any detailed questions about this should be directed to a
            MS specific newsgroup.
            [color=blue]
            > Also if i remove all the comments, i have problems with temp.clear()[/color]

            What problems exactly.


            Comment

            • Chris

              #7
              Re: string not working in c++

              > If i compile this program i get following warnings[color=blue]
              >
              > *************** ******
              > Compiling...
              > project.cpp
              > C:\Windows\Desk top\project\pro ject.cpp(43) : warning C4786:
              > 'std::reverse_i terator<std::ba sic_string<char ,std::char_trai ts<char>,std::a llocator<char>[color=green]
              > > const *,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char>
              > >,std::basic_st ring<ch[/color]
              > ar,std::char_tr aits<char>,std: :allocator<char > > const
              > &,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char> >
              > const *,int>' : identifier was truncated to '255' characters in the
              > debug information
              > C:\Windows\Desk top\project\pro ject.cpp(43) : warning C4786:
              > 'std::reverse_i terator<std::ba sic_string<char ,std::char_trai ts<char>,std::a llocator<char>[color=green]
              > > *,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char>
              > >,std::basic_st ring<char,std[/color]
              > ::char_traits<c har>,std::alloc ator<char> >
              > &,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char> >
              > *,int>' : identifier was truncated to '255' characters in the debug
              > information
              >
              > [...snip...]
              >
              > how can i get rid of these ?
              >
              > Also if i remove all the comments, i have problems with temp.clear()
              >
              > All this program is doing is inverting about 40 files, whose names are
              > in tickers.txt.
              >
              > thank you in advance.
              >
              > Gaurav[/color]

              This is a problem with VC. You need to include

              #pragma warning(disable : 4786)

              at the top of the relevant header files. If there are many then do it in stdafx.h

              Cheers,

              Chris

              Comment

              Working...