Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   multiple values for one char (http://www.programmingforums.org/showthread.php?t=15737)

ChevyCowboy15 Apr 30th, 2008 3:28 PM

multiple values for one char
 
i am just messing around with C++ its my first time doing stuff on my own... i am just writing a Q&A quiz... now i ask a question that has multiple answers too.. like for instance if i asked a question "Who was prime minister of england during WWII?" now the answer is Winston Churchill... but say you put in Churchill that is still right... how do you declare multiple values to one decloration??

Freaky Chris Apr 30th, 2008 3:42 PM

Re: multiple values for one char
 
I am right in saying your asking for user input and then checking if it matches x.
You would set an array up, then loop through the array checking to see if the input form the user is equal to any of them.

Chris

ChevyCowboy15 Apr 30th, 2008 3:47 PM

Re: multiple values for one char
 
:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7.  
  8.   char correct1 [20] = {'W','i','n','s','t','o','n', ' ','C','h','u','r','c','h','i','l','l','\0'};
  9.   char correct2 [10] = {'C','h','u','r','c','h','i','l','l','\0'}
  10.   char input1 [30];
  11.  
  12.   cout << "Who was the Prime Minister of England during WWII?\n";
  13.   cin >> input1;
  14.  
  15.   return 0;
  16.  
  17. }


is that right?
what would come next?
or is there a shorter way of doing this?

Freaky Chris Apr 30th, 2008 4:12 PM

Re: multiple values for one char
 
not quite, along the right lines though.

I myself would do something like this

:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main ()
  5. {
  6.   string correct1 [2] = {"Winston Churchill", "Chruchill"};
  7.   string input1;
  8.   cout << "Who was the Prime Minister of England during WWII?\n";
  9.   getline(cin, input1);
  10.   return 0;
  11. }


I understand there may be a few new concepts in there but strings are good to look into for something like this. getline(), may also be new to you. Don't worry have a look at a reference and you should be fine with it.

Once you have got the user's input you will want to look at checking to see if the input matches either of the 2 entries in your array.

:

  1. for(int i = 0; i < 2; i++){
  2.     if(correct1[i] == input1){
  3.           cout << "Congratulations, you got it!";
  4.     }
  5. }


I hope none of that confuses you too much.

one final thing to consider is what happens if i type in "winston churchill" or "Winston cHurchill", are these wrong?

i'm happy to explain anything your unclear about

Chris

peaceofpi Apr 30th, 2008 4:19 PM

Re: multiple values for one char
 
Quote:

Originally Posted by ChevyCowboy15 (Post 144581)
is that right?
what would come next?
or is there a shorter way of doing this?

Something like this should do (obviously you'd have to modify it a bit for whatever you're doing)
:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main() {
  5.     string corrects[2] = { "Winston Churchill", "Churchill" };
  6.     string input;
  7.     getline(cin,input);
  8.     if (input == corrects[0] || input == corrects[1])
  9.         // do stuff
  10.     return 0;
  11. }


ChevyCowboy15 Apr 30th, 2008 4:22 PM

Re: multiple values for one char
 
i just read up on the string and i get that now but the only thing that i dont get is
:

  1. for(int i = 0; i < 2; i++)


Thanks for helping!!

Freaky Chris Apr 30th, 2008 4:29 PM

Re: multiple values for one char
 
peaceofpi posted a solution using just an if() statement that you could use. However the for statement is a simple concept and VERY helpful.

to begin with we state we want a for loop, for().
Then we set a variable up used for counting, int i = 0; note i is normally used just by default. then we specify the condition that must be met, i < 2; (i must be less than 2) if the condition is met then we continue to loop, and finally i++, is how we modify i. Each time the loop iterates through i++ is called. Explained here.

Chris

ChevyCowboy15 Apr 30th, 2008 4:33 PM

Re: multiple values for one char
 
Thanks for the help! and explaining it to me

Freaky Chris Apr 30th, 2008 4:34 PM

Re: multiple values for one char
 
Thats what we are here for ;)

Chris

ChevyCowboy15 Apr 30th, 2008 5:17 PM

Re: multiple values for one char
 
Freaky Chris you said something about the case sensitive is there something to solve that problem?


All times are GMT -5. The time now is 12:59 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC