C++ Help

Posted on 2006-10-20
Last Modified: 2008-02-01
heres the code im having problems with:;                        //Opens file

 char tmpC;
 char tmpchar1;
 char tmpchar2;
    while (! inputfile.eof() )



      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?
Question by:xneo27
  • 3

Author Comment

ID: 17777815
Updating my issue:
Heres the code:;                        //Opens file

 char tmpC;
 char tmpchar1;
 char tmpchar2;
    while (! inputfile.eof() )



      getline (inputfile,line);
      if (tmpchar1 == '/')                                    //checks if file is open

cout << "found a comment";



Here is the error:

sr4 $ CC -o Program06 Program06.cpp
"Program06.cpp", line 64: Error: "}" expected instead of "tmpchar1".
1 Error(s) detected.

Expert Comment

ID: 17777900
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


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



Author Comment

ID: 17777931
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?

Accepted Solution

0xC0DEB07 earned 500 total points
ID: 17778508
Hi xneo27

Using C style char arrays with C++ is absolutely unnecessary!
Here is how to do it with std::string:

    ifstream ifs;
     std::string str;
     unsigned count=0; ("source.cpp");
     while(std::getline(ifs, str) ){// reads each line until the EOF
          std::cout << str << std::endl;
          if(std::string::npos!=str.find("/*", 0) || std::string::npos!=str.find("//", 0) ) // seeks for /* and // in each line
     cout << count << endl;

Author Comment

ID: 17796529
Thank you

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
strCopies  challenge 17 82
delphi parse string to params 3 97
groovy example issue 10 89
How to obtain Administrator permission when you are the Administrator 6 62
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now