-2

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;
}
Jake Wright
  • 373
  • 1
  • 8
  • 2
    I've downvoted because you put zero effort in writing readable code; in particular your indentation is awful. – YSC Jan 22 '18 at 16:04
  • 2
    What are your constraints? Is there a reason why you're not using `std::string` instead of a character array and `std::vector` instead of a C-style array? – Justin Randall Jan 22 '18 at 16:04
  • 1
    Are the character fields really written in the file as fixed-size strings? That's pretty unusual for text files. Can you show an example of what the file looks like? – Barmar Jan 22 '18 at 16:05
  • @JustinRandall we use arrays in school. I know a little bit about how to use vectors and strings, but I never use them at school. – Jake Wright Jan 22 '18 at 16:07
  • @Barmar All of our school problema mention fixed-sized strings. – Jake Wright Jan 22 '18 at 16:08
  • 1
    @JakeWright You might have fixed-size strings in the structure, but are they fixed-size in the file? – Barmar Jan 22 '18 at 16:09
  • 1
    Put it in the question, not a comment. – Barmar Jan 22 '18 at 16:09
  • [`using namespace std;` is a bad practice](https://stackoverflow.com/q/1452721/2176813), never use it. – tambre Jan 22 '18 at 16:11
  • @tambre We use it in school. We don't solve very complicated problems in school. This is usually as bad as it gets. We were taught this way and we're just trying to get our problems to print the right answers. I'm in the process of self teaching coding, but it takes time to familiarise and get used to not doing these bad practices, especially because in class we are supposed to apply soo many bad practices. – Jake Wright Jan 22 '18 at 16:18
  • @tambre everybody that contributes to SO knows using namespace std to be bad. But I'm amazed how we still mention it every time. It's useless. The stream of students that are forced by their schol to do it or are too lazy, or don't care will not stop. So, rather, let's help them understand the part they come here to ask about. Eventually they'll get there. – Jeffrey Jan 22 '18 at 16:29
  • @Jeffrey Thanks for the understanding. I've posted this 30 mins ago, everyone rushed to correct me on my indentation, my bad practices that are actually insignificant to my actual problem, and yet here I am, with no clue whatsoever why my problem is not even reading the data well. – Jake Wright Jan 22 '18 at 16:37
  • 1
    Arrays are 0 based, so your for loop should be `for (i=0; i – Justin Randall Jan 22 '18 at 16:50
  • The thing that stands out most in the code is it makes no checks to see if file access succeeded or that the file was opened in the first place. This makes it hard to see what went wrong where. Test with `fin.is_open()` to get things started and place `if (fin)` statements after the reads so you know what worked and what didn't. Code::Blocks has a decent front end for the GDB debugger. This will allow you to step through your program line by line to see what is does and inspect the variables so you can see why it did it. – user4581301 Jan 22 '18 at 16:51
  • Your `pret` and your `nr` are on different lines in your example input file, so presumably you are missing some `fin.get()` calls. – Justin Randall Jan 22 '18 at 16:59

1 Answers1

1

Before you compute the total number of copies (pret * nr) you should make an extra call to fin.get() to deal with what I think is a carriage return. I noticed this after reformatting the code a bit to show the output in terms of a line of comma separated values, or csv.

#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();

  cout << "Read: " << n << endl;

  cout << "Title, Author, Price, Copies" << endl;

  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;
    fin.get();
    v[i].val=v[i].pret*v[i].nr;
    if(v[i].val>maxx) {
      maxx=v[i].val;
      x=i;
    }
  }

  cout << "Output: " << endl;
  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;

}
Read: 3
Title, Author, Price, Copies
Ion,Liviu Rebreanu,100,10
Mara,Ioan Slavici,50,3
Poezii,Mihai Eminescu,60,20
Output:
Ion Liviu Rebreanu 100 10 1000
Mara Ioan Slavici 50 3 150
Poezii Mihai Eminescu 60 20 1200
Poezii
Snohdo
  • 156
  • 5