What should I do to get a reply ?

hi i've been working on this not for hours but days and weeks already changed it tooo many times read tooo many comments here and I am so confused already that i'm going crazy....
please somebody help me with this:

1) getline doesn't wait for input I've tried ignore, and sync but non works
2) I have another function calling this display one and I get onley some addresses... to admit I am not very good with pointers and I already read the theory but I need explanation with exaples ..more examples... so that os the code:
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
82
83
void get_record (struct student ss)
{ fflush(stdin);
  char act,act1;
  int size;
  char xname[50], xfac_No[9], xgroup[3];
  fp=fopen("DB.txt","rb+");
   if (fp!=NULL)
 {cout<<"\nWould you like to seach by full name,faculty number or group?Press: \n"
      <<"\n   - N to search by name,\n"
      <<"   - F - by number and\n"
      <<"   - G - by group;\n";
      cin>>act1;
      
  if (act1=='N'||act1=='n')
  { cout<<"\nPlease write the name of the student you want to search for: ";
     cin.sync();
    cin.ignore('\n'); 
    cin.getline(xname,50);
    rewind(fp);
    while (!feof(fp))
   {if (strcmp(ss.fac_No,xfac_No) == 0)
    { display(ss);
      break;}
    }
  cout<<"\nThere is no such student enrolled in this course.\n";
  cout<<"Would you like to add him/her? If yes, press A,\n"
      <<" if not- M (to return to the main menu):";
  cin>>act;}
  if ((act=='a')||(act=='A')) add_student(ss);
  else if ((act=='m')||(act=='M')) menu();
  }
   
  else if (act1=='F'||act1=='f')
   { cout<<"\nPlease enter the faculty number you want to search for: ";
     cin.sync();
     cin.ignore('\n');
     cin.getline(xfac_No,9);
     rewind(fp);
     while (!feof(fp))
   {if (strcmp(ss.fac_No,xfac_No) == 0)
    { display(ss);
      break;}
    }
    cout<<"\nThere is no such student enrolled in this course.\n";
    cout<<"Would you like to add him/her? If yes, press A,\n"
        <<" if not- M (to return to the main menu):";
    cin>>act;
    if ((act=='a')||(act=='A')) add_student(ss);
    else if ((act=='m')||(act=='M')) menu();
   }
  
 else if (act1=='G'||act1=='g')
 { cout<<"\nPlease write the group you want to search for:"<<endl;
     cin.sync();
    cin.ignore('\n');
    cin.getline(xgroup,3);
    rewind(fp);
  while (!feof(fp))
   {if (strcmp(ss.fac_No,xfac_No) == 0)
    { display(ss);
      break;}
    }
  cout<<"\nThere is no such group .\n";
  getchar();
  menu();
  }
 else cout<<"File could not be opened!!"; 
}


void display ( student st)
{ int size;
  cout << "\n---- View student's record ----\n";
  cout <<"\n"<<"Student's faculty number:"<<" "<<st.fac_No<<"; "<<"Student's name:"<<" "<<st.name;
  cout <<"\n"<<"Specialty:"<<" "<<st.spec<<"; "<<"Group:"<<" "<<st.group;
     cout<<setw(15)<<"Mathematics:"<<setw(6)<<st.notes.math<<endl;
     cout<<setw(15)<<"PIK1:"<<setw(6)<<st.notes.pik<<endl;
     cout<<setw(15)<<"English:"<<setw(6)<<st.notes.en<<endl;
     cout<<setw(15)<<"Electotechnics:"<<setw(6)<<st.notes.et<<endl;
     cout<<setw(15)<<"Economics:"<<setw(6)<<st.notes.econ<<endl;
     cout<<setw(15)<<"Web Design:"<<setw(6)<<st.notes.web_d<<endl;
     cout<<setw(15)<<"Sport:"<<setw(6)<<st.notes.sport<<endl;   
}
Last edited on
Line 1: You're passing struct student ss by value. Since it is passed by value, you can't use it to return anything. It also doesn't appear that you're using any values passed in. In which case, you should just declare it as a local variable.

