1

So I made a program according to the task. The program is mainly Lithuanian but I phink you'll understand. So what I need is to find the name of a magazine (string pav) and look for people who subscribed it. So I basically enter Pavadinimas1 and it should give me two results cause there's two people who subscribed magazines named Pavadinimas1, but it only gives me one result. I think it's because of the if statement or the for statement.. I don't know. I know the code's a bit messy, but I'm just a begginer.

Here it is :

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
const int CMax=100;

struct Prenumerata{
string zurnalas;
string vardas;
string pavarde;
int men;
double kaina;
};
string pav;
//---------------------------------------------------------
void Duomenys(Prenumerata A[], int &n);
void Isvedimas(Prenumerata A[], int n, double pren[], string pav);
void Kaina(Prenumerata A[], int n, double pren[]);

//----------------------------------------------------------
int main(){
    setlocale(LC_ALL, "Lithuanian");
    Prenumerata A[CMax];
    int n=0;
    double pren[CMax];
    cout << "Áveskite þurnalo pavadinimà: ";
    cin >> pav;
    Duomenys(A, n);
    Kaina(A, n, pren);
    Isvedimas(A, n, pren, pav);

return 0;
}

void Duomenys(Prenumerata A[], int &n){
ifstream duom("U5.txt");
while (!duom.eof()){
    getline(duom, A[n].zurnalas, ',');
    getline(duom, A[n].pavarde, ' ');
    getline(duom, A[n].vardas, ',');
    duom >> A[n].men >> A[n].kaina;
    n++;
}
duom.close();
}
void Isvedimas(Prenumerata A[], int n, double pren[], string pav){
    ofstream ras("Rezultatai.txt");
    ras << "Pradiniai duomenys:" << endl;
    ras << endl;
    ras << setfill ('-') << setw(80) << "-" << endl;
    ras << setfill (' ');
    ras << endl;
    ras << setw(2) << "Pavadinimas" << setw(10) << "Pavardë" << setw(10) << "Vardas" << setw(20) << "Mënesiø skaièius" << setw(20) << "Mënesio kaina" << endl;
    ras << endl;
    ras << setfill ('-') << setw(80) << "-" << endl;
    ras << setfill (' ');
    ras << endl;
    for (int i=0;i<n;i++){
        ras << setw(2) << A[i].zurnalas << setw(10) << A[i].pavarde << setw(10) << A[i].vardas << setw(10) << A[i].men << setw(25) << A[i].kaina;
    }
    ras << endl;
    ras << endl;
    ras << setfill ('-') << setw(80) << "-" << endl;
    ras << setfill (' ');
    ras << endl;
    ras << "Nurodytas þurnalas: " << pav << endl;
    ras << endl;
    ras << setfill ('-') << setw(40) << "-" << endl;
    ras << setfill (' ');
    ras << setw(9) << "Pavardë" << setw(9) << "Vardas" << setw(22) << "Prenumeratos kaina" << endl;
    ras << setfill ('-') << setw(40) << "-" << endl;
    ras << setfill (' ');
    ras << endl;
    for (int i=0;i<n;i++){
        if (A[i].zurnalas==pav){
        ras << setw(9) << A[i].pavarde << setw(9) << A[i].vardas << setw(22) << pren[i] << endl;
        }
    }
    ras << endl;
    ras << setfill ('-') << setw(40) << "-" << endl;
    ras << setfill (' ');
    ras.close();
    }

    void Kaina(Prenumerata A[], int n, double pren[]){
    for (int i=0;i<n;i++){
            pren[i]=A[i].kaina*A[i].men;
    }
    }

The input file:

Pavadinimas1,Pavarde1 Vardas1,  3    2.99
Pavadinimas2,Pavarde2 Vardas2,  6    3.99
Pavadinimas3,Pavarde3 Vardas3,  8   12.99
Pavadinimas1,Pavarde3 Vardas3,  4    2.99
EJay
  • 1
  • 2
  • I would check (via debugging or printing) the value of `n` before getting into the loop – SJuan76 Jun 27 '15 at 16:06
  • 1
    Fix this first: `while (!duom.eof())`, [it's wrong; read here for why](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). And I highly suspect the remainder of that function is still wrong, as it uses formatted input followed by `getline` on the following iteration without consuming the prior line's newline char. Echoing the data as you're reading it will likely be *very* telling. – WhozCraig Jun 27 '15 at 16:06
  • @SJuan76 I checked the value of 'n' and it's how it is supposed to be I think (4). So what should I use instead of eof? – EJay Jun 27 '15 at 16:14
  • @EJay follow [the link in my comment](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). The answers there have examples of how to do it properly. – WhozCraig Jun 27 '15 at 16:23
  • @WhozCraig I checked the link, but didn't understand much since I dont understand much about programming in English. And most of these examples are without string, so I don't really know how to use it properly either. – EJay Jun 27 '15 at 16:29
  • @EJay It would look [something like this](http://pastebin.com/rSthzc6V). That assuming each line in your input file has one subscription *per line*. As we don't know the input format since it isn't included in the question, that is a best-guess, but a likely one. – WhozCraig Jun 27 '15 at 16:40
  • I included the input format if it helps. – EJay Jun 27 '15 at 16:42
  • @EJay It does indeed. The function I linked is what you're likely looking for to read that data. I'm not saying that's the silver bullet to solve your problem, but it was definitely broken prior, and *reading* the descriptions in the answers of the question I linked will explain why. – WhozCraig Jun 27 '15 at 16:44
  • So I tried your code and for some reason I got an error that numeric_limits was not declared – EJay Jun 27 '15 at 16:51
  • Okay so I forgot to include . Now it works, so I guess the problem really was with my input. Thank you very much. – EJay Jun 27 '15 at 16:59

0 Answers0