How do you stop a program skipping inputs?

In this program I want to be able to take user input to make a class. Unfortunately when I run the progrma the computer skips some input.
This is the section of code where I am having a problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
int ncards;
Card **cardarr = new Card*[ncards];
void Create()
{
    string title;
    string number;
    char restriction;
    short int stock;
    short int attr;
    short int lvl;
    short int mtype;
    char tune;
    short int atk;
    short int def;
    string meffect;

    cout<<"How many cards do you want to create? ";
    cin>>ncards;

    // Define a pointer to an array of base class pointers

    char decision; //switch control character

    for(int i=0;i<ncards;i++)
        {
            //Ask for card type
        cout<<"Do you want to creat a (m)onster card, (e)ffect monster card, (t)rap card or (s)pell card?"<<endl;
        cin>>decision;
        for(;decision!='m' && decision!='e' && decision!='t' && decision!= 's';cin>>decision)
            {cout<<"Error: input not recognised. Please reenter choice."<<endl;}
        switch(decision)
        {
            case 'e':
            cout<<"Enter card name: "<<endl;
            getline(cin, title);

            for(;number.length()!=8;){
                cout<<"Enter card number. It must be exaclty eight characters long."<<endl;
                getline(cin, number);
                if(number.length()!=8){cout<<"Error: The number entered is not the correct length."<<endl;}
            }

            do
            {
                cout<<"Set limit: (f)orbidden, (l)imited, (s)emi-limited or (u)nlimited."<<endl;
                cin>>restriction;
                if(restriction!='f'&& restriction!='l'&& restriction!='s'&& restriction!='u')
                {cout<<"Error: Limitation not recognised"<<endl;}
            }while(restriction!='f'&& restriction!='l'&& restriction!='s'&& restriction!='u');

            cout<<"Enter the stock amount."<<endl;
            cin>>stock;

            cout<<"Set attribute: 1: Dark, 2: Divine, 3: Earth 4: Fire, ";
            cout<<"5: Light, 6: Water, 7: Wind. Any other value will be taken as 0 (Unknown)"<<endl;
            cin>>attr;
            if(0>attr || attr>7){}
            else{attr=0;}

            cout<<"Set level. A value not between 1 and 12 will be taken as 0 (Unknown)"<<endl;
            cin>>lvl;
            if(0>lvl || lvl>12){}
            else{lvl=0;}

            cout<<"Set monster type: 1: Aqua, 2: Beast, 3: Beast-Warrior, 4: 
Dinosaur, 5: Divine-Beast, 6: Dragon 7: Fairy, ";
            cout<<"8: Fiend ,9: Fish, 10: Insect, 11: Machine, 12: Plant, 13: 
Psychic, 14: Pyro, ";
            cout<<"15: Reptile, 16: Rock, 17: Sea Serpent, 18: Spellcaster, 19:
Thunder, 20: Warrior, 21: Winged-Beast, ";
            cout<<"22: Zombie. Any other value will be taken as 0 (Unknown)"<<endl;
            cin>>mtype;
            if(0>mtype || mtype>22){}
            else{mtype=0;}

            cout<<"Is the card a tuner? y/n"<<endl;
            cin>>tune;
            if(tune!='y'){tune='n';}

            cout<<"Enter attack value."<<endl; cin>>atk;
            cout<<"Enter defence value."<<endl; cin>>def;

            cout<<"Type in the monster effect"<<endl;
            getline(cin, meffect);

            cardarr[i] = new Effect_Monster(meffect, attr, lvl, mtype, tune, 
atk, def, title, number, restriction, stock);
            break;
        {


The program compiles fine but the it takes no input on lines 35 and 84. It automatically enters whitespace into the string so when I print the data in the class those fields come out blank:
Name:
attr:<stuff>
lvl:<stuff>
type:<stuff>
tuner:<stuff>
effect:


How can I stop this happening?
This line looks strange... pretty sure you've made a mistake here: (meant to be an "if" statement?)

for(;decision!='m' && decision!='e' && decision!='t' && decision!= 's';cin>>decision)
Last edited on
Solution is simple, every time it skips input, add an additional cin>> command above the problem in question.

Do the following:
Above line 35 insert the following:

cin>>title;


Above line 84 insert the following:

cin>>meffect;

Solved. Does it work?
Last edited on
Yep works wonderfully now thanks.
Obvious solution but I'm so flustered by all the problems I've had with this program I can't think.
Thanks very much Sputnik

To liero:
Its a for loop because I want it to loop until the the user enters a correct value.
No problem.
Although Ill be honest with you and say I don't know why it works by adding a cin>> above, maybe one of the C++ veterens can answer that. The strange thing is, cin>> together with getline() is redundant and asking the same thing twice in a way. It should work with just getline() alone... but hey, if the damn code works, im smiling...
Topic archived. No new replies allowed.