C ++文本文件读取无限while循环

时间:2017-08-13 11:38:31

标签: c++ text-files infinite-loop eof

我正在做C ++程序,它读取无限循环,

让用户输入所需的月份和年份。 循环变为无限,只输出“AAAAA ....”

TEXTFILE(输入)

  

1 9 2017 | 0112233445 | 20.00 5.00
2 9 2017 | 0123456789 | 6.00 3.00
  3 10 2017 | 0135792468 | 30.00 1.00
4 10 2017 | 0166778899 | 7.00   10.00
4 10 2017 2017 | 0177001228 | 40.00 4.00
5 10 2017 | 0184040626 | 8.00 2.00
6 10 2017 | 0134567892 | 50.00 6.00
7   10 2017 | 0145678910 | 9.00 7.00 7.O

我尝试使用
while(inS>> Day>> Month>> Year>> ch>> hpNO>> ch>> c_Charges>&gt ; sms_charges)
但似乎没有任何工作。
有任何解决这个问题的建议吗?先谢谢。

void monthlyreport(char manager[])
{
	SYSTEMTIME T;
	GetSystemTime(&T);
	int month, year;   //userinput
	char ch;//store delimitor
	int count = 0;
	

	double Totalcallcharges = 0;
	double Totalsmscharges = 0;
	double Totalcharges = 0;

	int  Day,Month, Year;//for textfile reading
	char hpNO[11];
	double c_Charges =0;
	double sms_charges =0;



	string Months[] = { "January","February","March","April","May","June","July","August","September","October","November","December" };

	system("cls");
	cout << "You choose : MONTHLY CHARGES REPORT ...";


	cout << "\n\n Which month/year  (mm yyyy)?     ";
	cin >> month;
	cin >> year;



	ifstream inS;
	inS.open("Transaction.txt");
	/*while (inS >> Day)  
	{
		while (inS >> Month)
		{
			while (inS >> Year)
			{
				while (inS >> ch)
				{
					while (inS >> hpNO)
					{
						while (inS >> ch)
						{
							while (inS >> c_Charges)
							{
								while (inS >> sms_charges)
								{
									
										if (month == Month && year == Year)
										{
											Totalcallcharges += c_Charges;
										}
										if (month == Month && year == Year)
										{

											Totalsmscharges += sms_charges;
										}

										count++;
									
								}
							}
						}
					}
				}
			}
		}
	}
*/
while (!inS.eof()) correct answer
			{
				inS >> tranS.day >> tranS.month >> tranS.year >> ch;
				inS.getline(tranS.phoneNO, 12, '|');
				inS >> tranS.call_charges >> tranS.sms_charges;


				transactions(&head, tranS);
				if (tranS.month == month &&tranS.year == year)
				{
					tranS.Total_call_charges += tranS.call_charges;
					tranS.Total_sms_charges += tranS.sms_charges;
				}
				count++;
			}	
	Totalcharges = Totalcallcharges + Totalsmscharges;

	inS.close();
	system("cls");

	cout << Month << year;
	cout << endl << c_Charges;
	cout << setw(50) << setfill('=') << " " << endl << endl;
	cout << "       T A R U C M O B I L E   S D N   B H D" << endl << endl;
	cout << setfill(' ') << setw(30) << "Monthly Report - " << Months[month - 1] << " " << year << endl;
	cout << "\nTotal Number of TARUCMOBILE users = " << count;
	cout << "\n\nTotal Charges from Calls = RM" << setw(8) << setfill(' ') << Totalcallcharges << setprecision(2);
	cout << "\nTotal Charges from SMS's = RM" << setw(8) << setfill(' ') << Totalsmscharges << setprecision(2);
	cout << "\n" << setfill(' ') << setw(27) << " " << right << setw(12) << setfill('-') << "-" << endl;
	cout << "TOTAL CHARGES (POSTPAID) = RM" << setw(8) << setfill(' ') << Totalcharges << setprecision(2);
	cout << "\n" << setw(27) << setfill(' ') << " " << setw(12) << setfill('=') << "=" << endl;
	cout << "***        E N D     O F    R E P O R T      *** " << endl << endl;

	cout << "Generated by" << endl;
	cout << "______________" << endl;
	cout << manager << "(Manager)" << endl;
	cout << T.wDay << " " << Months[T.wMonth - 1] << " " << T.wYear << endl;
	cout << endl << setw(50) << setfill('=') << " " << endl;

	
	system("pause");
}

1 个答案:

答案 0 :(得分:0)

尝试

    while(inS >> Day)
    {
    while(inS >> Month)
    {
    while(inS >> Year)
    {
    while(inS >>ch)
    {
    .
    .
    .
    }

然后在最里面的while循环中做任何你想做的事。