Thread: String Comparison

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    82

    String Comparison

    Ahh back again (audience *groan* ), i heard that

    anway after looking over the tutorial on strings and trying to jury rig one into my old code to see if i could just get a simple comparison going.

    Code:
    # include <iostream>
    # include <stdio.h>
    #
    
    using namespace std;
    
    int Sequence()
    
    {
          
          int Sequence ;
          
        cout << "Input Residue Sequence: "<< endl;
        cin >> Sequence;
        cin.ignore();
        
         cout << "Sequence entered: "<< Sequence <<"\n";
         
         
       return 0;
      
    }
    char Alldone(void)
    
    {
        char done[100];
        
        cout <<"All Done!"<< endl;
        cin >> done;
        cin.ignore();
        cout << "Done: "<< done <<"\n";
        cin.ignore();
        return 0;
        
     /*   
    if ( strcmp ( done, "Yes" ) == 0 ) //equal strings
    
    cout <<"Finished"<< endl;
    cin.ignore();
    
    else
    
        cout<<"continue instead?"<<"\n";
        cin.ignore();
        return 0;
        */
    }   
        
    char Finish(void);
        
        {
            string done;
            
        getline(cin, done, '\n')
        
        if(done == "Yes")
        {
            cout<<"Finished";
            cin.ignore()
        }
        else
        {
            cout<<"continue?";
            cin.ignore();
        }        
        
    int main (int argc, char* argv[]) 
    {
    
    
    
        Sequence();
    
    
       
        Alldone();    
       
       Finish();
    
    cin.get();
     }
    Well i though i could make a seperate function (Finish) to check what output function (Alldone) gave and give a suitable response by comparing it to a predifined string.
    I'am thinking it probably isn't possible to compare "done" from the previous function (Alldone), but then i don't really know .
    The stuff in blue was an earlier attempt until i found out there is a C++ specific string version

    Any help is greatly appreciated

    Regards Wolfe
    Last edited by Cdrwolfe; 03-25-2006 at 06:06 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Make done a string instead of a character array. Then return done from the AllDone function (make AllDone return a string instead of int or char).

    Change Finish to take a string as a parameter and use it instead of querying the user again.

    In main, create a local variable that remembers the value returned by AllDone and pass that variable to Finish.

    If you aren't sure about function return values or parameters/arguments, go back to the function tutorial.

    Also, don't forget to #include <string> when you use the string class.
    Last edited by Daved; 03-25-2006 at 06:16 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Thank you will do, but first bed

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Code:
    # include <iostream>
    # include <stdio.h>
    # include <string>
    
    using namespace std;
    
    int Sequence()
    
    {
          
          int Sequence ;
          
        cout << "Input Residue Sequence: "<< endl;
        cin >> Sequence;
        cin.ignore();
        
         cout << "Sequence entered: "<< Sequence <<"\n";
         
         
       return 0;
      
    }
    string Alldone(void)
    
    {
        string done[100];
        
        cout <<"All Done!"<< endl;
        cin >>  done;
        cin.ignore();
        cout << "Done: "<<   done <<"\n";
        cin.ignore();
        return  done;
        
    
    }   
        
    string Finish(char thedone);
        
        {
            string done;
            
        getline(cin, string done, '\n')
        
        if(done == "Yes")
        {
            cout<<"Finished";
            cin.ignore()
        }
        else
        {
            cout<<"continue?";
            cin.ignore();
        }        
        
    int main (int argc, char* argv[]) 
    {
    
    
    
        Sequence();
    
    
       
        Alldone();    
       
       Finish();
    
    cin.get();
     }
    Well here is the unworking prototype, i can't for the heaven of me think what should be done next.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Finish takes an arguement that you aren't passing to it in main.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    How do you pass it from within main though.

    Finish(string done)???

    I haven't a clue

    sorry

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    In your case, it's best just to take the character thedone out of the function. You aren't using it in the function. Otherwise, though. Since it accepts a character, the function call has to pass it:
    Code:
    Finish('a');
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Quote Originally Posted by Cdrwolfe
    Well here is the unworking prototype, i can't for the heaven of me think what should be done next.
    A good stating place for "what should be done next" is to have code that if possible will compile.

    This is an array of 100 strings. Not a string with a 100 characters. Is that what you really want?
    Code:
    string done[100];
    If it is then you need to use it with an index of the string you want.
    Code:
    cin >> done[index];
    Maybe just.
    Code:
    string done;
    Then this will be fine.
    Code:
    cin >> done;
    You should also remove the "semicolon".
    Code:
    string Finish(string thedone);
    to
    Code:
    string Finish(string thedone)//; remove semicolon
    Here you need a semicolon and remove the word string.
    Code:
    getline(cin, string done, '\n')
    to
    Code:
    getline(cin, /*string*/ done, '\n');//add semicolon
    You might want to add this to your function "Finish".
    Code:
    	//for testing take note of the output?
    	//cout<<"You just entered : " << done << endl;
    This will let you see that you are not getting the input you expect.

    You also need to add a brace to the end of your function "Finish".
    Code:
    		cin.ignore();
    	}   
    
    }// Add missing brace
    I did not point out every change needed to make it compile. But the below code has them. Make of this what you will.
    Code:
    # include <iostream>
    //# include <stdio.h>
    # include <string>
    
    using namespace std;
    
    int Sequence()
    {
    	int Sequence;
    
    	cout << "Input Residue Sequence: "<< endl;
    	cin >> Sequence;
    	cin.ignore();
    
    	cout << "Sequence entered: "<< Sequence <<"\n";
    
    	return 0;
    }
    string Alldone()
    
    {
    	string done/*[100]*/;
    
    	cout <<"All Done!"<< endl;
    	cin >> done;
    	cin.ignore();
    	cout << "Done: "<<   done <<"\n";
    	cin.ignore();
    
    	return  done;
    }   
    
    void /*string*/ Finish(string thedone)//; remove semicolon 
    {
    	string done;
    
    	getline(cin, /*string*/ done, '\n');//add semicolon
    
    	//for testing take note of the output?
    	//cout<<"You just entered : " << done << endl;
    
    	if(done == "Yes")
    	{
    		cout<<"Finished";
    		cin.ignore();//add semicolon
    	}
    	else
    	{
    		cout<<"continue?";
    		cin.ignore();
    	}   
    
    }// Add missing brace 
    
    int main(/*int argc, char* argv[]*/) 
    {
    	Sequence();
    
    	string var1 = Alldone();    
    
    	Finish(var1);
    
    	cin.get();
    }

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Thank you all, i didn't get as much as i thought i would wrong first time round

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM