DEV C++ VS Microsoft Visual C++

I did a code using dev c++ and it works just fine. no error during compilation and it ran well. but when i used microsoft visual c++ (i changed int main into void main), the code ran but it had some error when i entered "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG". why is that so?

[i commented some parts that is used in dev c++]

#include <iostream>
#include <string.h>
#include <cstdlib>
using namespace std;

void main()
//int main()
{
int choice=0;

cout<<"\n \t\t -----------------MENU------------";
cout<<"\n \t\t -- 1. Check Duplication --";
cout<<"\n \t\t -- 2. Check Full Alphabet --";
cout<<"\n \t\t ---------------------------------";

do{
cout<<"\n\n Enter number of choice: ";
cin>>choice;

if(!(choice==1 || choice==2))
{
cout<<"\n INCORRECT";
}

}while(!(choice==1 || choice ==2));

if(choice==1)
{
char word[255], temp[255];
int x=0,y=0,a=0,b=0,d=0,ctr=0;
int ctr2=0;

do{ //for the checking of numbers :D
cout<<"\n\n Enter a word: ";
cin>>word;

x=0;
ctr2=0;
while(x!=strlen(word))
{
if(isdigit(word[x]))
{
ctr2++;
}
if(isupper(word[x]))
{
word[x]=tolower(word[x]);
}
x++;
}

if(ctr2>0)
{
cout<<"\n You entered a digit. ";
}

}while(ctr2>0);


x=0;
y=strlen(word);
a=1;
while(ctr!=y)
{
while(!(word[x]!=word[a]))
{
a++;
}
temp[b]=word[x];
b++;
x=a;
a=a+1;

ctr++;
}
temp[b]='\0';
cout<<"\n TEMP: "<<temp;

}
else
{
char alpha[26], sent[80];
int line=0, a=0,b=0,ctr=0;
int ctr2=0, ctr3=0; // -> this is for checking if there are lowercase letters

strcpy(alpha,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");

do{
cout<<"\n\n Enter how many lines: ";
cin>>line;
}while(line<=0);

for(int lines=1; lines<=line; ++lines)
{
do{ //for the checking of lowercase letters

fflush(stdin);
cout<<"\n\n Enter a sentence [ALL CAPS PLEASE]: ";
cin.getline(sent,80);
fflush(stdin);
strcpy(alpha,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");

// THIS PART CHECKS WHETHER A LOWERCASE LETTER IS ENTERED. it asks the user again :) this is connected with the do-while.
a=0;
ctr2=0;
ctr3=0;
while(a!=strlen(sent))
{
if(islower(sent[a]))
{
ctr2++;
}
if(isdigit(sent[a]))
{
ctr3++;
}
a++;
}

if(ctr2>0)
{
cout<<"\n You entered a lowercase letter. ";
}

if(ctr3>0)
{
cout<<"\n You entered a digit.";
}

}while(ctr2>0 || ctr3>0); //this is connected with the do on the upper part


a=0;
b=0;
while(a!=(strlen(sent)))
{
if(isspace(sent[a]))
{
a++;
}
else
{

while(!(sent[a]==alpha[b]))
{
b++;
}
if(sent[a]==alpha[b])
{
alpha[b]='1';
b=0;
a++;
}

}
}

b=0;
while(b!=strlen(alpha))
{
if(alpha[b]=='1')
{
ctr+=1;
}
b++;
}

if(ctr==26)
cout<<"\n SUFFICIENT";
else
cout<<"\n DEFICIENT";
ctr=0;

}

}




fflush(stdin);
cout<<endl;
// system("pause");
// return 0;
}
closed account (z05DSL3A)
main should be int main().

char alpha[26] should be char alpha[27] to allow for a null at the end of it when you do strcpy(alpha,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");. NB: strcpy is not very from a security point of view.

A minor point, you use constructs like (x!=strlen(word)) a lot and the type of x does not match the type returned by strlen();

There are other type mismatches that will produce warnings.

Edit:
This is just initial comments on the code not on the specific error you mentioned.
Last edited on
thanks for your reply Grey Wolf.
(x!=strlen(word)) on my behalf, did not produce any warning. :) but thanks anyway for your comment, i tried changing those which you've said but same thing happens. :D
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
            a=0;
            b=0;
            while(a!=(strlen(sent)))
            {
                if(isspace(sent[a]))
                {
                    a++;
                }
                else
                {

                    while(!(sent[a]==alpha[b]))
                    {
                        b++; 
                    }
                    if(sent[a]==alpha[b])
                    {
                        alpha[b]='1';
                        b=0; 
                        a++; 
                    }

                } 
            }

With "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" as input, when you match the first 'T' you set alpha[b] to '1', so the next 'T' is not matched and b gets incremented until if finds a 'T' outside the bounds of aplha. This will likely throw an access violation at some point.

Edit:
(x!=strlen(word)) on my behalf, did not produce any warning. :)

I compile with warning levels set to 4, treat warning as errors and finally run code analysis, so I got used to seeing type mismatches, I think the standard setting on VC is is warning level 3, so you probably would not see a warning. ;0)
Last edited on
Topic archived. No new replies allowed.