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;
}
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
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.
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
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)