【问题标题】:C++ textfile reading infinite whileloopC++文本文件读取无限while循环
【发布时间】:2017-08-13 11:38:31
【问题描述】:

我正在做一个读取无限while循环的C++程序,

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

文本文件(输入)

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|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


我尝试使用
while(inS >> Day>>Month >> Year >> ch >>hpNO >> ch >> c_Charges >> 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");
}

【问题讨论】:

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


【解决方案1】:

试试

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

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

【讨论】:

  • 终于不是无限循环,但无法读取双精度变量。它只显示'0'。
  • 我不会为你写代码。更新您的帖子,以便我看到您的新代码。
  • 为什么你的文件有'|' ?在我看来,这可能会导致您的问题。
  • 使用 inS.getline(phoneNO, 12, '|') 解决了我的问题。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多