On Apr 6, 5:42*pm, "Daniel T." <danie...@earthlink.net> wrote:
> arunix<arru...@gmail.com> 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...
|