1 Replies - 618 Views - Last Post: 05 December 2012 - 03:23 PM Rate Topic: -----

#1 Nela12   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 16-October 12

Stuck in infinite loop - cannot pinpoint why

Posted 05 December 2012 - 02:57 PM

fstream file;
	file.open ( "inventory.dat", ios::out | ios :: in | ios::binary );

	if (!file) // Validate that the file is openable
	{
		cout << "Error opening file! Program Terminating..." << endl;
		exit(0);
	}

	do
	{
		cout << "What item number do you need to update? ";
		cin >> i.item_num; 

		file.seekg( (i.item_num-1)*sizeof(i) , ios::beg );
		file.read ( reinterpret_cast<char*>(&i),sizeof(i) );

			cin.ignore();
			cout << "Enter the description: ";
			cin.getline(i.description, 30, '\n');

			cout << "Enter quantity on hand: ";
			cin >> i.qty_on_hand;

			cout << "Enter wholesale price: ";
			cin >> i.wholesale;

			cout << "Enter retail price: ";
			cin >> i.retail;


		//write the record to the binary file
		file.seekp((i.item_num-1)*sizeof(i) , ios::beg );
		file.write(reinterpret_cast<char*>(&i) , sizeof(i));

		cout << "Do you want to update another item? ( Y or N ): ";
		cin >> input;
		input = toupper ( input );
		cout << endl << endl;

	} while ( input == 'Y' );

	file.close();



I've tried numerous things ( adding seekp). It begins to infinite loop right after I enter the description (a string). I am using a break so I have no idea why it is doing this.

Thanks in advance for any advice

Never mind!Got it

Is This A Good Question/Topic? 0
  • +

Replies To: Stuck in infinite loop - cannot pinpoint why

#2 AKMafia001   User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

Re: Stuck in infinite loop - cannot pinpoint why

Posted 05 December 2012 - 03:23 PM

If by entering the description on line 20 causes the problem, would you try to add on line 18 these two lines:
cin.clear();
cin.ignore(10000,'\n');



Just give it a try...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1