943,865 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 868
  • C++ RSS
Mar 11th, 2009
0

problems with type q to quit

Expand 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:

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hansel13 is offline Offline
17 posts
since Mar 2009
Mar 11th, 2009
0

Re: problems with type q to quit

Use && instead of || in if condition -

(*vendorName != 'q' && *vendorName != 'Q')
Reputation Points: 11
Solved Threads: 3
Newbie Poster
vivekc++ is offline Offline
12 posts
since Feb 2009
Mar 11th, 2009
0

Re: problems with type q to quit

Click to Expand / Collapse  Quote originally posted by hansel13 ...
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:

C++ Syntax (Toggle Plain Text)
  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
cpp Syntax (Toggle Plain Text)
  1. *vendorName != 'q' || *vendorName != 'Q'
to
cpp Syntax (Toggle Plain Text)
  1. vendorName[0] != 'q' && vendorName[0] != 'Q'
Last edited by ivailosp; Mar 11th, 2009 at 5:42 am.
Reputation Points: 21
Solved Threads: 22
Junior Poster
ivailosp is offline Offline
129 posts
since Apr 2008
Mar 11th, 2009
0

Re: problems with type q to quit

Click to Expand / Collapse  Quote originally posted by ivailosp ...
change
cpp Syntax (Toggle Plain Text)
  1. *vendorName != 'q' || *vendorName != 'Q'
to
cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hansel13 is offline Offline
17 posts
since Mar 2009
Mar 11th, 2009
0

Re: problems with type q to quit

use a string comparison function rather than a pointer to the first letter in the string. Use strcmp().

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Mar 11th, 2009
0

Re: problems with type q to quit

Click to Expand / Collapse  Quote originally posted by hansel13 ...
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:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Mar 11th, 2009
0

Re: problems with type q to quit

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..

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


C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hansel13 is offline Offline
17 posts
since Mar 2009
Mar 12th, 2009
0

Re: problems with type q to quit

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hansel13 is offline Offline
17 posts
since Mar 2009
Mar 12th, 2009
0

Re: problems with type q to quit

Got it to work, thanks to everyone who helped.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hansel13 is offline Offline
17 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: problem in text files
Next Thread in C++ Forum Timeline: Visual Studio 2005 acting weird





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC