Thread: Help....

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Lightbulb Help....

    Please can you execute this file then tell me what I have wrong in it ....
    Thanks

    The file is :

    #include<iostream>
    #include<string>

    int main(){
    string str1;
    string str2;
    cout<<"Please insert the First word"<< endl;
    cin>>str1;
    cout<<"Please insert the second word"<<endl;
    #include<iostream>
    #include<string>

    int main(){
    string str1;
    string str2;
    cout<<"Please insert the First word"<< endl;
    cin>>str1;
    cout<<"Please insert the second word"<<endl;
    cin>>str2;
    cout<< "thank you"<< endl;
    bool an = anagrams(str1, str2);
    if( ! an) {
    cout<<" So sorry.. I cannot chick your strings..."<<endl;
    cout<< " do you know math... can you count the characters.."<<endl;
    cout<< "Please.. do it in a good way next time...by "<<endl;
    }
    else{
    cout<< "they are anagrams";
    }
    return 0;
    }

    bool anagrams(string s1,string s2)
    {

    if ( s2.length()!=s1.length())
    {
    return false;
    }
    else{
    swap(s1);
    swap(s2);
    if( s1 != s2 ) return true; else return flase;
    }// else
    }
    string swap(string s){
    char temp;
    for (int i=0;i<s1.length();i++)
    for (int j=i+1;j<str1.length();j++)
    if (char(s[i])== char(s[j]){
    temp = s[i];
    s[i]= s[j];
    s[j]= temp;
    }//if statement
    return s;
    }

    --------------
    Thanks for your help////
    C++
    The best

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Well the first thing that pops out is that you have two int main() and no code tags.

  3. #3
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    you need prototypes at the top of your program also, or just move your functions to the top, before int main.
    the best things in life are simple.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    I will try to do so then we will see...

    let me try it ... but I feel that will make no different...
    C++
    The best

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    and fix the spelling errors of your variable names and the word false.

    and take out the 2nd bit of includes too.
    Don't you have to have .h in those includes or a: uses namespace std; ? (could be wrong though)
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    I am done with it ...

    here is the code...
    I just start... Here we go...

    #include<iostream>
    #include<string>
    void swap (string & s){
    char temp;
    for (int i=0 ; i < s.length(); i++)
    for (int j= i+1; j< s.length(); j++)
    if ((s[i]) < (s[j])){
    temp = s[i];
    s[i]= s[j];
    s[j]= temp;
    }//if statment
    }
    bool anagrams (string s1 , string s2){
    if ( s2.length()!=s1.length())
    {
    return false;
    }
    else{
    swap(s1);
    swap(s2);
    if( s1 == s2 ) return true;
    else return false;
    }//else
    }
    int main(){
    string str1;
    string str2;
    cout<<"Please insert the First word"<< endl;
    cin>>str1;
    cout<<"Please insert the second word"<<endl;
    cin>>str2;
    cout<< "thank you"<< endl;
    bool an = anagrams(str1, str2);
    if( ! an) {
    cout<<" So sorry.. I cannot chick your strings..."<<endl;
    cout<< " do you know math... can you count the characters.."<<endl;
    cout<< "Please.. do it in a good way next time...by "<<endl;
    }
    else{
    cout<< "they are anagrams"<<endl;
    }
    return 0;
    }


    -------------------
    Thnx God....

    Thanks guys for your help tooo
    C++
    The best

  7. #7
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    please use [CODE] tags?

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    It ran for me so it runs. (after changing the headers to .h)

    just a few comments though:
    Use [ code ] [ \code ] tags.
    Use capital letters in your sentences.
    I can't chick your strings either.

    Suggestions
    have the length check before the anagram check so you can output only one of the error messages instead of both.

  9. #9
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    add .h

    you need

    Code:
    #include<iostream.h>
    #include<string.h>
    use these instead of the ones you have and it should work

  10. #10
    Unregistered
    Guest
    use #include <string> if you want to use string class of STL to hold your strings and to be able to declare them like:

    string word1;

    If you use #include <string> it is advised (necessary?) you indicate what namespace you are going to use, as previously posted.

    If you use #include <string.h> you are including the library for manipulating null terminated char array type strings, and not the library for STL string class.

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    That is right...

    Because of that I used to have
    #include<iosteam>
    #include<string >

    I will be able to modify the LIB itself in the case that can fit my program ...

    Am I wrong...
    C++
    The best

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    I tried to close this one too.

    Please help me closeing this thread tooo

    Thnx
    C++
    The best

Popular pages Recent additions subscribe to a feed