What is wrong with this code??

Apr 15, 2012 at 3:29pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <fstream>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

void raamatu_lugemine(int arv, int arv2);

struct Raamat{
       char Raamat[100];
       char Eesnimi[20], Perekonnanimi[20];
       int Ilmumisaasta, Lehtedearv;
       char Keel[4];
       int Hinne;
       char Loetud[5];
       };

int main(){
    char sisend_rida[100];
    string andmebaas [100];
    struct Raamat Raamat;
    char g[1];
    int x = 0;
    int y = 4;
    ifstream fin("andmebaas.txt");
    // Andmete lugemine jadasse
   // for(int i = 0; i <= 100 && ! fin.eof(); i++){
     //     fin.getline(cin, sisend_rida);
       //   andmebaas[i] = sisend_rida;
         // }
   // for(int i = 0; i <= 100; i++){
     //       sisend_rida = andmebaas[i];
     // Tutvustus
     // 5 esimest raamatut
     raamatu_lugemine(x, y);
     while(true){
     cout << "j = 5 j'rgmist kirjet, e = 5 eelmist kirjet, v = v'ljub"<< endl;
     cin >> g;
     if(strcmp(g,"j")){
         x = x+5;
         y = y+5;
         raamatu_lugemine(x,y);
         }
    if(strcmp(g,"e")){
         x = x-5;
         y = y-5;
         raamatu_lugemine(x,y);
         }
    if(strcmp(g,"v")){
         break;
    }
    }
    system("PAUSE");
    return 0;
   }

void raamatu_lugemine(int arv, int arv2){
    ifstream fin("andmebaas.txt");
    struct Raamat Raamat;
    char sisend_rida[100];
    for(int i = arv; i <= arv2 && ! fin.eof(); i++){
            fin.getline(sisend_rida, 100);
            strcpy(Raamat.Raamat,strtok(sisend_rida, "#"));
            Raamat.Ilmumisaasta = atoi(strtok(NULL,"#"));
            Raamat.Lehtedearv = atoi(strtok(NULL,"#"));
            strcpy(Raamat.Eesnimi,strtok(NULL,"#"));
            strcpy(Raamat.Perekonnanimi,strtok(NULL,"#"));
            strcpy(Raamat.Keel,strtok(NULL,"#"));
            strcpy(Raamat.Loetud, strtok(NULL,"#"));
            Raamat.Hinne = atoi(strtok(NULL, "#"));

            cout << i+ 1 << "." << endl;
            cout << "Raamat: " << Raamat.Raamat << "[" << Raamat.Keel << "]" << endl;
            cout << "Ilmumisaasta: " << Raamat.Ilmumisaasta << endl;
            cout << "Lehek[lgede arv: " << Raamat.Lehtedearv << endl;
            cout << "Autor: " << Raamat.Eesnimi << ", " << Raamat.Perekonnanimi << endl;
            if(Raamat.Loetud == "true"){
                      cout << "Loetud(" << Raamat.Hinne << "/10)" << endl;
                      }
}
}


sin you dont understand estonian then j = 5 next rentryes, e = 5 last entryes. v = exit. But it only reads the same entries 1 entry = 1 line in the file. No matter what i press it activates 5 last netryes and ten ends its cycle. ant the part with // is temporary removed, cause it didnt work, but i need to get it working.
Apr 15, 2012 at 5:43pm
char g[1]; is one problem. Even if you enter a string of 1 char, it is gong to take one more for the 0 terminator. You could use a char[10], string or just char instead.

Raamat.Loetud == "true" is another. Use strcmp to compare cstrings. Though there is no reason to use cstrings at all. std::string makes things much more comfortable.

raamatu_lugemine is also weird. Aren't you just reading first (arv2-arv+1) elements?

