cin problem

Hello everyone. I am creating a simple program that creates a file, outputs the contents and then manipulates the data. All very basic. My problem is that for some reason the function "int getUserChoice()" does not read the second time it is called from the loop. It does not allow any input ancd program stops. Strangely, the first time the funcion is invoked, the cin >> choice statement works perfectly the second, it does not allow any input fro the console.

Please help!

#include <iostream>
#include <fstream>


using namespace std;


int getUserChoice()
{
int choice;

cout << "Enter 1 for plain items " << endl;
cout << "Enter 2 for helpful items " << endl;
cout << "Enter 3 for harmful items " << endl;
cout << "Enter 4 to quit program " << endl;

cin >> choice;
return choice;
}

void displayItems(int x)
{
// reading file using ifstream

ifstream object("objectsfile2.txt");
string name;
double power;

if(object.is_open())
{
if(x == 1)
{
while(object >> name >> power)
{
if (power == 0)
cout << name << endl;

}
}



if(x == 2)
{
while(object >> name >> power)
{
if (power > 0)
cout << name << endl;

}
}

if(x == 3)
{

while(object >> name >> power)
{

if (power < 0)
{
cout << name << endl;
}

}

}
}
}

int main()
{




int userChoice;







userChoice = getUserChoice();

ofstream characters("objectsfile2.txt");
cout << "Enter item, power item" << endl;
cout << "Press control+z to end entering" << endl;

string item;
double power = 0.0;
if (characters.is_open())
{
while (cin >> item >> power)
{
characters << item << " " << power << endl;

}

ifstream characters("objectsfile2.txt");
if (characters.is_open())
while(characters >> item >> power)
{
cout << "object is: " << item << " power is: " << power << endl;
}

characters.close();
}


// while (userChoice != 4)
//{
switch (userChoice)
{
case 1:

displayItems(1);

break;

case 2:
displayItems(2);

break;

case 3:
displayItems(3);

break;

case 4:
break;

}
userChoice = getUserChoice();

// }

return 0;
}

Topic archived. No new replies allowed.