Functions Type String

A

arunix

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;
}

}
 
I

Ian Collins

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.
 
A

arunix

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




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.


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
 
A

arunix

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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Rearranging .ply file via C++ String Parsing 0
Crossword 2
file open in C++ 15
TF-IDF 1
Crossword 14
Identifier not declared? 1
CIN Input #2 gets skipped, I don't understand why. 1
Character operations in C++ 2

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top