loop

Good night every one ... when i run my program its give me an infinity loop!!
i really give up and i need help ... thnx



#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void NewCustomerData(){
fstream customer;
customer.open("customers.txt", ios::app);
double MobileNumber,MB;
string FiristName;
string LastName,Type;


do{
cout << "Enter Mobile Number as ( 555 ) to STOP: ";
cin >> MobileNumber;
if(MobileNumber!=-1){
cout<<"Please enter your Firist Name: ";
cin >> FiristName;
cout << "Please enter your Firist Name: ";
cin >> LastName;
cout << "Please enter your Pacage Type";
cin >> Type;
cout << "Please enter how many MG you have been consumed:";
cin >> MB;
customer << MobileNumber << '\t' << FiristName <<'\t' << LastName <<'\t' << Type << "\t"<< MB << endl;
customer.close();}
}
while (MobileNumber!=-1);}


int main(){

NewCustomerData();


return 0;
}
It isn't an infinite loop as far as I can tell.
The loop terminates when MobileNumber is -1.
thnx chervil ^_^

but its write to the file for only once i mean when i enter for example : mobile: 77777777
firist name: jey
last name: adam
type: extra
MB: 55
then loping and enter for another customer it write to the file only the data for jey !?

Yes. The statement customer.close(); is inside the body of the if statement.

That statement should be moved to the last line just before the end of function NewCustomerData(). That is, move it completely outside the do-while loop.

Actually, the file close statement can be omitted completely, as the system will automatically close the file when the fstream is destroyed (at the end of the function).
Last edited on
aha really thanks a lot Mr.chervil it's worrrrrrrk now ^_^
but i have only last problem which with mobile number for example when i enter 777777 its wirte to the file as 7.77778e+006 !!! :(
1
2
3
#include <iomanip>

customer << setprecision (15) << MobileNumber .... etc.


There's still a possibility of an infinite loop, if a non-numeric value is input when a number is expected.
http://www.cplusplus.com/forum/beginner/87178/#msg467738
Topic archived. No new replies allowed.