6 Replies - 1010 Views - Last Post: 05 September 2008 - 03:51 AM Rate Topic: -----

#1 zerogee  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 20-August 08

Array not counting re-entered invalid number

Posted 02 September 2008 - 02:35 PM

I have this program which works correctly until you enter an invalid number. After prompting an invalid number it ask for new number. But the problem is that the new number is not being counted in the total.

here is the code;

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

int main ()

{
	const int SIZE = 5;
	int PeopleTypes[SIZE];
		int i = 0, Infant = 0, Children = 0, Teenagers = 0, Adults = 0;	
		
		for (i;i<SIZE;i++) 

		{
	  cout << "Enter an age type: (1=Infants,2=Children,3=Teenagers,4=Adults)";
		cin >> PeopleTypes[i];
	  
	  if(PeopleTypes[i] == 1)
		  Infant++;
	  else if(PeopleTypes[i] == 2)
		  Children++;
	  else if(PeopleTypes[i] == 3)
		  Teenagers++;
	  else if(PeopleTypes[i] == 4)
		  Adults++;
	  else
		  {
		  cout << "You have put an Invalid Entry in, please check the type\n" 
			   << " of person and reenter your answer: ";
		  cin >> PeopleTypes[i];
		  }
		  
		}
			
	cout << "\nAt the school function the attendence looked like this: " << endl;
		cout << Infant << " Infants" << endl;
			cout << Children << " Children" << endl;
				cout << Teenagers << " Teenagers" << endl;
					cout << Adults << " Adults" << endl;

		cout << endl;
		return 0;
}


This post has been edited by zerogee: 02 September 2008 - 02:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Array not counting re-entered invalid number

#2 penguin2  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 90
  • Joined: 22-July 08

Re: Array not counting re-entered invalid number

Posted 02 September 2008 - 04:35 PM

If you do it that way, and you make it count the new valid number, so everything works, you will still have a problem if someone enters another invalid number. The easiest way to fix this would be to print a message like "That was not valid input. Valid input would be......" and then deduct 1 from the counter variable (i).
Was This Post Helpful? 0
  • +
  • -

#3 zerogee  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 20-August 08

Re: Array not counting re-entered invalid number

Posted 02 September 2008 - 05:20 PM

View Postpenguin2, on 2 Sep, 2008 - 04:35 PM, said:

If you do it that way, and you make it count the new valid number, so everything works, you will still have a problem if someone enters another invalid number. The easiest way to fix this would be to print a message like "That was not valid input. Valid input would be......" and then deduct 1 from the counter variable (i).


Ok, I not sure how to do that. i tried this but it still does not count the new number. this was the code i tried;

cout << "You have put an Invalid Entry in, please check the type\n" 
			   << " of person and reenter your answer: ";
		  cin >> PeopleTypes[i];



View Postzerogee, on 2 Sep, 2008 - 05:19 PM, said:

View Postpenguin2, on 2 Sep, 2008 - 04:35 PM, said:

If you do it that way, and you make it count the new valid number, so everything works, you will still have a problem if someone enters another invalid number. The easiest way to fix this would be to print a message like "That was not valid input. Valid input would be......" and then deduct 1 from the counter variable (i).


Ok, I not sure how to do that. i tried this but it still does not count the new number. this was the code i tried;

cout << "You have put an Invalid Entry in, please check the type\n" 
			   << " of person and reenter your answer: ";
		  cin >> PeopleTypes[i];



I meant this code;

cout << "You have put an Invalid Entry in, please check the type\n" 
			   << " of person and reenter your answer: ";
		  cin >> PeopleTypes[i-1];

Was This Post Helpful? 0
  • +
  • -

#4 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Array not counting re-entered invalid number

Posted 02 September 2008 - 05:26 PM

I think this is what penguin means. It works fine for me anyway. Just make the following small changes here:

      else
          {
          cout << "You have put an Invalid Entry in, please check the type\n" 
               << " of person and reenter your answer: \n";
          //cin >> PeopleTypes[i]; //you can let them retry when the loop iterates again instead of here
          i--; //decrement the counter so it will not count the invalid entry as an entry, but will keep going for the 
               //required number of iterations
          }
          


This post has been edited by OliveOyl3471: 02 September 2008 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#5 zerogee  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 20-August 08

Re: Array not counting re-entered invalid number

Posted 02 September 2008 - 05:42 PM

View PostOliveOyl3471, on 2 Sep, 2008 - 05:26 PM, said:

I think this is what penguin means. It works fine for me anyway. Just make the following small changes here:

      else
          {
          cout << "You have put an Invalid Entry in, please check the type\n" 
               << " of person and reenter your answer: \n";
          //cin >> PeopleTypes[i]; //you can let them retry when the loop iterates again instead of here
          i--; //decrement the counter so it will not count the invalid entry as an entry, but will keep going for the 
               //required number of iterations
          }
          



Thanks, that worked great.
Was This Post Helpful? 0
  • +
  • -

#6 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Array not counting re-entered invalid number

Posted 02 September 2008 - 08:30 PM

You're welcome, but I don't think you are done yet.
In order to comply with this:
Any other integer value should not be accepted as valid input, and data entry should stop when a negative value has been entered.
I'd add a while or do while loop.
pseudocode:
while input >=0
//do stuff

btw I have seen at least four threads here with this same program. You should try to keep your questions in the same thread if they involve the same program. It makes it easier for others to help you, easier for you to find the answer, and it keeps the board from getting cluttered. Also when someone else has the same problem and does a search, they won't get 50 different threads with the same exact problem.
Thanks :)

This post has been edited by OliveOyl3471: 02 September 2008 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#7 RedSonja  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 172
  • Joined: 04-September 08

Re: Array not counting re-entered invalid number

Posted 05 September 2008 - 03:51 AM

The second

cin >> PeopleTypes[i];

is redundant. When you go back to the top of the for loop it reads in another.
So the new one does not get processed.

Try and tidy your tabs up, makes it easier to read.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1