how do i compare strings

This is a discussion on how do i compare strings within the C++ Programming forums, part of the General Programming Boards category; How do i compare strings. more specificly how do jou do the good old Y/N function. you know " do ...

  1. #1
    wierd guy bart's Avatar
    Join Date
    Aug 2001
    Posts
    87

    Question how do i compare strings

    How do i compare strings. more specificly how do jou do the good old Y/N function. you know "do you want to quit (Y/N) ". ive been trying for ages. thanks

    bart

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Use strcmp to compare strings.

    As for your question, I'd put the whole thing in a while loop, kinda like this:
    Code:
    char response = 0;
    
    while(response != 'n')
    {
       //Do your stuff
       printf("Do you want to continue?");
       scanf("%c",response);
    }

  3. #3
    Satan valar_king's Avatar
    Join Date
    Aug 2001
    Posts
    288

    Thumbs up Here's something...

    I'm NO expert with strings, but try this:

    #include <iostream.h>

    int main() {
    char a; //Makes a new character
    cout << "Menu: \n[1] Yes \n[2] No \n>"; //Makes the menu
    cin >> a; //Get the input
    /* Some may argue I sould use select here,
    but I need to practice it first. */
    if (a == 'y') {
    // do y stuff
    }
    if (a == 'n') {
    // do n stuff
    } else {
    // do other stuff
    }
    return 0;
    }

    Please correct me if there's a problem with the code .
    Hope this helps.
    -Save the whales. Collect the whole set.

  4. #4
    wierd guy bart's Avatar
    Join Date
    Aug 2001
    Posts
    87
    i use dev c++. i do

    #include <iostream.h>// for basic in out functions
    #include <stdio.h> // for getchar
    char YorN[1]={'n'}; // Yes or No, set standard to no
    int main()
    {
    point: // for errors
    cout<<"want to know who created this (Y/N)"; //ask
    cin>>YorN; //get the reply
    if (YorN == 'y') // if he wants to know
    {
    cout<<"shuks, im honored. its bart!";
    getchar();
    return 0; //quit
    }
    else
    {
    if (YorN == 'n') // i dint need to put this in,
    // anyone wil say yes
    {
    cout<<"im offended.";
    getchar();
    return 0; //jes, quit
    }
    else
    cout<<"there was a error. please retype if you ";goto point;
    //this is wat happens if i run
    }
    }


    but it keeps on doing cout<<"there was a error. please retype if you ";goto point;

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    It's cleaned up and far from perfect (bad, bad, goto)
    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <conio.h>
    
    char YorN = 'n'; // There's no reason for an array
    
    using namespace std;
    
    int main() 
    { 
    point: // for errors 
    cout << "want to know who created this (Y/N)";
    cin >> YorN;
    
    if (YorN == 'y')
    { 
      cout<<"shucks, im honored. its bart!";
      fflush(stdin);
      getch();
      return 0; //quit
    } 
    else 
    { 
      if (YorN == 'n')
      {
       cout<<"im offended.";
       fflush(stdin);
       getch();
       return 0;
      }
      else
      {
       cout<<"there was a error. please retype if you ";
       goto point;
      }
    }
    }

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Govtcheez you are gonna get whacked by salem's wand of undefined behaviour for using fflush(stdin) again.....

    why the using namespace std when using the old headers?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Jesus, that is horrible code! What the hell is it? LOL
    I compile code with:
    Visual Studio.NET beta2

  8. #8
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Check this link for why you shouldn't fflush(stdin).

  9. #9
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    #include<string>
    #include<iostream>
    
    int main()
    {
    	using namespace std;
    	string s1;
    	cout << "Enter Choice (N for No): ";
    	while( cin >> s1)
    	{
    		if(s1 == "N" || s1 == "n") break;
    		//do some stuff here
    		cout << "Try again (Y/N): ";
    	}
    	return 0;
    }
    I'm not a great C++er but here is some code.
    I compile code with:
    Visual Studio.NET beta2

  10. #10
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Whoops - I had it in there left over form smth else I was doing... Yes, I know Salem's gonna smack me, but it works on Dev-C++, like he has, whether its defined or not. Far from perfect, like i said, but he's got worse things to worry about (like the goto)...

    Jeez gimme a break, people!

  11. #11
    wierd guy bart's Avatar
    Join Date
    Aug 2001
    Posts
    87
    wat do

    using namespace std;

    and

    fflush(stdin);

    do. you can tell im new at c++, and i was testing some new theorys about camparing those darn strings and stuff . they were wrong, so i asked. and this seems along simpler with goto.

  12. #12
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Don't use the goto. There is not need for it.

    the using declaration makes it so that functions from the using class library do not have to be preceded by the word 'using', for example:

    using::cout << "A few words";

    And fflush(stdin) clears the standard input buffer. A more portable code that you could use to do the same thing would be:

    while(getchar() != '\n') continue;

    I think that cin would also have a member that would clear the standard input buffer but I'm not sure what it is called. I'd have to look it up.
    I compile code with:
    Visual Studio.NET beta2

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    firstly witch kings post is slightly wrong .... where he says using read std
    i.e. std::cout not using::cout

    std is a namespace which is like a partition of the global namespace.All the functions,objects etc that are standard are in the std namespace.

    secondly cin does not have a method to flush the input buffer.You have to write it yourself. The closest you can get to a method like this is cin.ignore() which will read and discard chars from the input stream but this never works properly for me when i use it to clear the input buffer.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  14. #14
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    C++ is like other languages in that it is in a constant state of flux. A rather recent addition to the language is the concept of namespaces, the most common of which is the std namespace. I believe Borland became namespace compliant with version 5.0 and VC++ with version 6. Before those versions you could not use namespaces. Your compiler may or may not be compliant with this standard. If it is, it is recommended you use it, rather than the older version, although you can use the older version if you wish with the up to date compilers.

    To my knowledge, stoned_coder is correct in that istreams do not have a built in flush mode like ostreams do, although I'd love to have someone prove me wrong.

    To make things even more confusing you need to know what type of string you are talking about. The word string can refer to a c_style string (which is a null terminated char array) or an instance of a string class, the most common of which are string (in STL), CString (in VC++), and TString(in BC)(also called AnsiString). If you wish you can even make your own string class. The importance is that string classes frequently have overloaded comparison operators like == or < available, whereas c_style strings rely on functions located in string.h or cstring (depending on your compiler) such as strcmp() to do the comparisons. You can compare single char using operators like == and < as your compiler regards chars as integers behind the scenes. However, remember that 'c' is a char and "c" is a string eventhough when printed to the screen they appear the same. Likewise 1 is an int, '1' is a char, and "1" is a string although all three appear the same on the screen.

    To have options is nice, but frequently it's confusing, too.

  15. #15
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    istreams do not have a built in flush mode
    I've never had to many problems with cin.ignore(). Failing that you could remove all the characters from the streambuf. Something like this should work -

    Code:
    void cinflush(istream& in)
    {
        char dustbin[255];
        streambuf* sb;
        sb = in.rdbuf();
        int i = sb->in_avail();
        sb->sgetn(dustbin,i);
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 09:01 PM
  2. how do i compare first letters in two strings?
    By MalickT in forum C Programming
    Replies: 8
    Last Post: 04-20-2008, 06:47 PM
  3. compare strings not working
    By gtriarhos in forum C Programming
    Replies: 7
    Last Post: 09-29-2005, 01:51 PM
  4. Trying to compare to strings...
    By imortal in forum C++ Programming
    Replies: 9
    Last Post: 05-16-2003, 01:27 PM
  5. using Stacks & Queues to compare Strings
    By eskimo083 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2003, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21