menu loop problem

Hello all, I am new to c++ as you may have guessed. I am working on creating a program that has an input menu.My problem occurs when ever I call the addToArray function. The addToArray function is supposed to accept data and place it into an array. When ever I called it before I put it into the menu format it worked. When it is in this menu format it outputs "Enter Name (control -Z to stop): " twice and then when I end the loop with control -Z it goes into an infinite loop repeating the menu options over and over. It skips over the cin command and goes on to the the if statements. I found this out by doing a system("pause"). I have looked through some forums and someone said to apply cin.clear() after the cin. For this code, cin.clear() does not fix the problem. Any advice?

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
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    int size=0;
    int choice;
    string array[1000];

    do{
        cout<<"======================="<<endl;
        cout<<"1) Load\n2) Add\n3) Search\n4) List\n5) Save\n\n0) Exit\n";
        cout<<"======================="<<endl<<endl<<": ";
        cin>>choice;

        if (choice==2){
            while(!cin.eof()){
                addToArray(array, size);
            }

        }

    }while(choice!=0);
    system("PAUSE");
    return 0;
}

void addToArray(string a[], int &s)
{
    cout<<"Enter Name (control -Z to stop): ";
    getline(cin, a[s]);

    if(!cin.eof()){
        s++;
        putInOrder(a, s-1);
    }
}

void putInOrder(string a[], int pos) //insertion sort
{
    int spotInArray = pos-1;
    string itemToInsert = a[pos];

    while (spotInArray >= 0){
        if (a[spotInArray] <= itemToInsert){
            a[spotInArray + 1] = itemToInsert;
            return;
        }

        a[spotInArray + 1] = a[spotInArray];
        spotInArray --;
    }
    a[0] = itemToInsert;
}
What system are you running this on? Windows? Mac? Linux?

A control-Z is the End-Of-File character on Windows.
A control-D is the End-Of-File character on Linux and Mac.

Try a control-D.
I am running this code on windows 7; control-Z does end the .eof loop but the do-while loop (the part that displays the menu) repeates over and over again. I am running this on codebocks with mingw as my compiler, do you think its a compiler issue maybe?
Last edited on
Probably not, I get the same action on Linux.

Is your professor insisting that you use this: if(!cin.eof())? I notice that you do this in two different places, but that removes the EOF char from the stream, so in you main, !cin.eof() will be true, and call the function again.

You might want to try cin.peek(), that does not remove the character from the stream.

Hey thanks for the help, it at first did not seem to fix my problem. I messed around with it putting cin.peek() in various places to see if i could get it to work however i couldn't. I then remembered trying to use cin.clear() so i thought what the heck lets throw in a cin.clear() in at the begining to see what that does and it worked! i still have an issuse of adding a blank line to the array in the begininng but thats a different problem thanks and here i sthe final code:
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
int main()
{
    int size=0;
    int choice;
    string array[1000];
    char c;


    do{
        cin.clear();
        cout<<"======================="<<endl;
        cout<<"1) Load\n2) Add\n3) Search\n4) List\n5) Save\n\n0) Exit\n";
        cout<<"======================="<<endl<<endl<<": ";
        cin>>choice;

        if (choice==2){
            while(!cin.eof()){
                addToArray(array, size);
            }

    }while(choice!=0);
    system("PAUSE");
    return 0;
}

void addToArray(string a[], int &s)
{
    cout<<"Enter Name (control -Z to stop): ";
    getline(cin, a[s]);

    if(cin.peek()!='^Z'){
        s++;
        putInOrder(a, s-1);
    }
}
Last edited on
Topic archived. No new replies allowed.