I have to be able to have the program output invalid input if the number is not 1,2,3,4. When I run this program it automatically puts invalid for everything.

#include "stdafx.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	const int People = 5;
	int i, PeopleTypes[People];

cout << "Enter 1 for Infant, 2 for Child, 3 for Teenager, or 4 for Adult\n";
cout << "for each person that attended the school function.\n\n";

	for(i=0; i<People; i++)
	{
		cout << "Person #" << i + 1 << ": ";
		cin  >> PeopleTypes[i];
	if (PeopleTypes[i] != 1,2,3,4)
	{
	cout << "This is invalid input!";
	}
}

	return 0;
}

To answer your question: no.

>> PeopleTypes != 1,2,3,4
Do you want it to be or, or and?

OR:
PeopleTypes != 1 || PeopleTypes !=2 || PeopleTypes !=3 || PeopleTypes !=4

AND:
PeopleTypes != 1 && PeopleTypes !=2 && PeopleTypes !=3 && PeopleTypes !=4

Aint pretty but you'll get over it.

For some reason no matter what number I type in it says invalid input. What am I doing wrong?

Did you read my post? You could think about tidying it up by checking if it's above

if (PeopleTypes>4 || PeopleTypes<1){...}


What's that say when you replace it?

>What am I doing wrong?
twomers already showed you. 1,2,3,4 isn't a list of values, it's four expressions separated by the comma operator. The result is most certainly not what you expected. Here's what actually happens:

PeopleTypes != 1 is tested. If PeopleTypes is actually 1, the expression is false, but execution still evaluates the next expression in the condition, which is 2. Because C++ has a default condition of <anything> != 0, 2 != 0 results in a true condition. Keep doing this until the last expression (which actually controls the loop execution) and the body of the if statement is executed because it's non-zero. You can test this behavior if you want. Change the condition to this:

if (PeopleTypes[i] != 1,0)

Now anything you type will always fail the condition and skip the body of the if statement.

My directions state that any other interger value should not be accepted as valid input. Now the program shows invalid input when I enter something other than 1 thru 4 but it asks for the next person. Do you think this is correct?

>Do you think this is correct?
No, but it's not my program. Do you think it's correct? If not, then you shouldn't be incrementing i if there's an error like that.

I believe I could just use a break statement and stop the program right?

>I believe I could just use a break statement and stop the program right?
You could, and sometimes that's the best option. But I would recommend trying to recover from the error if you can. In this case it's not that hard to ask for input again rather than bail out of the entire program if someone accidentally types 5 instead of 4. :icon_rolleyes:

The program will stop if any other integer is entered except 1-4. My next question is this. Is there a way to ask the user to reenter the number instead of just ending the program?

#include "stdafx.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	const int People = 5;
	int i, PeopleTypes[People];

cout << "Enter 1 for Infant, 2 for Child, 3 for Teenager, or 4 for Adult\n";
cout << "for each person that attended the school function.\n\n";

	for(i=0; i<People; i++)
	{
		cout << "Person #" << i + 1 << ": ";
		cin  >> PeopleTypes[i];
	if (PeopleTypes[i]>4 || PeopleTypes[i]<1)
	{
	cout << "This is invalid input!\n\n";
	break;
	}
	if (PeopleTypes[i]<0)
		break;
	}
}

>Is there a way to ask the user to reenter the number instead of just ending the program?
How about not breaking, and not incrementing your loop counter on the error, like I just suggested.

OK. I took out the break but how would you stop incrementing just on the error?

The program will stop if any other integer is entered except 1-4. My next question is this. Is there a way to ask the user to reenter the number instead of just ending the program?

Yes. You put the input in a while loop and don't exit the loop until a correct number is entered:

for(i=0; i<People; i++)
    {
        cout << "Person #" << i + 1 << ": ";
        cin  >> PeopleTypes[i];
        while (PeopleTypes[i]>4 || PeopleTypes[i]<1)
        {
            cout << "This is invalid input!\n\n";
            cout << "Person #" << i + 1 << ": ";  // input number again
            cin  >> PeopleTypes[i];
        }
        if (PeopleTypes[i]<0)
        {
            break;
        }
    }

I'll let you work out how to implement the <0 exit...

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.