problems with type q to quit

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 12
Reputation: hansel13 is an unknown quantity at this point 
Solved Threads: 0
hansel13 hansel13 is offline Offline
Newbie Poster

problems with type q to quit

 
0
  #1
Mar 11th, 2009
This subroutine is very basic, however I am struggling.

When the user types q (or Q) into the input when asked for the vendor name, the program should return false... However, this function is always returning true.

MY if statement is clearly not working:
(*vendorName != 'q' || *vendorName != 'Q')

Anyone know why? Thanks.. Here's the code:

  1. bool getVendor(data & adata)
  2. {
  3. char vendorName[MAX_LEN];
  4. char phoneNum[MAX_LEN];
  5. char productType[MAX_LEN];
  6. int eventNum;
  7. char events[MAX_LEN];
  8.  
  9. cout << "\nPlease enter information about the winery: " << endl;
  10. getString("\tVendor name(type q to quit): ", vendorName);
  11.  
  12. if(*vendorName != 'q' || *vendorName != 'Q')
  13. {
  14. getString("\tPhone number: ", phoneNum);
  15. getString("\tType of product: ", productType);
  16. eventNum = getInt("\tThe amount of events: ");
  17. getString("\tName(s) of events: ", events);
  18.  
  19. adata.setVendorName(vendorName);
  20. adata.setPhoneNum(phoneNum);
  21. adata.setProductType(productType);
  22. adata.setEventNum(eventNum);
  23. adata.setEvents(events);
  24. return true;
  25. }
  26.  
  27. return false;
  28. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 12
Reputation: vivekc++ is an unknown quantity at this point 
Solved Threads: 3
vivekc++ vivekc++ is offline Offline
Newbie Poster

Re: problems with type q to quit

 
0
  #2
Mar 11th, 2009
Use && instead of || in if condition -

(*vendorName != 'q' && *vendorName != 'Q')
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: problems with type q to quit

 
0
  #3
Mar 11th, 2009
Originally Posted by hansel13 View Post
This subroutine is very basic, however I am struggling.

When the user types q (or Q) into the input when asked for the vendor name, the program should return false... However, this function is always returning true.

MY if statement is clearly not working:
(*vendorName != 'q' || *vendorName != 'Q')

Anyone know why? Thanks.. Here's the code:

  1. bool getVendor(data & adata)
  2. {
  3. char vendorName[MAX_LEN];
  4. char phoneNum[MAX_LEN];
  5. char productType[MAX_LEN];
  6. int eventNum;
  7. char events[MAX_LEN];
  8.  
  9. cout << "\nPlease enter information about the winery: " << endl;
  10. getString("\tVendor name(type q to quit): ", vendorName);
  11.  
  12. if(*vendorName != 'q' || *vendorName != 'Q')
  13. {
  14. getString("\tPhone number: ", phoneNum);
  15. getString("\tType of product: ", productType);
  16. eventNum = getInt("\tThe amount of events: ");
  17. getString("\tName(s) of events: ", events);
  18.  
  19. adata.setVendorName(vendorName);
  20. adata.setPhoneNum(phoneNum);
  21. adata.setProductType(productType);
  22. adata.setEventNum(eventNum);
  23. adata.setEvents(events);
  24. return true;
  25. }
  26.  
  27. return false;
  28. }
change
  1. *vendorName != 'q' || *vendorName != 'Q'
to
  1. vendorName[0] != 'q' && vendorName[0] != 'Q'
Last edited by ivailosp; Mar 11th, 2009 at 5:42 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 12
Reputation: hansel13 is an unknown quantity at this point 
Solved Threads: 0
hansel13 hansel13 is offline Offline
Newbie Poster

Re: problems with type q to quit

 
0
  #4
Mar 11th, 2009
Originally Posted by ivailosp View Post
change
  1. *vendorName != 'q' || *vendorName != 'Q'
to
  1. vendorName[0] != 'q' && vendorName[0] != 'Q'
Thanks, that worked!

But that also brought about another problem.

Now if I type in any word that begins with q or Q, it returns false.

Is there away to avoid this? Because I only want the letters q and Q to return false. Not words that begin with q or Q.
Last edited by hansel13; Mar 11th, 2009 at 5:47 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: problems with type q to quit

 
0
  #5
Mar 11th, 2009
use a string comparison function rather than a pointer to the first letter in the string. Use strcmp().

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: problems with type q to quit

 
0
  #6
Mar 11th, 2009
Originally Posted by hansel13 View Post
Now if I type in any word that begins with q or Q, it returns false.

Is there away to avoid this? Because I only want the letters q and Q to return false. Not words that begin with q or Q.
To avoid this problem you should just compare the two strings instead of comparing the first characters ...

Try something like this:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. char input[51];
  8. cout << "Type something: ";
  9. cin.getline(input, 50);
  10. cout << endl;
  11.  
  12. if(!strcmp(input, "q") || !strcmp(input, "Q"))
  13. {
  14. cout << "A \"q/Q\" was detected !!!" << endl;
  15. } else {
  16. cout << "You typed: " << input << endl;
  17. }
  18.  
  19. cin.get();
  20. return 0;
  21. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 12
Reputation: hansel13 is an unknown quantity at this point 
Solved Threads: 0
hansel13 hansel13 is offline Offline
Newbie Poster

Re: problems with type q to quit

 
0
  #7
Mar 11th, 2009
I used the strcmp() function, but now the subroutine never runs true. The if statement doesn't seem to be effective. I tried doing the following:
if(!strcmp(*vendorName, "q") || !strcmp(*vendorName, "Q"))
if(!strcmp(vendorName[0], "q") || !strcmp(vendorName[0], "Q"))
but then I got syntax errors.

Not sure what's wrong with time..

  1. bool getVendor(data & adata)
  2. {
  3. char vendorName[MAX_LEN];
  4. char phoneNum[MAX_LEN];
  5. char productType[MAX_LEN];
  6. int eventNum;
  7. char events[MAX_LEN];
  8.  
  9. cout << "\nPlease enter information about the winery: " << endl;
  10. getString("\tVendor name(type q to quit): ", vendorName);
  11.  
  12. if(!strcmp(vendorName, "q") || !strcmp(vendorName, "Q"))
  13. {
  14. getString("\tPhone number: ", phoneNum);
  15. getString("\tType of product: ", productType);
  16. eventNum = getInt("\tThe amount of events: ");
  17. getString("\tName(s) of events: ", events);
  18.  
  19. adata.setVendorName(vendorName);
  20. adata.setPhoneNum(phoneNum);
  21. adata.setProductType(productType);
  22. adata.setEventNum(eventNum);
  23. adata.setEvents(events);
  24. return true;
  25. }
  26.  
  27. return false;
  28. }


  1. void getString(char * prompt, char * input)
  2. {
  3. cout << prompt;
  4. cin.get(input, MAX_LEN, '\n');
  5. cin.ignore(100, '\n');
  6. }
Last edited by hansel13; Mar 11th, 2009 at 7:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 12
Reputation: hansel13 is an unknown quantity at this point 
Solved Threads: 0
hansel13 hansel13 is offline Offline
Newbie Poster

Re: problems with type q to quit

 
0
  #8
Mar 12th, 2009
Anyone? I've still tried different ways to make it work, but no luck so far..
Last edited by hansel13; Mar 12th, 2009 at 3:09 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 12
Reputation: hansel13 is an unknown quantity at this point 
Solved Threads: 0
hansel13 hansel13 is offline Offline
Newbie Poster

Re: problems with type q to quit

 
0
  #9
Mar 12th, 2009
Got it to work, thanks to everyone who helped.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC