Loop Exit

OK I am Trying to Get this to Work, But the Problem i am having is that the N_STUDENT Loop Works fine, and the N_MODULE Loop only seems to Loop Once, if i enter 3 Students it allows me to Enter Modules for the First Student but not for the Second it asks me how Many Modules i enter 2.. the Program Exits



For Some Reason its not Looping for the N_modules.


Also cant get the Write to File Working.. what do i need to place within the const char* if i am using Strings ?



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
#include<iostream>
#include<string>
#include<sstream> 
#include<fstream> 

using namespace std;

struct Student
{
    int student_number;    //Student number
    string student_firstname; //Students first Name
    string student_lastname; //Students last Name
    string Address;         //Address of Student
    int Date_Registered; //Date Registered
};

struct Course
{
    string modules; //Student modules
    int marks; //Marks of modules 
};

int main ()
{
    int N_Module, j=0;
    struct Course stArray1[6];
    int N_STUDENT, i=0;    
    struct Student stArray[4];
    
    
    fstream myFile ("Student.text", ios::in | ios::out | ios::binary); 
    N_STUDENT = -1;
    
    do
    { 	cout  << "Enter number of students less than 4" <<endl;
        
        cin >> N_STUDENT;
        cout << endl;
    }
    while(N_STUDENT > 4 || N_STUDENT<0);
    
    for(i; i < N_STUDENT; i++)
    { 
    cout << " Enter Student First Name: ";
    cin >>stArray[i].student_firstname;
    
    cout << " Enter Student Last Name: ";
    cin >> stArray[i].student_lastname;
    
    cout << "Enter Student Address: ";
    cin >> stArray[i].Address;        
     
    N_Module = -1;
    
    do
    { 	cout  << "Enter number of Modules Taken less than 6" <<endl;
        
        cin >> N_Module;
        cout << endl;
    }
    
    while(N_Module > 6 || N_Module < 0);
    
    for (j; j < N_Module; j++)
    {
        cout << "Enter Module Name: ";
    cin >> stArray1[j].modules;
    
    cout << "Enter Module Marks: ";
    cin >> stArray1[j].marks; 
    }
    
    }
    myFile.write (reinterpret_cast <const char*> (& N_STUDENT), sizeof (Student));
    myFile.write (reinterpret_cast <const char*> (& N_Module), sizeof (Course));
    return 0;
}



ÿâ?pˇä?pˇ∞ç?pÿâ?pˇnttbl}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww9000\viewh8400\viewkind0
}

That is what i am getting in my Text File..
Last edited on
Line 74/75 are wrong. You're writing a binary int with the size of Student/Course. That causes the gibberish.

Instead use the operator << like so:

1
2
3
4
5
6
7
8
9
10
myFile << N_STUDENT << " ";
for(i = 0; i < N_STUDENT; i++)
    {
        myFile << stArray[i].student_firstname << " " << ...
        myFile << N_Module << " ";
        for (j = 0; j < N_Module; j++)
        {
            myFile << stArray1[j].modules << " " << ...
        }
    }


Separate the entries with blank (" "). So you can read it in the same fashion

Line 64 should be for (j = 0; j < N_Module; j++) // Set j to 0 otherwise it stops working the second time it comes to this
Last edited on
are you saying something like this



1
2
3
cout << " Enter Student First Name: ";
    cin >>stArray[i].student_firstname;
    myFile << stArray[i].student_firstname;
I'm not too sure what you're refering to, but yes stream is stream
I dont know if this work

1
2
 myFile.write (reinterpret_cast <const char*> (& N_STUDENT), sizeof (Student));
    myFile.write (reinterpret_cast <const char*> (& N_Module), sizeof (Course));

instead you can use
1
2
3
4
5
6
7
8
for(i = 0; i < N_STUDENT; i++)
{
      myFile<<stArray[i]. student_number;    //Student number
      myFile<< stArray[i].student_firstname; //Students first 
      myFile<<stArray[i].student_lastname; //Students last Name
      myFile<< stArray[i].Address;         //Address of Student
     myFile<< stArray[i].Date_Registered; //Date Registered
}
Topic archived. No new replies allowed.