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