While Loops

When I run my program, everything executes as written. But, when it gets to the while-loop statement, my 'cout' statements work, but my 'cin' statements do not. Can anyone tell me what's going wrong?

int main(int argc, char *argv[])
{
char inv_file[20], new_file[20];
char items1[item_size];
char ans = 'y';
string your_inv;
ifstream in_file;
ofstream out_file;

while(ans == 'y' || ans == 'Y')
{
cout << "Hello world! Welcome to Lab 11 Inventory Management System" << endl << endl;
int choice = 0, numofinv = 0, cnt = 0;
cout << "Enter your inventory: ";
cin.get(items1, item_size);
cout << "\n\nType in your file to open, without spaces [Default is 'Inventory']: ";
cin >> inv_file;
cout << "\n\nType in a name for your new file: ";
cin >> new_file;

in_file.open(inv_file, ios::in);
out_file.open(new_file, ios::app);
if(in_file.fail())
{
cout << "File is inaccessible." << endl;
exit(1);
}
choice = menu(cnt);
if(choice == 1)
{
load_inv(in_file, out_file, your_inv, y_index, numofinv);
}else if(choice == 2)
{
add_inv(cin, out_file, items1, item_size, numofinv);
}else if(choice == 3)
{
list_inv(cout, in_file, items1, item_size, numofinv);
}else if(choice == 4)
{
save_inv(in_file, out_file, items1, item_size);
}else
{
out_file.flush();
cin.sync();
out_file.close();
in_file.close();
your_inv.clear();
cout << "\nDo you want to continue? [y or n] ";
cin >> ans;
system("cls");
}
}

system("pause");
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main (){
   int i = 0;
   while( i != -1 ){
      std::cin >> i;
      std::cout << "You entered " << i << " !\n";
   }
   return 0;
}
works.
If you don't see how your code is different from this, post it.
Topic archived. No new replies allowed.