Hi everyone just started Daniweb!
I have been using devcpp for some time.I am trying to make a switch from DevCpp to Visual Studio but find the latter confusing having so many options. Recently, I did a console application that will accept a word from user and prints its anagrams. The code compiled and ran successfully. But when I try to compile the same code using Visual studio the program gives wrong output. The code is given below
//Data Structure Class Assignment
//Anagrams Program
//Accepts a word and prints its anagrams.
#include<fstream>
#include <string.h>
#include<iostream>
//for sorting characters in an array using insertion sort
void CharSort (char a[],int n)
{
int i, j = 0;
int key;
for (i = 1; i < n; j = i, i++)
{
key = a[i];
while (j >= 0 && a[j] > key)
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
}
//for finding sum of ASCII values of the characters of a string
int CharSum(char a[],int n)
{
int sum=0,i;
for(i=0;i<n;i++)
sum+=a[i];
return sum;
}
//for comparing 2 words to see whether they are anagrams
bool isanagrams(char a[], char b[],int alen,int blen)
{
if(alen!=blen)
return false;
if(CharSum(a,alen)!=CharSum(b,blen))
return false;
CharSort(b,blen);//CharSort(a,alen) is not required because it is sorted before passing to this function
if(strcmp(a,b)==0)
return true;
}
using namespace std;
int main()
{
ifstream fin;
fin.open("dict.txt",ios::in);
if(!fin)
{
cout<<"File Handling Error\n";
cin.get();
exit(1);
}
char keyword[20],dictword[20],copy[20];//for user input word, dictionary word from file and a copy of dict word for output
bool flag;
int p,q;//for storing strlen of the 2 words.
cout<<"ANAGRAM SOLVER\n"
<<"***************\n";
while(1)
{
fin.clear();
fin.seekg(0);
int count=0;
cout<<"Enter the word (0 to exit)\n";
cin>>keyword;
if(keyword[0]=='0')
break;
p=strlen(keyword);
CharSort(keyword,p);
//loop begins checking each word of file till EOF is reached.
while(!fin.eof())
{ fin>>dictword;
q=strlen(dictword);
int i;
for(i=0;dictword[i]!='\0';i++)
copy[i]=dictword[i];
copy[i]='\0';
if(isanagrams(keyword,dictword,p,q))
{ count++;
cout<<count<<'.';
for(int j=0;copy[j]!='\0';j++)
cout<<copy[j];
cout<<endl;
}
}
if(!count)
cout<<"NO ANAGRAMS FOUND\n";
//loop ends.set file pointer to start of file.
}
return EXIT_SUCCESS;
}
Pls help me with the problem.