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:

bool getVendor(data & adata) 
{
    char vendorName[MAX_LEN];
    char phoneNum[MAX_LEN];
    char productType[MAX_LEN];
    int eventNum;
    char events[MAX_LEN];

	cout << "\nPlease enter information about the winery: " << endl;
    getString("\tVendor name(type q to quit): ", vendorName);
	
    if(*vendorName != 'q' || *vendorName != 'Q') 
	{
		getString("\tPhone number: ", phoneNum);
		getString("\tType of product: ", productType);
		eventNum = getInt("\tThe amount of events: ");
		getString("\tName(s) of events: ", events);

		adata.setVendorName(vendorName);
		adata.setPhoneNum(phoneNum);
		adata.setProductType(productType);
		adata.setEventNum(eventNum);
		adata.setEvents(events);
		return true;
	}

	return false;
}

Recommended Answers

All 8 Replies

Use && instead of || in if condition -

(*vendorName != 'q' && *vendorName != 'Q')

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:

bool getVendor(data & adata) 
{
    char vendorName[MAX_LEN];
    char phoneNum[MAX_LEN];
    char productType[MAX_LEN];
    int eventNum;
    char events[MAX_LEN];

	cout << "\nPlease enter information about the winery: " << endl;
    getString("\tVendor name(type q to quit): ", vendorName);
	
    if(*vendorName != 'q' || *vendorName != 'Q') 
	{
		getString("\tPhone number: ", phoneNum);
		getString("\tType of product: ", productType);
		eventNum = getInt("\tThe amount of events: ");
		getString("\tName(s) of events: ", events);

		adata.setVendorName(vendorName);
		adata.setPhoneNum(phoneNum);
		adata.setProductType(productType);
		adata.setEventNum(eventNum);
		adata.setEvents(events);
		return true;
	}

	return false;
}

change

*vendorName != 'q' || *vendorName != 'Q'

to

vendorName[0] != 'q' && vendorName[0] != 'Q'

change

*vendorName != 'q' || *vendorName != 'Q'

to

vendorName[0] != 'q' && vendorName[0] != 'Q'

Thanks, that worked!

But that also brought about another problem. :confused:

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.

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

Chris

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:

#include <iostream>

using namespace std;

int main(void)
{
    char input[51];
    cout << "Type something: ";
    cin.getline(input, 50);
    cout << endl;

    if(!strcmp(input, "q") || !strcmp(input, "Q"))
    {
        cout << "A \"q/Q\" was detected !!!" << endl;
    } else {
        cout << "You typed: " << input << endl;
    }

    cin.get();
    return 0;
}

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

bool getVendor(data & adata) 
{
    char vendorName[MAX_LEN];
    char phoneNum[MAX_LEN];
    char productType[MAX_LEN];
    int eventNum;
    char events[MAX_LEN];

	cout << "\nPlease enter information about the winery: " << endl;
    getString("\tVendor name(type q to quit): ", vendorName);
	
    if(!strcmp(vendorName, "q") || !strcmp(vendorName, "Q")) 
	{
		getString("\tPhone number: ", phoneNum);
		getString("\tType of product: ", productType);
		eventNum = getInt("\tThe amount of events: ");
		getString("\tName(s) of events: ", events);

		adata.setVendorName(vendorName);
		adata.setPhoneNum(phoneNum);
		adata.setProductType(productType);
		adata.setEventNum(eventNum);
		adata.setEvents(events);
		return true;
	}

	return false;
}
void getString(char * prompt, char * input) 
{
    cout << prompt;
    cin.get(input, MAX_LEN, '\n');
    cin.ignore(100, '\n');
}

Anyone? I've still tried different ways to make it work, but no luck so far..

Got it to work, thanks to everyone who helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.