I have this problem where I must read data from n
books: title, author, price( the pret
variable), the number of copies (the nr
variable). The variable val
represents the "value" of the book, which is price* number of copies.
We only use arrays in school, not vectors of strings therefore all of our problems have fixed-sized strings. Example of the file we are reading from:
3
Ion
Liviu Rebreanu
100
10
Mara
Ioan Slavici
50
3
Poezii
Mihai Eminescu
60
20
I need to print out the data read for every book and also the "value" of it. And I also need to print the data of the book with the highest value afterwards. I'm working in Code::Blocks 13.12 because it'a school assignment. I have no idea why, but it reads only the data for my first book. So therefore it prints a lot of nonsense after that reading. What's wrong with it?
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
struct carte
{
char t[50], a[50];
int pret, nr, val;
} v[100];
int main()
{
int n, i, maxx=0, x, j;
ifstream fin ("carte.txt");
fin>>n;
fin.get();
for (i=1; i<=n; i++)
{
fin.get(v[i].t, 50); cout<<v[i].t<<" ";
fin.get();
fin.get(v[i].a, 50);
fin.get(); cout<<v[i].a<<" ";
fin>>v[i].pret>>v[i].nr; cout<<v[i].pret<<" "<<v[i].nr<<endl;
v[i].val=v[i].pret*v[i].nr;
if(v[i].val>maxx)
{
maxx=v[i].val;
x=i;
}
}
for(i=1; i<=n; i++)
{
cout<<v[i].t<<" "<<v[i].a<<" "<<v[i].pret;
cout<<" "<<v[i].nr<<" "<<v[i].val<<endl;
}
cout<<v[x].t<<endl;
return 0;
}