我需要更多的幫助。我已經設法將我的所有字符輸入從文本文件轉換爲數字。 示例: 從文件輸入:將單獨的數字轉換爲一個整數C++
$1,9,56#%34,9
!4.23#$4,983
輸出:
1956
349
423
4983
現在,我需要採取那些個別數字的1 9 5 6並使其讀爲一個整數。輸出看起來是一樣的,但他們實際上是整數。合理?我必須在我的外部循環中執行此操作。它也必須是EOF循環。所以,我知道我需要把第一個數字乘以10並添加下一個數字,然後乘以10,直到達到最後一個數字。我怎樣才能以一種高效率的非崩潰方式來編寫它? input.txt文件的輸入如上所述。 這是我迄今爲止... 任何幫助是極大的讚賞
/*
*/
//Character Processing Algorithm
#include <fstream>
#include <iostream>
#include <cctype>
using namespace std;
char const nwln = '\n';
int main()
{
ifstream data;
ofstream out;
char ch;
char lastch;
int sum;
data.open ("lincoln.txt"); //file for input
if (!data)
{
cout << "Error!!! Failure to Open lincoln.txt" << endl;
system ("pause");
return 1;
}
out.open ("out.txt"); //file for output
if (!out)
{
cout << "Error!!! Failure to Open out.txt" << endl;
system ("pause");
return 1;
}
data.get (ch); // priming read for end-of-file loop
while (data)
{
sum = 0;
while ((ch != nwln) && data)
{
if (isdigit(ch))
out<<ch;
if (ch == '#')
out<<endl;
{
;
}
lastch = ch;
data.get (ch); // update for inner loop
} // inner loop
if (lastch != '#')
out<<endl;
data.get (ch); // update for outer loop
} //outer loop
cout << "The End..." << endl;
data.close(); out.close();
system ("pause");
return 0;
} //main
符號'#'是數字的唯一分隔符? –
是的,'#'是分隔數字的唯一符號。 – byzzyby
然後我的帖子中的代碼示例就是你所需要的。:) –