Please read this...i am newbie

i am newbie in c++..
i have trouble in my program...
i confuses in this...why my program can't continue...just keep in main page...please help me..thank you

#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib> //declaration
#include<conio.h>
#include<string>
using namespace std;
int main()
{

int StudentAge,menu,choice,number;
string StudentIdNo, StudentCourse, StudentAddress;
char StudentName[25], StudentGender[1],StudentIcNo[15];

//Display main menu
cout<<"\n\n\n\n\n";
cout<<" =.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.="<<endl;
cout<<" = !!COURSE REGISTRATION SYSTEM!! ="<<endl;
cout<<" = !!WELCOME TO METRO COLLEGE!! ="<<endl;
cout<<" = \t\t\t\\t\t\t\t\t\tDesign by Shirley Ng ="<<endl;
cout<<" =.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.="<<endl;
cout<<"\n\n\n\n\n";
cout<<" ";
while (true)
{
do
{

cout<<" Please enter the follwing options:\n";
cout<<" \t1.ADD A NEW STUDENT RECORD\n";
cout<<" \t2.SEARCH STUDENT RECORD\n";
cout<<" 3.DELETE STUDENT RECORD\n";
cout<<" \t4.QUIT\n";
cout<<"Enter choice(1-4):";

menu = _getch();
}
while(false);

system("cls");
switch(menu){
case 1:
cout<<" =.=.=.=.=.=.=.=.=.=.=.=.=.=.=\tNew Student Profile=.=.=.=.=.=.=.=.=.=.=.=.=.="<<endl;
{
ofstream output("student file.txt");
cout<<"Student Name:\t";
gets(StudentName);

cout<<"Student ID:\t";
cin>>StudentIdNo;
cout<<"Gender:\t";
cin>>StudentGender;
cout<<"IC No:\t";
cin>>StudentIcNo;
cout<<"Age:\t";
cin>>StudentAge;
cout<<"Student Course:\t";
cin>>StudentCourse;

output<<" New Student Profile\t"<<endl<<"Name:\t"<<StudentName<<endl;
output<<"ID:\t"<<StudentIdNo<<endl<<"Gender:\t"<<StudentGender<<endl;
output<<"Age:\t"<<StudentAge<<endl<<"IC no:\t"<<StudentIcNo<<endl;
output<<"Course:\t"<<StudentCourse<<endl<<endl;

if(StudentIdNo==StudentIdNo)
{
cerr<<"Student ID:"<<StudentIdNo<<"already contains information."<<endl;
}
cout<<"\n\nStudent Record has been save to file"<<endl<<endl;
system("pause");

cout<<"Do you want to go back menu?\n";
cout<<"1. Yes"<<endl;
cout<<"2 No"<<endl;
cin>>number;
if (number==1)
return main();
else
cout<<" Thank you for register"<<endl;
cout<<"Have A Nice Day"<<endl;
} break;

case 2:
cout<<" =.=.=.=.=.=.=.=.=.=.=.=.\tSearch Student Record=.=.=.=.=.=.=.=.=.=.=.="<<endl;
cout<<endl;
cout<<"Please Enter Studend ID:"<<endl;
cin>>StudentIdNo;
cin.ignore();
{
ifstream input ("student file.txt");
while(!input.eof())
{
input>>StudentIdNo;
cout<<StudentIdNo<<endl;
}
input.close();
system("pause");

cout<<"Do you want to go back menu?\n";
cout<<"1. Yes"<<endl;
cout<<"2 No"<<endl;
cin>>number;
if (number==1)
return main();
else
cout<<" Thank you for register"<<endl;
cout<<"Have A Nice Day"<<endl;
}break;

case 3:
cout<<" Please Enter Student ID:"<<endl;
cin.ignore();
cout<<StudentIdNo<<endl;
{
ifstream input;
input.open("student file.txt");
while(!input.eof())
{
input>>StudentIdNo;
cout<<StudentIdNo<<endl;
}
system ("pause");
cout<<"Are you sure want to delete?\n";
cout<<" 1. Yes"<<endl;
cout<<" 2. No"<<endl;
cin>>number;
if(remove("student file.txt")==1)
{
cout<<"Data have been delele";
}
return 3;
input.close();
if(number==2)
return 0;
}break;

case 4:{
cout<<"\n\n\n";
cout<<"=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=";
cout<<" \t THANK YOU FOR REGISTER " ;
cout<<\t METRO COLLEGE!!\t ";
cout<<" HAVE A NICE DAY!! \t ";
cout<<"=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.= ";
menu=_getch();
while(menu==4);
return 0;
}break;
system("cls");
default:
cout<<"Please enter again"<<endl;
{
while(menu>5);
cout<<endl;
return main ();
}
}
}
}
Last edited on
while (true)

