Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,340 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,767 people online right now. Registration is fast and FREE... Join Now!




Array not counting re-entered invalid number

 
Reply to this topicStart new topic

Array not counting re-entered invalid number

zerogee
2 Sep, 2008 - 01:35 PM
Post #1

New D.I.C Head
Group Icon

Joined: 20 Aug, 2008
Posts: 49

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;

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: 2 Sep, 2008 - 01:36 PM
User is offlineProfile CardPM
+Quote Post

penguin2
RE: Array Not Counting Re-entered Invalid Number
2 Sep, 2008 - 03:35 PM
Post #2

D.I.C Head
Group Icon

Joined: 22 Jul, 2008
Posts: 79



Thanked: 1 times
Dream Kudos: 25
My Contributions
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).
User is offlineProfile CardPM
+Quote Post

zerogee
RE: Array Not Counting Re-entered Invalid Number
2 Sep, 2008 - 04:20 PM
Post #3

New D.I.C Head
Group Icon

Joined: 20 Aug, 2008
Posts: 49

QUOTE(penguin2 @ 2 Sep, 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).


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;

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


QUOTE(zerogee @ 2 Sep, 2008 - 05:19 PM) *

QUOTE(penguin2 @ 2 Sep, 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).


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;

CODE
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;

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


User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: Array Not Counting Re-entered Invalid Number
2 Sep, 2008 - 04:26 PM
Post #4

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,624



Thanked: 18 times
Dream Kudos: 150
My Contributions
I think this is what penguin means. It works fine for me anyway. Just make the following small changes here:

cpp

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: 2 Sep, 2008 - 04:28 PM
User is online!Profile CardPM
+Quote Post

zerogee
RE: Array Not Counting Re-entered Invalid Number
2 Sep, 2008 - 04:42 PM
Post #5

New D.I.C Head
Group Icon

Joined: 20 Aug, 2008
Posts: 49

QUOTE(OliveOyl3471 @ 2 Sep, 2008 - 05:26 PM) *

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

cpp

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.
User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: Array Not Counting Re-entered Invalid Number
2 Sep, 2008 - 07:30 PM
Post #6

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,624



Thanked: 18 times
Dream Kudos: 150
My Contributions
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 smile.gif

This post has been edited by OliveOyl3471: 2 Sep, 2008 - 07:32 PM
User is online!Profile CardPM
+Quote Post

RedSonja
RE: Array Not Counting Re-entered Invalid Number
5 Sep, 2008 - 02:51 AM
Post #7

D.I.C Head
Group Icon

Joined: 4 Sep, 2008
Posts: 170



Thanked: 3 times
Dream Kudos: 25
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 08:15AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month