Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 30th, 2008, 3:28 PM   #1
ChevyCowboy15
Newbie
 
ChevyCowboy15's Avatar
 
Join Date: Nov 2007
Location: Central USA
Posts: 19
Rep Power: 0 ChevyCowboy15 is on a distinguished road
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??
ChevyCowboy15 is offline   Reply With Quote
Old Apr 30th, 2008, 3:42 PM   #2
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 270
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
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
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is online now   Reply With Quote
Old Apr 30th, 2008, 3:47 PM   #3
ChevyCowboy15
Newbie
 
ChevyCowboy15's Avatar
 
Join Date: Nov 2007
Location: Central USA
Posts: 19
Rep Power: 0 ChevyCowboy15 is on a distinguished road
Re: multiple values for one char

C++ Syntax (Toggle Plain Text)
  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?
ChevyCowboy15 is offline   Reply With Quote
Old Apr 30th, 2008, 4:12 PM   #4
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 270
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: multiple values for one char

not quite, along the right lines though.

I myself would do something like this

C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is online now   Reply With Quote
Old Apr 30th, 2008, 4:19 PM   #5
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 93
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: multiple values for one char

Quote:
Originally Posted by ChevyCowboy15 View Post
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)
c++ Syntax (Toggle Plain Text)
  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. }
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Apr 30th, 2008, 4:22 PM   #6
ChevyCowboy15
Newbie
 
ChevyCowboy15's Avatar
 
Join Date: Nov 2007
Location: Central USA
Posts: 19
Rep Power: 0 ChevyCowboy15 is on a distinguished road
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
c++ Syntax (Toggle Plain Text)
  1. for(int i = 0; i < 2; i++)

Thanks for helping!!
ChevyCowboy15 is offline   Reply With Quote
Old Apr 30th, 2008, 4:29 PM   #7
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 270
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
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
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is online now   Reply With Quote
Old Apr 30th, 2008, 4:33 PM   #8
ChevyCowboy15
Newbie
 
ChevyCowboy15's Avatar
 
Join Date: Nov 2007
Location: Central USA
Posts: 19
Rep Power: 0 ChevyCowboy15 is on a distinguished road
Re: multiple values for one char

Thanks for the help! and explaining it to me
ChevyCowboy15 is offline   Reply With Quote
Old Apr 30th, 2008, 4:34 PM   #9
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 270
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: multiple values for one char

Thats what we are here for

Chris
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is online now   Reply With Quote
Old Apr 30th, 2008, 5:17 PM   #10
ChevyCowboy15
Newbie
 
ChevyCowboy15's Avatar
 
Join Date: Nov 2007
Location: Central USA
Posts: 19
Rep Power: 0 ChevyCowboy15 is on a distinguished road
Re: multiple values for one char

Freaky Chris you said something about the case sensitive is there something to solve that problem?
ChevyCowboy15 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem processing file into a char array csrocker101 C++ 1 May 8th, 2007 11:50 PM
Convert char[14] to char * myName C++ 1 Mar 5th, 2006 8:08 PM
Setting value to char pointer element.... tempest C 11 Oct 5th, 2005 3:22 PM
Char assignment using Octal values aznluvsmc C 11 Aug 18th, 2005 5:31 AM
Pointers to return multiple values Clotters C++ 8 Jul 20th, 2005 9:54 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:54 AM.

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