fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. // string input; //need to declare user input first
  11. // cout << "Please enter the file you wish to be counted." << endl;
  12. // cin >> input;
  13. // ifstream cppfile(input); //user specified file is opened here
  14.  
  15. istringstream cppfile(
  16. " // comment\n"
  17. " // comment\n"
  18. " // comment\n"
  19. " // comment\n"
  20. " // comment\n"
  21. " // comment\n"
  22. " // comment\n"
  23. );
  24.  
  25. string line; //used to store lines in the file
  26.  
  27. int counter = 0;
  28.  
  29. // if(!cppfile.is_open()){
  30. // cout << "Cannot open that file, try again." << endl;
  31. // }
  32.  
  33. // else{
  34. while(getline(cppfile,line))
  35. {
  36. if(line.find("//") == 0){
  37. continue; //exclude any single lone comments
  38. }
  39. if(line.find("{" or "}") == 0){
  40. continue; //exclude any brackets
  41. }
  42. else if(line.empty()){
  43. continue; //exclude empty lines
  44. }
  45. ++counter;
  46. }
  47. // cppfile.close();
  48. cout << "Number of Lines: " << counter << endl;
  49. // }
  50. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Number of Lines: 7