Pregunta

Estoy leyendo un archivo en el siguiente formato

1001    16000     300    12.50
2002    24000     360    10.50
3003    30000     300     9.50

donde los artículos son:. Identificación del préstamo, director, meses, tasa de interés

No estoy seguro de qué es lo que estoy haciendo mal con mi flujo de cadena de entrada, pero no estoy leyendo los valores correctamente porque sólo el ID de préstamo se lee correctamente. Todo lo demás es igual a cero. En este momento se trata de una tarea, pero yo sólo quería saber si podrías ayudarme a identificar mi error.

if( inputstream.is_open() ){

        /** print the results **/
        cout << fixed << showpoint << setprecision(2);
        cout << "ID " << "\tPrincipal" << "\tDuration" << "\tInterest" << "\tPayment" <<"\tTotal Payment" << endl;
        cout << "---------------------------------------------------------------------------------------------" << endl;

        /** assign line read while we haven't reached end of file **/
        string line;
        istringstream instream;
        while( inputstream >> line ){
            instream.clear();
            instream.str(line);

            /** assing values **/
            instream >> loanid >> principal >> duration >> interest;


            /** compute monthly payment **/
            double ratem = interest / 1200.0;
            double expm = (1.0 + ratem);
            payment = (ratem * pow(expm, duration) * principal) / (pow(expm, duration) - 1.0);

            /** computer total payment **/
            totalPayment = payment * duration;

            /** print out calculations **/
            cout << loanid << "\t$" << principal <<"\t" << duration << "mo" << "\t" << interest << "\t$" << payment << "\t$" << totalPayment << endl;

        }
    }
¿Fue útil?

Solución

Usted no está leyendo linewise. Reemplazar la condición por

while( getline(inputstream, line) )

Si utiliza operator>> se va a extraer sólo la primera palabra a line.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top