Function with string parameter
Apr 10, 2011 at 10:38pm UTC
I'm tring to make a small program that checks for a file and if it doesn't find it, send the name of the file to a function that'll make the file with a line of writing. The error I get is "error: expected ',' or '...' before string constant" (line 11 & 25).
Code:
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
/* Section: header */
#include <iostream>
#include <fstream>
using namespace std;
/* Section: constant */
#define NEWLINE '\n'
/* Section: prototype */
void nfound( string "v_file" );
int main() {
string the_file = "test_file.txt" ;
if ( "the_file" ) {
cout << "File found!" ;
} else {
cout << "File not found!" ;
nfound( "the_file" );
}
cin.get();
}
void nfound( string "v_file" ) {
ofstream a_file ( "v_file" );
a_file << "This text will go inside!NEWLINE" ;
a_file.close();
}
I'm new to C++, but from everything I've read, this should be correct.
Apr 10, 2011 at 10:45pm UTC
Your problem is that variables can't start with the "
character.
void nfound( string validVariableName);
Apr 10, 2011 at 10:45pm UTC
Don't put the name of your variable in quotes.
Topic archived. No new replies allowed.