Welcome! Log In Create A New Profile

Advanced

Assignment 2 Question 3

Posted by Tracey 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Assignment 2 Question 3
May 15, 2007 12:19AM
I have the following code:

int main () {

ifstream fin;
ofstream fout;

cout << "Enter the name of the input file: ";
cin >> fin;

cout << "Enter the name of the output file: ";
cin >> fout;

I'm getting the error ambiguous overload for the line in bold.
How should I be writing this? The names of the files are given by the user so I would have thought that's the best way to write it confused smiley
Re: Assignment 2 Question 3
May 15, 2007 06:39AM
I dont know if this is the best way to do this but it should work.

Declare a character array to store the filename (apparently a string should work as well, but I seem to remember having trouble with that) and assign the users input to that.

e.g

int main() {

char Filename[20];
ifstream fin;
ofstream fout;

cout << "Enter filename: ";
cin >> Filename;

fin.open(Filename);

Obviously you would need 2 variables, one for input and one for output.
I hope this helps.
Re: Assignment 2 Question 3
May 15, 2007 08:14AM
Thanks, I'll try that.
Re: Assignment 2 Question 3
May 15, 2007 03:32PM
Tracey,

Like how Morris showed you above, the name of a file should be a character string array and not a stream like cin, your fin or cout! The fin that you declared in your code above is an input stream object (for reading from a file) equivalent to cin (when you are reading from the keyboad).

You can declare a string representing the name of your file but it will need to be converted to a character string array (old C style) as the constructor/open method for the ifstream and ofstream classes expect a character string array terminated by '\0' character hence you declare it like:

char Filename[20];

Else you could declare it as:

string Filename;
ifstream fin;
ofstream fout;
.
.
.
fin.open(Filename.c_str());
...


Obviously, you need to #include <cstring> for the function c_str().

Hope this explanation will assist you.

Cheers,

Shupi.
Re: Assignment 2 Question 3
May 17, 2007 02:57PM
How do you declare the string #N#? In the example in the textbook they only have one character that has to be replaced so they declare it as a char, but when its three characters together, do you declare it as a string then search for it and replace it with the name?
Re: Assignment 2 Question 3
May 18, 2007 05:09PM
Hi Trace,

Well, the string #N#, like you have mentioned is a string hence you should declare it as a string. Since it's a string literal that doesn't change I suggest you should declare it as a constant like

const string ANY_NAME = "#N#";

Cheers.
Re: Assignment 2 Question 3
May 25, 2007 03:10PM
You can access each character of a string uisng the c_str() function. Alternativley use a char array instead of a string. Either should work.
Sorry, only registered users may post in this forum.

Click here to login