Infinite loop

 
while(false)


Unreachable condition
Last edited on
how to solve it?
can u teach me?
I can't even read the code..
how to make it like them post code have line by line
?
how to make it like them post code have line by line
?


Use code tags [code][/code].

[code]
// line 1
// line 2
// ...
[/code]

becomes

1
2
3
// line 1
// line 2
// ... 


Btw please align the braces in your code well...
Last edited on
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>           //declaration
#include<conio.h>
#include<string>
using namespace std;

int main()
{
							
	int StudentAge;
        int menu;
        int choice;
        int number;
	string StudentIdNo, StudentCourse, StudentAddress;
	char StudentName[25];
        char StudentGender[1];
        char StudentIcNo[15];
	
								//Display main menu
	cout<<"\n\n\n\n\n";
	cout<<"           =.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.="<<endl; 
	cout<<"           =               !!COURSE REGISTRATION SYSTEM!!                ="<<endl; 
	cout<<"           =                   !!WELCOME TO METRO COLLEGE!!    	        ="<<endl;
	cout<<"           = 		        \t\t\t\tDesign by Shirley Ng                      ="<<endl;
	cout<<"           =.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.="<<endl;
	cout<<"\n\n\n\n\n";
    cout<<"                         "; 

while (true)
	{
	do
	{
		
	cout<<"  Please enter the follwing options:\n";     //Get selection
	cout<<"  1.ADD A NEW STUDENT RECORD\n";       
	cout<<"  2.SEARCH STUDENT RECORD\n";
	cout<<"	 3.DELETE STUDENT RECORD\n";
	cout<<"  4.QUIT\n";
	cout<<"Enter choice(1-4):";
	
	menu = _getch();
	}
	while(false);
	
		system("cls");
		switch(menu){
		case 1:            //Create new student account
	cout<<" =.=.=.=.=.=.=.=.=.=.=.=.=.=.=\tNew Student Profile=.=.=.=.=.=.=.=.=.=.="<<endl;  


	{
		//create new file
		ofstream output("student file.txt");
		cout<<"Student Name:\t";
		 gets(StudentName);

	cout<<"Student ID:\t";
	cin>>StudentIdNo;
	cout<<"Gender:\t";
	cin>>StudentGender;
	cout<<"IC No:\t";
	cin>>StudentIcNo;
	cout<<"Age:\t";
	cin>>StudentAge;
	cout<<"Student Course:\t";
	cin>>StudentCourse;

	output<<" New Student Profile\t"<<endl<<"Name:\t"<<StudentName<<endl;
	output<<"ID:\t"<<StudentIdNo<<endl<<"Gender:\t"<<StudentGender<<endl;
	output<<"Age:\t"<<StudentAge<<endl<<"IC no:\t"<<StudentIcNo<<endl;
	output<<"Course:\t"<<StudentCourse<<endl<<endl;

	if(StudentIdNo==StudentIdNo)
{                           //when a ID aldready contains information can't using it
	cerr<<"Student ID:"<<StudentIdNo<<"already contains information."<<endl;

}
	cout<<"\n\nStudent Record has been save to file"<<endl<<endl;	
	system("pause");
	
	cout<<"Do you want to go back menu?\n";    //return to menu
	cout<<"1. Yes"<<endl;
	cout<<"2  No"<<endl;
	cin>>number;
	
                  if (number==1)
                         return main();
	else 
		cout<<" Thank you for register"<<endl;
		cout<<"Have A Nice Day"<<endl;
	}	break;

		case 2:
			cout<<" =.=.=.=.=.=.=.=.Search Student Record=.=.=.=.=.=.=.=.=.=.=.="<<endl; 

//Search student record when keyin ID number
			cout<<endl;
			cout<<"Please Enter Studend ID:"<<endl;
			cin>>StudentIdNo;
			cin.ignore();
			{
			ifstream input ("student file.txt");			//read the information
			while(!input.eof())
			{
				input>>StudentIdNo;
				cout<<StudentIdNo<<endl;
			}
			input.close();
			system("pause");
			
	cout<<"Do you want to go back menu?\n";
	cout<<"1. Yes"<<endl;
	cout<<"2  No"<<endl;
	cin>>number;
	
           if (number==1)
               return main();
	else 
		cout<<" Thank you for register"<<endl;
		cout<<"Have A Nice Day"<<endl;
			}break;

		case 3:						//Delete student account

			cout<<" Please Enter Student ID:"<<endl;	
			cin.ignore();
			cout<<StudentIdNo<<endl;
			{
				ifstream input;
				input.open("student file.txt");
				while(!input.eof())
				{
					input>>StudentIdNo;
					cout<<StudentIdNo<<endl;
				}
					system ("pause");

					cout<<"Are you sure want to delete?\n";
					cout<<" 1. Yes"<<endl;
					cout<<" 2. No"<<endl;
					cin>>number;
				
                               if(remove("student file.txt")==1)
					{
					cout<<"Data have been delele";
					}
					return 3;
					
					if(number==2)
						return 4;
                                       input.close();
			}break;
					
		case 4:{							//Quit the program
				cout<<"\n\n\n";
						      
                   cout<<"=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.= "; 
                   cout<<"     \t                       THANK YOU FOR REGISTER                   ";
		   cout<<"                		   \tMETRO COLLEGE!!                                 ";
		   cout<<"		 			 HAVE A NICE DAY!!                             ";
		   cout<<"=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=.=. ";
		
                 menu=_getch();
			while(menu==4);
						return 0;
				}break;

		default:									
			cout<<"Please enter again"<<endl;
			{
			while(menu>5);
			cout<<endl;
			return main ();
			}
				}
				}
}
//end of program 
Last edited on
Just referring to
just keep in main page
, then line 49 should be case '1':...etc, getch() gets a char but menu is an int.
case 49: also "works", because char(49) is '1'.

What is the meaning of do...while(false);? If a loop always executes once, it needs not to be a loop.

Tip of the century: Use functions
oic...do u knw hw to write delete code?
cause i dont knw how to write in my case 3...my case 3 cant working....
I'm not familiar with remove(), but http://www.cplusplus.com/reference/clibrary/cstdio/remove/ says
On failure, a nonzero value is reurned
so I think line 145 should be if(remove("student file.txt")==0).

That's from cstudio. Try using something also from fstream.

Just give relevant codes next time, and related errors...don't just paste everything and expect someone to read through it.
ok....

now i have this problem..in return...
1
2
3
4
5
6
7
8
9
10
cout<<"2  No"<<endl;
	cin>>number;
	
                  if (number==1)
                         return main();
	else 
		cout<<" Thank you for register"<<endl;
		cout<<"Have A Nice Day"<<endl;
	}	break;

why have a error?
http://img140.imageshack.us/i/12594637.jpg/
Last edited on
You are missing a '{' on your else statement...also, do NOT EVER rescursively call main() (e.g. call it inside of itself like you are on line 5 of that snippet)
Yes, why are you recursing? You'll get a stack overflow...
Topic archived. No new replies allowed.