Line 7 suggestion: If you can't open the file, display a message and return immediately rather than embedding your entire function within an if(fp != NULL)
1
2
3
4
  if (! fp)
  { cout << "File could not be opened" << endl;
     return;
  }


Lines 15-21: You're asking for the student name, but comparing against the faculty number.

Lines 20-27: You're not reading fp anywhere. What's the point of comparing ss.fac_No with the user input xfac_No? Did you mean to read fp into ss? As the code reads, you're comparing against the struct that was passed in each time through the while loop? You're going to hang in the loop since unless the file is empty, eof() will always be false.,

Line 39-43: Same comments as lines 20-27.

Lines 30, 49, 65: Assuming get_record is called from menu, you're calling menu recurrisively. This is going to cause problems. You should simply return; to the calling function (menu).

Lines 58-62: Same comments as lines 20-27.















THANK YOU SOO MUCH !!!

I fixed the mistakes you pointed out but I'm still not sure about how this call by reference should work ...

I mean I have 4 functions (called from the menu) and the display function also... so Should I function declaration looke like for exaple get_record (struct& student ss) and should I call it with another argument in the main menu for example to declare another student st and than call like get_record(st) is that what I should do ...because I'm really confused about this call by refence thing...

...and one last thing would it cause some problems if all functions are using same arguments

thanks again :)
Call by reference says that both the caller and the called function shared the same structure. That makes sense when the caller has loaded data into the structure and the called function wants to access the same data.

My point about making the struct a local variable was because it didn't appear that you were storing any data into ss in menu(), therefore there is no need to pass it as an argument. However, if you're storing data into the struct in one function, and then passing that data to another function, then yes, you want to pass it by reference.

It doesn't matter what you call the structure in the calling function or in the called function. The compiler associates the references automatically. An example of this is where you call display passing ss, but in display it's called st.


would it cause some problems if all functions are using same arguments

No.
Last edited on
Thanks again .. a lot :) that explanation was a lot clearer than the ones I found before I made some changes and now I have troubles finding the name I am searching for :? I changed the definition of display to display(student& st) and also fixed the others..I think.. now the get_record function looks like this:

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
void get_record ()
{ student stt;
  fflush(stdin);
  cin.sync();
  char act,act1;
  long int size;
  size=sizeof(stt);
  char xname[50], xfac_No[9], xgroup[3];
  fp.open("DB.txt",ios::binary|ios::in|ios::out);  
  if (!fp)
   {cout << "File could not be opened" << endl;
     return;
   }
 fp.seekg(ios::end,ios::beg);
 cout<<"\nWould you like to seach by full name,faculty number or group?Press: \n"
      <<"\n   - N to search by name,\n"
      <<"   - F - by number and\n"
      <<"   - G - by group;\n";
 cin>>act1;
if (act1=='N'||act1=='n')
  { cout<<"\nPlease write the name of the student you want to search for: ";
    cin.sync();
    cin.ignore('\n');
    cin.getline(xname,50);
    while (!fp.eof())
    {fp.read ((char*)&stt,size);
    if (strcmp(stt.name,xname) == 0)
    {display(stt);
    getchar();
	return;}
    }
   }

...and same with group and fac_No search but I have no maches

Furthermore I replaced the recursive calls to menu() with retunr but when I go back to the menu and want to try another function the only result I get is.. "File cannot be opened" always

Would it be too much to ask for more help
A couple of things:

Line 14: Your use of seekg is incorrect.
http://www.cplusplus.com/reference/istream/istream/seekg/
Should be:
 
fp.seekg (0, ios_base::beg);


Line 26: How was db.txt written? Was the entire struct written as one element, or is it a space separated file? A read for the size of struct will only work if that is the way the file was written.

well yeah here is the other thing I am quite unsure about...
Can I create my own txt file and read from it with this functions because i tried and nothing.. so now as I chek the input looks completely different each time first there were spaces and the content was not completely... well coded and now it looks like a whole element of some ||[][] or something like that is this because of the mode of the opened file
Topic archived. No new replies allowed.