Comparing Strings

This is a discussion on Comparing Strings within the C++ Programming forums, part of the General Programming Boards category; How would I have my program compare input from a user to an already stored string within the program? Plus, ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    13

    Comparing Strings

    How would I have my program compare input from a user to an already stored string within the program? Plus, how would I make my program give the ability of multiple choice?

    Something like this would pop up on the screen:

    The boy's ball is:
    A. Blue
    B. Red



    Then the user has to press either A or B and then press Enter. The program then compares the input from the user to a stored char and if correct moves on.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    What have you tried ?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    Code:
    //Study - program that allows User input/output
    //for studying.  Program asks User to state
    //what user thinks is answer for problem at hand.
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
    	string char1;
    	cout <<"The Boy's Ball is: ";
    	cout <<"A. Blue.\n";
    	cout <<"B. Red.\n";
        cin >> char1;
    
    	string Answer;
    	Answer = "A. Blue.";
    
    	if (char1==Answer)
    	{
    		cout <<"Correct!" <<endl;
    	}
    	system("PAUSE");
    	return 0;
    }
    This isn't all of the coding for the program that I want, I know, but I just want to get this out of the way first.
    Last edited by RazorBlade; 12-13-2007 at 12:46 PM. Reason: I forgot something.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    Erm...
    So you read into an int??? How is an int a string???
    A char is not a string; it's a character. Suggest you read a string tutorial!
    http://www.cprogramming.com/tutorial/lesson9.html
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    Ohh, oops. I forgot to change that back from when I was messing around. I know the difference between a string, char, and int. But since you mentioned a string tutorial, would you know of any good ones/links?

    Code:
    //Study - program that allows User input/output
    //for studying.  Program asks User to state
    //what user thinks is answer for problem at hand.
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
    	string char1;
    	cout <<"The Boy's Ball is: ";
    	cout <<"A. Blue.\n";
    	cout <<"B. Red.\n";
        cin >> char1;
    
    	string Answer;
    	Answer = "A. Blue.";
    
    	if (char1==Answer)
    	{
    		cout <<"Correct!" <<endl;
    	}
    	system("PAUSE");
    	return 0;
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    Re-read the reply for a link!
    But your code is correct. Though you can compare like if (char == "A. Blue") too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    Thanks for the link. I am reading it right now. So you say my code is correct? 'Cause, even with your link I am having a hard time understanding it.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    What part do you not understand?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    I don't understand how I store the right answer in the code so the program has the right answer to compare to the User's answer.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    Code:
    	if (char1 == "Your answer here")
    	{
    		cout <<"Correct!" <<endl;
    	}
    Basically, since you compare a string to another string. Simple. You did it already, only you assigned the string to a string class and then compared it to the answer the user entered.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    First of all, I just want to thank you for having being patient with me.

    But I keep getting a "Permission Denied" message whenever I try to compile it. Here is my code so far:

    Code:
    //Study - program that allows User input/output
    //for studying.  Program asks User to state
    //what user thinks is answer for problem at hand.
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
    	string char1;
    	cout <<"The Boy's Ball is:\n";
    	cout <<"A. Blue.\n";
    	cout <<"B. Red.\n";
        cin >> char1;
        if (char1=="A. Blue.")
    	{
    		cout <<"Correct!" <<endl;
    	}
    	
    	system("PAUSE");
    	return 0;
    }

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    Works fine for me.
    Btw, you might want to use getline, since cin will stop reading at the first encounter of a space.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    So I would replace "cin" with "getline"? If so, I already tried that, and it still didn't work. I hope there isn't something I am overlooking, because I am pretty sure I did a pretty good job of checking your suggestions.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    It's your computer and/or compiler. The code is 100&#37; fine (as in, should compile without errors).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yes, it should compile, but it just probably won't do what you want it to do.

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

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 11:13 AM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 10:20 AM
  4. comparing strings
    By infinitum in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2003, 01:10 PM
  5. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 11:41 PM

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