1

I want my C++ program to read all the inputs I've written in a .txt file and show me the output i want for each input. i basically want the program to run on each of my inputs, instead, it's only reading my first input. i want it to loop for each set of info i provide. Here is what i have so far:

#include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
int main()
{
    ifstream fin;
    ofstream fout;
    fin.open("emp.txt");
    fout.open("output.dat");
    string last, first;
    double salary, increase, nsalary;
    while (fin>>last>>first>>salary>>increase);
    {
    nsalary= salary + ((salary/100.0)*increase);
    fout<<first<<"\t"<<last<<"\t"<<fixed<<showpoint<<setprecision(3)<<salary<<"\t\t"<<nsalary;
    cout<<first<<"\t"<<last<<"\t"<<fixed<<showpoint<<setprecision(3)<<salary<<"\t\t"<<nsalary;
    fin.close();
    fout.close();
}
}

Thank you

Sura Alhaj
  • 11
  • 2
  • 1
    Have you learned about loops (`for` or `while`)? If you have not, do so. Else, what did you try that didn't work? – walnut Oct 23 '19 at 16:59
  • I know what 'for' and 'while' loops are, but they need conditions right? I don't have a condition to write. – Sura Alhaj Oct 23 '19 at 17:09
  • Possible duplicate of [How to read until EOF from cin in C++](https://stackoverflow.com/questions/201992/how-to-read-until-eof-from-cin-in-c) – walnut Oct 23 '19 at 17:10
  • Specifically [this answer](https://stackoverflow.com/a/202120/11941443) of the duplicate has an example using formatted input. In other word, just put your `fin >>` statement into the `while` condition. – walnut Oct 23 '19 at 17:12
  • I tried that, now the program is reading only my last input and giving me the output for that and nothing else. – Sura Alhaj Oct 23 '19 at 17:36
  • Then you did not do it correctly. It would probably be better to ask about that code then. – walnut Oct 23 '19 at 17:37
  • alright, i edited my question and replaced my old code with the new one so you could see it. – Sura Alhaj Oct 23 '19 at 17:49
  • Please also add the contents of the file that you are using, the output you are currently getting and what you expect instead. – walnut Oct 23 '19 at 18:34
  • That being said, you have to obvious errors: 1. There is a `;` after `while(...)` that doesn't belong there, effectively making the following block *not* part of the loop iteration. 2. You are closing the file in the (intended) loop block. I don't think you want to close the file in each iteration... – walnut Oct 23 '19 at 18:36
  • it worked! thank you so much! – Sura Alhaj Oct 24 '19 at 06:18

0 Answers0