I have written c++ code for metric analysis. If I given a c++ program as an input, the system should tell how many private variables, public variables, and public, private methods involved in each class.

the program is

#include <iostream.h>

class metric


  struct variable


     char name[15];
     char scope[12];


  struct method


     char name[30];
     char scope[12];



void main()


  char accspec[15][10]={"public", "private", "protected"};

  char keyword[15][10]={"int", "char", "float", "class"};

  char delimeter[10]={':','}','{',';'};

  FILE *fp;
  int a,type=0;
  char ch_class,str[50];
  char degr_visi[15], key[15],curr[15];   // to store the current keyword, access specifier


  while(ch_class != EOF)

     if ((ch_class!=' ')||(ch_class!='\t')||(ch_class!='\n')||(ch_class!=';')||(ch_class!='}')||(ch_class!='{'));

        if (isalnum(ch_class))

         if (isalpha(ch_class))




         for (int j=0;j<=10;j++)
         if (strcmp(keyword[j],str)==0)

         printf("Current Keyword is:\t%s",key);

         for (int m=0;m<10;m++)
         if (strcmp(accspec[m],str)==0)

         printf("access specifier: <%s>",str);
         printf("Current access specifier is: \t%s",degr_visi);



This program finds the first word of the program, but it is not executing further..

the contents of add1.cpp is

class a
         int a;
         char b,c;

it gives the output as:

current keyword is class...

(not executing further, I have written the code for the variable metric only )

I request your kind help on this regard, and I hope that you will give me well suggestion and guidance.

Waiting for your favourable reply.. Send it as soon as possible

Thanking you


1/ You have not clearly stated what your problem is.

Is the program not functioning as you expected or do you not know how to proceed?

2/ I do not have time at the moment to reverse engineer your code.

A quick glance suggests it looks to be more C than C++ ( struct, printf, fopen, char [], FILE etc..) and non-standard (void main - are you using MSVC?, conio.h, clrscr ) – be aware of what is standard and what is not.

You have no real documentation to help me with the code either.

My first suggestion is to sit back and think about what you need to do. Apply some design first then worry about implementation specifics. Make sure you understand the problem – try dry running some examples (dry running is where you pretend to be the program). Once you understand the problem look for some solutions – maybe there exists some code or libraries that can do this or help with parts of the problem already – if not then maybe there are standard techniques  - such as how to parse text for example (a well documented area) that you can read up on.

Do _not_ try to do the work using just one basic class and an enormous and incomprehensible main function. Use those object oriented techniques to break the problem down into classes which have state (data) and perform services or operations (member functions). Model the things in your problem onto classes. Assign them responsibilities and work out how they should work together. Also where you do require functions – members or not – try not to make them too long or complex – break them down using good old fashioned functional decomposition – it will help keep you sane.

My second suggestion is to rewrite it using a more C++. In particular use the C++ library – things such as std::string and std::vector and iostreams – rather than char [], char *, raw arrays and C FILE  IO – if your development system supports them. I note you include the old iostream header <iostream.h> (but do not seem to use C++ iostreams at all) so maybe you are stuck with some ancient C++ compiler which does not have these. However you can get free compilers and free implementations of the C++ standard library – check out:

http://gcc.gnu.org/          - GNU compiler home
http://www.delorie.com/djgpp/ - GNU port for PC running DOS
http://www.stlport.org/          - free portable C++ standard library implementation
http://www.sgi.com/tech/stl/    - Guide on the STL portion of the C++ library
http://www.digilife.be/quickreferences/PT.htm  - tutorials
http://www.accu.org/          - Development resources and book reviews

Just for starters.

Have fun…  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2016 About.com. All rights reserved.