Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Functions Type String

Reply
Thread Tools

Functions Type String

 
 
arunix
Guest
Posts: n/a
 
      04-06-2010
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;
}

}
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-06-2010
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
 
Reply With Quote
 
 
 
 
arunix
Guest
Posts: n/a
 
      04-06-2010
On Apr 6, 3:03*pm, Ian Collins <ian-n...@hotmail.com> 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
 
Reply With Quote
 
arunix
Guest
Posts: n/a
 
      04-12-2010
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...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
type(d) != type(d.copy()) when type(d).issubclass(dict) kj Python 5 12-26-2010 06:48 PM
#define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) Yevgen Muntyan C Programming 10 02-13-2007 02:52 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Re: Type casting- a larger type to a smaller type pete C Programming 4 04-02-2004 05:19 PM
Re: Type casting- a larger type to a smaller type heyo C Programming 3 04-01-2004 06:35 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57