Convert to upper case only comments from a c++ program

103 views Asked by At

I am trying to read a file and then convert the comments to upper case inside the file.

Here is my code:

char s[100];
void initstring();
void error(char);
void cap(char s[]);
void main(void)
{
  initstring();
  getchar();
}
void initstring()
{

FILE *fp;
fp = fopen("example.txt","r");
while(fgets(s,100,fp)!=NULL)
{
   cap(s);
}

}
void cap(char s[])
{
    cout << "its in";
 for(int i=0;i<strlen(s);i++)
 {
    if(s[i]=='/' && s[i+1]=='/')
    {       int x=i;
        do
        {
            s[x]=toupper(s[x]);
            x++;
        }while(s[x]!='\n');
        break;
    }
    else if(s[i]=='/' && s[i+1]=='*')
    {
        int y=i;
        do
        {
            s[y]=toupper(s[y]);
            y++;
        }while(s[y]!='/');
        break;
    }

 }
 cout << s;
}

Its givning me Warning on s,

warning C4018: '<' : signed/unsigned mismatch

and error on fopen

Error   1   error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

Actually the program was in c initially, and i am now doing it in c++.

Thanks for any help.

1

There are 1 answers

0
Irfan On BEST ANSWER

It's just the behavior of new versions of MSVC. Add #define _CRT_SECURE_NO_WARNINGS to the start of your file to continue using the unsafe functions.

With regard to the warning, that's because strlen returns an unsigned int, rather than a signed int, so you could just make int i into unsigned int i to avoid the warning.