さて、私といくつかの同僚がクラスの課題に取り組んでいました。私はそれを私の最大限に完了しました。しかし、私も私の同僚の誰も修正できないことが1つあります。私は何時間もオンラインで検索しましたが、私が必要としていた答えはうまくいかなかったのです。最初の行が1桁右にシフトされているのはなぜですか?
ファイルからデータを読み込んだ後に最初に印刷される行は、常にスペースが1つ右に移動します。それが私の唯一の問題です。文字列はシフトされませんが、すべてです。空白、タブ、余分な記号がないかどうかを確認して、読んでいたファイルをすべて元に戻してみました。誰かが私のプログラムに間違っていることを指摘できれば、本当に感謝しています。前もって感謝します!
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
ifstream inputFile;
cout << "Please enter the file you would like to open: ";
string filename;
cin >> filename;
inputFile.open(filename);
if(inputFile.fail())
{
cout << "Error: File failed to open or was not found..." << endl;
}
else
{
cout << "File opened" << endl;
string land;
double price, min, max, total, average, count;
count = 0;
min = 0;
max = 0;
total = 0;
while(inputFile.good())
{
getline(inputFile, land, '\t');
inputFile >> price;
cout << fixed << setprecision(2) << setw(40) << left << land << right << "$ " << price;
if(count == 0)
{
max = price;
min = price;
}
if(price > max)
{
max = price;
}
if(price < min)
{
min = price;
}
total += price;
count++;
}
inputFile.close();
average = total/count;
cout << endl << endl;
cout << setw(43) << "Average Price = $ " << average << endl;
cout << setw(43) << "Highest Price = $ " << max << endl;
cout << setw(43) << " Lowest Price = $ " << min << endl;
}
return 0;
}
私はそれがループであることには意味がありません、なぜ最初のものだけがシフトされていますか?
これは私が言葉や数字はすべてのタブで区切る必要があります
Landmark 1258
Creekside 1840
Parkside 1575
Gallatyn Walk 1710
Oak Mill 1185
Cutler's Ridge 1495
Prairie Creek Cottages 1987
Waterview Mills 1505
Canterbury Courts 1300
Breckinridge Point 1205
The Junction 1699
を使用していたファイルにあったものです。
出力は、私のシステムにシフトしていないようです。 – Jarvis
それは多分xcodeもの、端末でそれを試してみてください –
なぜgetline(dlim = '\ t')? –