Functions Type String

Discussion in 'C++' started by arunix, Apr 6, 2010.

  1. arunix

    arunix Guest

    Hello All
    here is one problem arise i am declaring a string type function
    for open a file from the current directory but i dont know much about
    the function type String
    when i complie it it shows the Error
    nonscalar conversion type int to string
    at return 1 i dont understand please tell me about it.....

    this code works fine without decaring it as function........

    std :: string str_show(std :: string str_file)
    {
    std :: ifstream myfile;
    std :: string line;

    myfile.open(str_file.c_str());

    if(!myfile)
    {
    std :: cerr << "Could Not open " ;;
    return 1; // Error
    }

    else
    {
    while(true)
    {
    std :: getline(myfile,line);
    if(!myfile) break;
    std :: cout<< line << std:: endl;
    }

    }
     
    arunix, Apr 6, 2010
    #1
    1. Advertising

  2. arunix

    Ian Collins Guest

    On 04/ 6/10 09:34 PM, arunix wrote:
    > Hello All
    > here is one problem arise i am declaring a string type function
    > for open a file from the current directory but i dont know much about
    > the function type String
    > when i complie it it shows the Error
    > nonscalar conversion type int to string
    > at return 1 i dont understand please tell me about it.....
    >
    > this code works fine without decaring it as function........
    >
    > std :: string str_show(std :: string str_file)


    Why the whitespace around the "::", it's most unusual.

    > {
    > std :: ifstream myfile;
    > std :: string line;
    >
    > myfile.open(str_file.c_str());
    >
    > if(!myfile)
    > {
    > std :: cerr<< "Could Not open " ;;
    > return 1; // Error


    Indeed it is, your function is declared returning a std::string and you
    are attempting to return an int. What do you want to return?

    There isn't an automatic conversion from int to string.

    --
    Ian Collins
     
    Ian Collins, Apr 6, 2010
    #2
    1. Advertising

  3. arunix

    arunix Guest

    On Apr 6, 3:03 pm, Ian Collins <> wrote:
    > On 04/ 6/10 09:34 PM, arunix wrote:
    >
    > > Hello All
    > > here is one problem arise i am declaring a string type function
    > > for open a file from the current directory but i dont know  much about
    > > the function type String
    > > when i complie it it shows the Error
    > > nonscalar conversion type int to string
    > > at return 1 i dont understand please tell me about it.....

    >
    > > this code works fine without decaring it as function........

    >
    > > std :: string str_show(std :: string str_file)

    >
    > Why the whitespace around the "::", it's most unusual.
    >
    > > {
    > >    std :: ifstream myfile;
    > >    std :: string line;

    >
    > >      myfile.open(str_file.c_str());

    >
    > >       if(!myfile)
    > >         {
    > >     std :: cerr<<  "Could Not open " ;;
    > >     return  1;  //  Error

    >
    > Indeed it is, your function is declared returning a std::string and you
    > are attempting to return an int.  What do you want to return?
    >
    > There isn't an automatic conversion from int to string.
    >
    > --
    > Ian Collins



    Thanks dear for reply my post
    i just want to write a function for file open when the function call
    just pass a string "file name" and it will open the given name file
    otherwise it will tell couldn't find.......
    write a function to open the file and the file name will give by the
    user
    and display the list of files and user will chose
     
    arunix, Apr 6, 2010
    #3
  4. arunix

    arunix Guest

    On Apr 6, 5:42 pm, "Daniel T." <> wrote:
    > arunix<> wrote:
    > > here is one problem arise i am declaring a string type function for
    > > open a file from the current directory but i dont know  much about the
    > > function type String when i complie it it shows the Error nonscalar
    > > conversion type int to string at return 1 i dont understand please
    > > tell me about it.....

    >
    > > this code works fine without decaring it as function........

    >
    > > std :: string str_show(std :: string str_file)
    > > {
    > >   std :: ifstream myfile;
    > >   std :: string line;

    >
    > >     myfile.open(str_file.c_str());

    >
    > >      if(!myfile)
    > >        {
    > >   std :: cerr << "Could Not open " ;;
    > >   return  1;  //  Error
    > >        }

    >
    > >      else
    > >        {
    > >   while(true)
    > >  {
    > >    std :: getline(myfile,line);
    > >    if(!myfile) break;
    > >    std :: cout<< line << std:: endl;
    > >  }

    >
    > >        }

    >
    > missing extra '}' here.
    >
    > You have two exits out of this function, the first is at the "return 1;"
    > line. The compiler is expecting you to return a std::string, but instead
    > you are returning an int (this is where your error occurs.) The second
    > exit is at the bottom of the function, but it doesn't return anything
    > and the compiler is expecting a std::string.
    >
    > There is too much above for a single function, you need to break it up
    > more. You want to first make a function that gets a name of a valid file
    > from the user:
    >
    > void open_file_dialog(std::ifstream& file);
    >    // this function keeps asking the user for a file name until it is
    >    // provided one that is valid.
    >
    > Then you want to make a function that can display the contents of the
    > already open file:
    >
    > void display_file(std::ifstream& file);
    >    // this function assumes the file object is already open, and outputs
    >    // its contents to cout.
    >
    > In the open_file_dialog function, you may want to give the user a
    > specific number of tries and then give up. If so, then you will need to
    > verify that the file was actually opened before passing it to the
    > display_file function.- Hide quoted text -
    >
    > - Show quoted text -


    Thanks Daniel.T for the good advise....
    sorry for late respone boz i wasn't int the town...
     
    arunix, Apr 12, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,621
    Victor Bazarov
    Jun 21, 2004
  2. heyo
    Replies:
    3
    Views:
    923
    Dan Pop
    Apr 1, 2004
  3. pete
    Replies:
    4
    Views:
    802
    Dan Pop
    Apr 2, 2004
  4. Yevgen Muntyan

    #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type)))

    Yevgen Muntyan, Feb 9, 2007, in forum: C Programming
    Replies:
    10
    Views:
    915
    Yevgen Muntyan
    Feb 13, 2007
  5. kj
    Replies:
    5
    Views:
    414
Loading...

Share This Page