I can't say what is the reason of your problem. Your code is a bit hard to understand. Try fixing the above and see if that helps.
Apr 16, 2012 at 12:01pm
strcmp(g, "j") gives the function of v, if i but !strcmp(g, "j") then ressing j gives the right function. raamatu_lugemine, its a temporary state for this function. When i manage to ket andmebaas[#] = sinsend_rida, then it will change.

How can i make it so that i can read frm text fie to string.
string sisend_rida

cin >> sisend_rida would be usual, but since i want to read from text file, i need to do something different.
Last edited on Apr 16, 2012 at 12:13pm
Apr 16, 2012 at 5:13pm
How can i make it so that i can read frm text fie to string.
1
2
3
4
5
6
7
8
9
//If you have
std::fstream file;
std::string str;

//, to read a single word do
file >> str;

//, to read a line do
std::getline( file, str );


Sorry, I didn't get the first half of your post at all.
Last edited on Apr 16, 2012 at 5:14pm
Apr 16, 2012 at 5:55pm
Ty, fewl last things and then its complete.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void raamatu_lugemine(int arv, int arv2, string y[]){

    struct Raamat Raamat;
    string sisend_rida;
    for(int i = arv; i <= arv2 && i <= y.length(); i++){
            sisend_rida = y[i];
            Raamat.Raamat = strtok(sisend_rida, "#");
            Raamat.Ilmumisaasta = atoi(strtok(NULL,"#"));
            Raamat.Lehtedearv = atoi(strtok(NULL,"#"));
            Raamat.Eesnimi = strtok(NULL,"#");
            Raamat.Perekonnanimi = strtok(NULL,"#");
            Raamat.Keel = strtok(NULL,"#");
            Raamat.Loetud = strtok(NULL,"#");
            Raamat.Hinne = atoi(strtok(NULL, "#"));

            cout << i+ 1 << "." << endl;
            cout << "Raamat: " << Raamat.Raamat << "[" << Raamat.Keel << "]" << endl;
            cout << "Ilmumisaasta: " << Raamat.Ilmumisaasta << endl;
            cout << "Lehek[lgede arv: " << Raamat.Lehtedearv << endl;
            cout << "Autor: " << Raamat.Eesnimi << ", " << Raamat.Perekonnanimi << endl;
            if(Raamat.Loetud == "true"){
                      cout << "Loetud(" << Raamat.Hinne << "/10)" << endl;
                      }
}
}


y.length() gives error lenght not declared
Raamat.Raamat = strtok(sisend_rida, "#"); gives error cannot convert `std::string' to `char*' for argument `1' to `char* strtok(char*, const char*)'
Apr 16, 2012 at 8:06pm
y is an array. Unlike Java, in C++ arrays are a basic type and don't have any methods. You need to pass the length as another argument.

cstrings and std::strings use different functions. Is it a # separated file you have? In that case it might not be pretty. If you want to take a string and split it, you'll need to use string::find a lot. You could also do this while reading the file, using getline( file, str, '#' ).
Note that all the members of Raamat need to be changed from char* to string too. Also, if you do ever need to convert string to char*, there is a method str.c_str() for that.
Apr 17, 2012 at 12:13pm
Atm program works like this: 1 opens the file, read the lines into array called andmebaas. then closes the files. after that, all things are done with that array. eats member of andmebaas is a separate line from the text file.
So by the time, raamatu_lugemine is called, the text file is closed and all the info is extracted, only thing ot do is to format it in this way.

wehat if in struct Raamat, i change all the types from string to char and then i use strtok(sisend_rida.c_str(), "#")
Last edited on Apr 17, 2012 at 12:15pm
Apr 17, 2012 at 3:08pm
It might work, but it's a bad idea. Other string functions would probably not work then any more.
Here's a strtok for string: http://ideone.com/VVVLF
It's also silly, but you won't have to change much for it. I suppose, the best way would be to build a vector from each line, or maybe do it with getline and stringstream, but whatever..
Apr 17, 2012 at 5:46pm
Ty very mutch,m its up and running. Only thing that doesent want to work is that lenght.
Topic archived. No new replies allowed.