xneo27
asked on
C++ Help
heres the code im having problems with:
inputfile.open(source.c_st r()); //Opens file
char tmpC;
tmpC=/;
char tmpchar1;
char tmpchar2;
counter=0;
while (! inputfile.eof() )
{
counter++;
tmpchar1=getchar(inputfile );
if (tmpchar1 == tmpC) //checks if file is open
{
cout << "found a comment";
}
}
inputfile.close(); //Closes input file
heres the errors im getting:
sr4 $ CC -o Program06 Program06.cpp
"Program06.cpp", line 54: Error: Badly formed expression.
"Program06.cpp", line 61: Error: Too many arguments in call to "std::getchar()".
"Program06.cpp", line 61: Warning: A non-POD object of type "std::ifstream " passed as a variable argument to function "std::getchar()".
"Program06.cpp", line 62: Warning: The variable tmpC has not yet been assigned a value.
"Program06.cpp", line 64: Error: "}" expected instead of "tmpC".
3 Error(s) and 2 Warning(s) detected.
Can anyone help me out here?
inputfile.open(source.c_st
char tmpC;
tmpC=/;
char tmpchar1;
char tmpchar2;
counter=0;
while (! inputfile.eof() )
{
counter++;
tmpchar1=getchar(inputfile
if (tmpchar1 == tmpC) //checks if file is open
{
cout << "found a comment";
}
}
inputfile.close(); //Closes input file
heres the errors im getting:
sr4 $ CC -o Program06 Program06.cpp
"Program06.cpp", line 54: Error: Badly formed expression.
"Program06.cpp", line 61: Error: Too many arguments in call to "std::getchar()".
"Program06.cpp", line 61: Warning: A non-POD object of type "std::ifstream " passed as a variable argument to function "std::getchar()".
"Program06.cpp", line 62: Warning: The variable tmpC has not yet been assigned a value.
"Program06.cpp", line 64: Error: "}" expected instead of "tmpC".
3 Error(s) and 2 Warning(s) detected.
Can anyone help me out here?
i dont know why its giving you that error (in the second post) but it looks like getline() doesnt have that overload.
getline() works like this:
istream& getline (char* s, streamsize n );
where s is your character array, and n is the maximum number of characters to read.
also, getline() is a function of the class istream, so you need to do it something like this
char line[256];
inputfile.getline(line, 256);
also, taking a look at your code, it looks like your logic says you want to take the Nth character in the Nth line, meaning if your text file was
run
bob
run
tempchr1 would be
'r' in the first iteration
'o' in the second
'n' in the third
if thats what you want, thats cool, just looked kinda fishy
fool around with what i said, let me know if you have any more questions
~b
getline() works like this:
istream& getline (char* s, streamsize n );
where s is your character array, and n is the maximum number of characters to read.
also, getline() is a function of the class istream, so you need to do it something like this
char line[256];
inputfile.getline(line, 256);
also, taking a look at your code, it looks like your logic says you want to take the Nth character in the Nth line, meaning if your text file was
run
bob
run
tempchr1 would be
'r' in the first iteration
'o' in the second
'n' in the third
if thats what you want, thats cool, just looked kinda fishy
fool around with what i said, let me know if you have any more questions
~b
ASKER
Well here is what im trying to do and lets see if you know a better way.
Im trying to read a file char by char and count how many time "/*" or "//" show up.
What do you think?
Im trying to read a file char by char and count how many time "/*" or "//" show up.
What do you think?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you
ASKER
Heres the code:
inputfile.open(source.c_st
char tmpC;
char tmpchar1;
char tmpchar2;
counter=0;
while (! inputfile.eof() )
{
counter++;
getline (inputfile,line);
tmpchar1=line[counter];
if (tmpchar1 == '/') //checks if file is open
{
cout << "found a comment";
}
}
inputfile.close();
Here is the error:
sr4 $ CC -o Program06 Program06.cpp
"Program06.cpp", line 64: Error: "}" expected instead of "tmpchar1".
1 Error(s) detected.