Why isn't it working?

This is for my school project. Please tell me why it won't work.
I want to write the class objects to a file called STUD-DBMS.


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
//-----------------------------------------------------------------------
//--------------------------Class Definition-----------------------------
//-----------------------------------------------------------------------

class Student{
		char stdname[40];
		int Admno;
		int rollno;
		char ClassSection[4];
		char phone[10];
		char stream[10];
		char Address[50];
public:
		void GetData();
		void ShowBasicData();
		void ShowAllData();
		void GetName();
		void GetAdmno();
		void GetClass();
		void GetPhone();
		void GetStream();
		void GetAddress();
}s;//End of Class Definition





//----------------------------------------------------------------------
//--------------------------Make A New Database-------------------------
//----------------------------------------------------------------------

void NewDB(){		//Function To Make/Replace The Database.
clrscr();
char GoGoGo='y';
cout<<"\nMain Menu -> Make a New Database\n";
cout<<"\n\t\tMake A New Database";
cout<<"\n\n\a\aWARNING: This option will over-write the current Database!";
cout<<"\nDo you really want to do this? ( Y-Yes | Any other Key-No )";
cin>>GoGoGo;
if(GoGoGo!=='y'||GoGoGo!=='Y') // <------SOMETHING WRONG HERE
exit(1);                       // <------AND HERE.
ofstream WriteToFile("STUD-DBMS",ios::out|ios::binary);

while(GoGoGo=='y'||GoGoGo=='Y'){//Keep writing till user wants.
	WriteToFile.write(((char*)&s),sizeof(Student));
				}//End of While
WriteToFile.close();
}//End of NewDB() 
Last edited on
I'm not that good at c++ but I would say that it might be the:
if(GoGoGo!=='y'||GoGoGo!=='Y')

Shouldn't it be:
if(GoGoGo!='y'||GoGoGo!='Y')
A couple of problems, one minor, another more important.

Line 41:
if(GoGoGo!=='y'||GoGoGo!=='Y')

The operator should be either "equal to" == or "not equal to" !-
In this case you presumably want this: if (GoGoGo!='y' || GoGoGo!='Y')
But then, if the user enters 'n', neither condition is true, so the exit is followed.
What if the user enters 'y'? Well, the first test is false. But the second will be true. So the exit is always taken.

It should actually look like this:
1
2
    if (GoGoGo!='y' && GoGoGo!='Y')
        exit(1);


That was the minor problem.

The other one is more serious.
Lines 45 to 47:
1
2
3
4
while (GoGoGo=='y'||GoGoGo=='Y')
{
    WriteToFile.write(((char*)&s),sizeof(Student));
}


If we reach this point, we know that one of the conditions is true, so the while loop is entered. The big question is this: How does the while loop ever terminate?


Last edited on
@Vynius and Chervil, Thank you very much! I really appreciate your help!


Could you please clarify this as well?

It should actually look like this:

1
2
    if (GoGoGo!='y' && GoGoGo!='Y')
        exit(1);



I get it now! Thanks!

1
2
3
4
while (GoGoGo=='y'||GoGoGo=='Y')
{
    WriteToFile.write(((char*)&s),sizeof(Student));
}


If we reach this point, we know that one of the conditions is true, so the while loop is entered. The big question is this: How does the while loop ever terminate?


Lol I'm so dumb :P
Last edited on
1
2
    if (GoGoGo!='y' && GoGoGo!='Y')
        exit(1);


But both the conditions can't be true at once. Therefore, I feel using OR ( || ) is better (?).


Look at it this way, GoGoGo = 'z'; // some other value
The code simplifies to this:
1
2
    if (true  && true)
        exit(1);

In this case it doesn't matter whether we use || instead of &&, either way the result is true.

Now the other example, GoGoGo = 'y'; // valid value
The code simplifies to this:
1
2
    if (true  && false)
        exit(1);

What is the result of true && false?
The result is only true if both conditions are true. Since they are not, the result is false and exit(1) is not executed.

What if we use || instead?
We get this:
1
2
    if (true  || false)
        exit(1);

What is the result of true || false? The result is true if either or both conditions are true, therefore the result is true and exit(1) is executed.

Basically, when you use the not-equal operator != you need to take extra care when combining more than one condition.
I thank you from the bottom of my heart :D
You've helped me so much. Thanks a lot!
Topic archived. No new replies allowed.