2016-10-20 1 views
0

Ich erstelle Vorlage Vektor, meine infile txt (courseofsale.txt) zu lesen und es per CSV-Datei (output.csv) auszugeben. Alles ist korrekt, aber das Timing ist falsch im höchsten Preis und niedrigsten Preis.
The highest price timing must be like thisAusgabezeit ist falsch als die Infile selbst

while the lowest price timing must be like this

sondern ich erhielt 16.57 als Timing für die beiden. Hier ist mein Haupt-CPP

// MainTest.cpp 
// 
// Class that runs the main program modules. 


#include <iostream> 
#include <string> 
#include <fstream> 
#include <iomanip> 
#include "StockT.h" 
#include "VectorT.h" 

void highestPrice(); 
void lowestPrice(); 
void outputFile(); 


VectorT<StockT> V1(4000); 
StockT S1; 
DateT d1; 
TimeT t1; 

using namespace std; 

//---------------------------------------------------------------------------- 
//Main Method 

int main() 
{ 
    ifstream inData; 
    inData.open("courseofsales.txt"); 
    string line; 
    string sDay, sMonth, sYear, sHour, sMinute,sSecond,sCondition; 
    double price,value; 
    int volume; 

    //Reading the text file 

    if (inData.is_open()) 
    { 
     inData.ignore(500, '\n'); 
     inData.ignore(500, '\n'); 
     while (getline(inData, line, '\n')) 
     { 
      while (!inData.eof()) 
      { 
       sDay = line.substr(0, 2); 
       sMonth = line.substr(3, 2); 
       sYear = line.substr(6, 4); 
       sHour = line.substr(11, 2); 
       sMinute = line.substr(14, 2); 
       sSecond = line.substr(16, 2); 

       inData.ignore(50, '\t'); 
       inData >> price; 
       inData.ignore(50, '\t'); 
       inData >> volume; 
       inData.ignore(50, '\t'); 
       inData >> value; 
       inData.ignore(50, '\t'); 
       getline(inData, sCondition); 
       S1.setAll(price, volume, value, sDay, sMonth, sYear, sHour, sMinute, sSecond); 
       V1.push_back(S1); 
      } 
     } 
     inData.close(); //closing text file 
    } 

    else 
     cout << "Unable to open file"; 

    int choice; 
    do { 
    cout << "Please enter your choice: " << endl << endl; 
    cout << "1. Retrieve highest share price of the day" << endl; 
    cout << "2. Retrieve lowest share price of the day" << endl; 
    cout << "3. Generate output file" << endl; 
    cout << "4. Exit the program" << endl; 
    cout << "Your choice: "; 
    cin >> choice; 
    cout << endl; 
    if (choice == 1) 
     { 
      cout << endl << "This is the highest bid of the day: " << endl; 
      highestPrice(); //calls the method for retrieving highest price 
      cout << endl; 
     } 

     else if (choice == 2) 
     { 
      cout << endl << "This is the lowest bid of the day: " << endl; 
      lowestPrice(); //calls the method for retrieving lowest price 
      cout << endl; 
     } 

     else if (choice == 3) 
     { 
      cout << endl << "Check your file." << endl; 
      outputFile(); //calls the method to create output.csv 
     } 

     else if (choice == 4) 
      exit(0); //exit the program 

     else 
     { 
      cout << endl << "This is an invalid choice. Enter again. " << endl; 
     } 
    }while(choice != 1 || choice != 2 || choice != 3 || choice != 4); //Looping for user input till the program exits 

    system("PAUSE"); 
    return 0; 
} 

//---------------------------------------------------------------------------- 
//Retrieve the highest share price. 

void highestPrice() 
{ 
    double HP = V1.at(0).getPrice(); 
     for (int i = 0; i < V1.size(); i++) 
     { 
      if (V1.at(i).getPrice() > HP) 
      { 
       HP = V1.at(i).getPrice(); 
      } 
     } 

     for (int j = 0; j < V1.size(); j++) 
     { 
      if (V1.at(j).getPrice() == HP) 
      { 
        cout << "Date and Time of transaction: " << V1.at(j).getDate() << V1.at(j).getTime()<< endl; 
        cout << "Highest price: " << V1.at(j).getPrice() << endl; 
        break; 
      } 
     } 
     cout << endl; 
} 

