-
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.
-
-
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.
-
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
-
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;
}
-
Re-read the reply for a link!
But your code is correct. Though you can compare like if (char == "A. Blue") too.
-
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.
-
What part do you not understand?
-
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.
-
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.
-
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;
}
-
Works fine for me.
Btw, you might want to use getline, since cin will stop reading at the first encounter of a space.
-
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.
-
It's your computer and/or compiler. The code is 100% fine (as in, should compile without errors).
-
Yes, it should compile, but it just probably won't do what you want it to do.