//---------------------------------------------------------------------------- 
//Retrieve the lowest share price. 

void lowestPrice() 
{ 
    double LP = V1.at(0).getPrice(); 
    for (int i = 0; i < V1.size(); i++) 
    { 
     if ((V1.at(i).getPrice() < LP) && (V1.at(i).getPrice() != 0)) 
     { 
      LP = V1.at(i).getPrice(); 
     } 
    } 

    for (int j = 0; j < V1.size(); j++) 
    { 
     if (V1.at(j).getPrice() == LP) 
     { 
      cout << "Date and Time of transaction: " << V1.at(j).getDate() << V1.at(j).getTime()<< endl; 
      cout << "Lowest price: " << V1.at(j).getPrice() << endl; 
      break; 
     } 
    } 
    cout << endl; 
} 

//---------------------------------------------------------------------------- 
//Creates the output file 

void outputFile() 
{ 
    ofstream outData; 
    outData.open("output.csv"); 
    string line; 
    int c1 = 0; 

    if (outData.is_open()) 
    { 
     for (int i = 0; i < V1.size(); i++) 
     { 
      outData << V1.at(i).getDate() << "," << setw(5); 
      outData << V1.at(i).getTime() << "," << setw(5); 
      outData << V1.at(i).getPrice() << "," << setw(5); 
      outData << V1.at(i).getVolume() << "," << setw(5); 
      outData << V1.at(i).getValue(); 
      outData << '\n'; 
     } 

     outData.close(); 
    } 

    else 
     cout << "Unable to write file"; 
} 
+1

Es klingt wie Sie müssen lernen, wie Sie einen Debugger verwenden, um durch Ihren Code zu gehen. Mit einem guten Debugger können Sie Ihr Programm Zeile für Zeile ausführen und sehen, wo es von dem, was Sie erwarten, abweicht. Dies ist ein essentielles Werkzeug, wenn Sie programmieren wollen. Weiterführende Literatur: ** [Wie kleine Programme zu debuggen] (http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) ** – NathanOliver

+0

'if (V1.at (j) .getPrice() == HP) 'Dies wird generell nicht für HW-FP-Typen wie' double' empfohlen. Obwohl in diesem speziellen Fall Ihre Dateneingabe nur in 2-Dezimalzahlen formatiert ist, ist der tatsächliche doppelte Wert nach dem Import für jedes Element identisch ungenau, so dass der genaue Gleichheitstest funktioniert. Über Ihre Zeitausgabe. Sie haben kein relevantes Datum/Uhrzeit für das Importieren/Exportieren von Code gepostet, oder Beispieldaten, also keine Ahnung, Sie haben wahrscheinlich einen Fehler in Ihrem Code. – Ped7g

+0

@ Ped7g Ich habe Header für das Datum und die Uhrzeit auch cpp für beide. aber es wäre lang und es wäre nicht mcve :(das ist, warum ich es hier nicht aufgenommen habe, aber sollte ich? – Blair

Antwort

0

darüber ein bisschen mehr denken, wenn die 16.57 Zeit des letzten Punktes ist, ist Ihr Copykonstruktor in StockT schlecht, nur flache Kopie erstellen, während Sie es in einem verwenden Weg, der eine tiefe Kopie benötigt (in).

Dies ist mein letzter Kommentar hier, da Sie MCVE nicht bereitgestellt haben, noch sieht es so aus, als ob Sie Ihren Code debuggen oder verstehen könnten. Wiederholen Sie einfach einige grundlegende Programmierkurse und lesen Sie etwas über Datenstrukturen und Speicher, damit Sie besser verstehen, was im Inneren passiert und wie Sie dies mit dem Debugger überprüfen können.