C++ Do-While Issue

Hello Everyone, I Was Asked To Make A Basic Database,Everything Works Okay Untill The Do-While Part,The While Part Doesn't Get Executed. Any Suggestions On How To Make It Work?

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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<string.h>
#include<process.h>
#include<ctype.h>
struct criminal
{ int rn,id,age,yrs;
  char name[25],crime,des[100],dang,gng;
}c[100];

void main ()
{ clrscr();
char choice;
int choice2,n,i,srn,ch;

randomize();

cout<<"*******++++++WELCOME TO The F.B.I DATABASE++++++*******";
cout<<"\n\nPlease Wait";
sleep(1);
cout<<".";
sleep(1);
cout<<".";
sleep(1);
cout<<".";
sleep(1);
cout<<"\n\n\nWelcome To The F.B.I Criminal Record Page,Information Beyond This Point Is Highly Confidential,Are You Sure You Want To Continue?"<<"\tEnter Y To Continue ";
cin>>choice;
toupper(choice);
if (choice!='Y')
   { cout<<"\n\nOkay,Bye";
     cout<<"\nExiting";
     sleep(1);
     cout<<".";
     sleep(1);
     cout<<".";
     sleep(1);
     exit(0);
   }

     cout<<"\n\nLoading";
     sleep(1);
     cout<<"-";
     sleep(1);
     cout<<"-";
     sleep(1);
     cout<<"-";
     sleep(1);
   do
     { cout<<"\n\n*****Menu*****";
       cout<<"\n1.Enter A New Criminal";
       sleep(1);
       cout<<"\n\n2.Search For A Criminal";
       sleep(1);
       cout<<"\n\n3.Edit The Details Of An Inputted Criminal";
       sleep(1);
       cout<<"\n\n4.Exit ";
       sleep(1);
       cout<<"\n\nYour Choice? ";

       cin>>choice2;

     switch (choice2)
     {

     case 1:   cout<<"\nEnter The Number Of Criminals Whose Record Is To Be Created ";
	       cin>>n;
	       for(i=0;i<n;i++)
		  { cout<<"\n\nEnter The Full Name Of The Criminal- ";
		    cin.getline(c[i].name,25);
		    cout<<"\nEnter Age Of The Criminal ";
		    cin>>c[i].age;
		    cout<<"\nEnter The Criminal Record Number- ";
		    cin>>c[i].rn;
		    c[i].id=random(1000+100);
		    cout<<"\nEnter The Number Of Years Sentenced- ";
		    cin>>c[i].yrs;
		    cout<<"\nIs The Criminal Certified Dangerous? (Y/N) ";
		    cin>>c[i].dang;
		    cout<<"\nIs The Criminal A Gangster? ";
		    cin>>c[i].gng;
		    cout<<"\nCriminal Description- ";
		    cin.getline(c[i].des,100);
		    cout<<"\nProcessing";
		    sleep(1);
		    cout<<".";
		    sleep(1);
		    cout<<".";
		    sleep(1);
		    cout<<"\nGenerating ID Number";
		    sleep(1);
		    cout<<".";
		    sleep(1);
		    cout<<".";
		    sleep(1);
		    cout<<"\nFinalizing";
		    sleep(1);
		    cout<<".";
		    sleep(1);
		    cout<<".";
		    sleep(1);
		    cout<<".";
		    sleep(1);

		   }
	       break;

     case 2:   cout<<"\nEnter The Criminal Record Number Of The Criminal- ";
	       cin>>srn;
	       cout<<"\nSearching";
	       sleep(1);
	       cout<<".";
	       sleep(1);
	       cout<<".";
	       sleep(1);
	       for (i=0;i<n;i++)
		   { if(srn==c[i].rn)
		       { cout<<"\n\nThe Full Name Of The Criminal- ";
			 cout<<c[i].name;
			 cout<<"\nAge Of The Criminal ";
			 cout<<c[i].age;
			 cout<<"\nThe Criminal Record Number- ";
			 cout<<c[i].rn;
			 cout<<c[i].id;
			 cout<<"\nThe Number Of Years Sentenced- ";
			 cout<<c[i].yrs;
			 cout<<"\nCriminal Certified Dangerous? (Y/N) ";
			 cout<<c[i].dang;
			 cout<<"\nThe Criminal A Gangster ";
			 cout<<c[i].gng;
			 cout<<"\nCriminal Description- ";
			 cout<<c[i].des;
		       }
		     else
		       { cout<<"The Searched Criminal Does Not Exist";
		       }
		   }
		   break;

     case 3:   cout<<"\nEnter The Criminal Record Number Of The Criminal- ";
	       cin>>srn;
	       cout<<"\nSearching";
	       sleep(1);
	       cout<<".";
	       sleep(1);
	       cout<<".";
	       sleep(1);
	       for (i=0;i<n;i++)
		   { if(srn==c[i].rn)
		       { cout<<"\nEnter Age Of The Criminal ";
			 c[i].age;
			 cout<<"\nCriminal Description- ";
			 cin>>c[i].des;
			 cout<<"Updating";
			 sleep(2);
			 cout<<".";
			 sleep(1);
			 cout<<".";
			 sleep(1);
			 cout<<"\nDetails Updated.";

		       }
		     else
		       { cout<<"The Searched Criminal Does Not Exist";
		       }
		   }
		   break;

     case 4:   cout<<"\n\nOkay,Bye";
	       cout<<"\nExiting";
	       sleep(1);
	       cout<<".";
	       sleep(1);
	       cout<<".";
	       sleep(1);
	       exit(0);
	       break;

     default:  cout<<"The Inputted Choice Is Wrong ";
	       break;


     }
     }

	       while(choice2!=4);



      getch();

 }
Last edited on
Where is the problem exactly? Everything works for me. (1)

Btw you should initialize n to 0 because if I enter e. g. 3 as my choice and I haven't entered any criminal before, program will crash.

1
2
3
4
cin>>n;
for(i=0;i<n;i++)
	{ cout<<"\n\nEnter The Full Name Of The Criminal- ";
	cin.getline(c[i].name,25);


cin lefts newline character in input stream and getline will read that character so user will not be able to enter any data here. So you will need to put cin.ignore after cin statement.

1
2
cout<<c[i].rn;
cout<<c[i].id;

You should print a space or new line between these two outputs.

(1)
I had to make some changes to be able to run the code on my machine:

Cannot open include file: 'iostream.h': No such file or directory

I have only <iostream> on my computer.

'cout' : undeclared identifier
 'cin' : undeclared identifier

I had to write using namespace std;

'sleep': identifier not found

I'm on Windows so I had to include <windows.h> and to change sleep(1/2) to Sleep(1000/2000).

'clrscr': identifier not found
'randomize': identifier not found
'random': identifier not found

I had to comment that out.
Last edited on
The while part is being executed every time round the loop, although the debugger may not show you that because of all the white space in your code.

1
2
3
    }

	       while(choice2!=4);


} while(choice2!=4);

there's nothing wrong with what you wrote, it just looks like the "while" is detached from the "}"

The debugger will loop at 186 because that's the last line of the loop, it it really is examining choice2 every time.
Thanks Alot Guys For The Help, I Modified The Code, But It Still Doesn't Run The While Part At The End. When It Asks For "Whether I Want To Continue" (I Added That In) And If I Type In Y Or N, It Just Crashes. Any Suggestions As To Why This Is Happening?

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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<string.h>
#include<process.h>
#include<ctype.h>
#include<stdio.h>
struct criminal
{ int rn,id,age,yrs;
  char name[25],crime,des[100],dang,gng;
}c[100];

void main ()
{ clrscr();
char choice,choice3;
int choice2,n,i,srn,ch;

randomize();

cout<<"*******++++++WELCOME TO The F.B.I DATABASE++++++*******";
cout<<"\n\n\t\t\tPlease Wait";
sleep(1);
cout<<".";
sleep(1);
cout<<".";
sleep(1);
cout<<".";
sleep(1);
cout<<"\n\n\nWelcome To The F.B.I Criminal Record Page"<<endl<<"Information Beyond This Point Is Highly Confidential."<<endl<<"You Must Be Authorised To Continue!"<<endl<<"Are You Sure You Want To Continue? "<<endl<<"Enter Y To Continue ";
cin>>choice;
choice=toupper(choice);
if (choice!='Y')
   { cout<<"\n\nOkay,Bye";
     cout<<"\nExiting";
     sleep(1);
     cout<<".";
     sleep(1);
     cout<<".";
     sleep(1);
     exit(0);
   }

     cout<<"\n\nLoading";
     sleep(1);
     cout<<"\t-";
     sleep(1);
     cout<<"-";
     sleep(1);
     cout<<"-";
     sleep(1);
   do
     { cout<<"\n\n*****Menu*****";
       cout<<"\n\n1.Enter A New Criminal";
       sleep(1);
       cout<<"\n\n2.Search For A Criminal";
       sleep(1);
       cout<<"\n\n3.Edit The Details Of An Inputted Criminal";
       sleep(1);
       cout<<"\n\n4.Exit ";
       sleep(1);
       cout<<"\n\nYour Choice? ";

       cin>>choice2;

      switch (choice2)
      {

     case 1:   clrscr();
	       cout<<"\nEnter The Number Of Criminals Whose Record Is To Be Created ";
	       cin>>n;
	       for(i=0;i<n;i++)
		  { cout<<"\n\nEnter The Full Name Of The Criminal - ";
		    gets(c[i].name);
		    cout<<"\nEnter Age Of The Criminal - ";
		    cin>>c[i].age;
		    cout<<"\nEnter The Criminal Record Number - ";
		    cin>>c[i].rn;
		    c[i].id=random(1000+100);
		    cout<<"\nEnter The Number Of Years Sentenced - ";
		    cin>>c[i].yrs;
		    cout<<"\nIs The Criminal Certified Dangerous? (Y/N) ";
		    cin>>c[i].dang;
		    cout<<"\nIs The Criminal A Gangster? ";
		    cin>>c[i].gng;
		    cout<<"\nCriminal Description - ";
		    gets(c[i].des);
		    cout<<"\nProcessing";
		    sleep(1);
		    cout<<".";
		    sleep(1);
		    cout<<".";
		    sleep(1);
		    cout<<"\nGenerating ID Number";
		    sleep(1);
		    cout<<".";
		    sleep(1);
		    cout<<".";
		    sleep(1);
		    cout<<"\nFinalizing";
		    sleep(1);
		    cout<<".";
		    sleep(1);
		    cout<<".";
		    sleep(1);
		    cout<<".";
		    sleep(1);

		   }
	       break;

     case 2:   clrscr();
	       cout<<"\nEnter The Criminal Record Number Of The Criminal- ";
	       cin>>srn;
	       cout<<"\nSearching";
	       sleep(1);
	       cout<<".";
	       sleep(1);
	       cout<<".";
	       sleep(1);
	       for (i=0;i<n;i++)
		   { if(srn==c[i].rn)
		       { cout<<"\n\nThe Full Name Of The Criminal- ";
			 cout<<c[i].name;
			 cout<<"\nAge Of The Criminal ";
			 cout<<c[i].age;
			 cout<<"\nThe Criminal Record Number- ";
			 cout<<c[i].rn;
			 cout<<c[i].id;
			 cout<<"\nThe Number Of Years Sentenced- ";
			 cout<<c[i].yrs;
			 cout<<"\nCriminal Certified Dangerous? (Y/N) ";
			 cout<<c[i].dang;
			 cout<<"\nThe Criminal A Gangster ";
			 cout<<c[i].gng;
			 cout<<"\nCriminal Description- ";
			 cout<<c[i].des;

		       }
		     else
		       { cout<<"The Searched Criminal Does Not Exist";

		       }

		   }
		   break;

     case 3:   clrscr();
	       cout<<"\nEnter The Criminal Record Number Of The Criminal- ";
	       cin>>srn;
	       cout<<"\nSearching";
	       sleep(1);
	       cout<<".";
	       sleep(1);
	       cout<<".";
	       sleep(1);
	       for (i=0;i<n;i++)
		   { if(srn==c[i].rn)
		       { cout<<"\nEnter Age Of The Criminal ";
			 c[i].age;
			 cout<<"\nCriminal Description- ";
			 gets(c[i].des);
			 cout<<"Updating";
			 sleep(2);
			 cout<<".";
			 sleep(1);
			 cout<<".";
			 sleep(1);
			 cout<<"\nDetails Updated.";

		       }
		     else
		       { cout<<"The Searched Criminal Does Not Exist";

		       }
		   }
		   break;

     case 4:   cout<<"\n\nOkay,Bye";
	       cout<<"\nExiting";
	       sleep(1);
	       cout<<".";
	       sleep(1);
	       cout<<".";
	       sleep(1);
	       exit(0);
	       break;

     default:  clrscr();
	       cout<<"The Inputted Choice Is Wrong ";
	       break;


     }
	       cout<<"\n\nDo You Want To Continue? (Y/N) ";
	       cin>>choice3;
	       choice3=toupper(choice3);
     }

	       while(choice3=='Y');



      getch();

 }
Hi,

There are a bunch of things I would change:

C++ include files don't have a .h on the end:

1
2
3
4
5
6
7
#include<iostream>
#include<conio.h> // avoid using this, it's ancient and non standard
#include<dos.h>   // why do you have this ?
#include<stdlib.h>
#include<string>
#include<process.h>    // why do you have this ?
#include<ctype> 


This and similar things elsewhere is plain annoying:

23
24
25
26
27
28
29
sleep(1);
cout<<".";
sleep(1);
cout<<".";
sleep(1);
cout<<".";
sleep(1);


If you really want to do this, put it in a function. Any time you have repetitive code, there is a better way.
http://www.cplusplus.com/doc/tutorial/functions/


With this:

1
2
3
4
struct criminal
{ int rn,id,age,yrs;
  char name[25],crime,des[100],dang,gng;
}c[100];


Choose better variable names. There is no need to abbreviate the hell out of everything, meaningful names would vastly improve your code. If you do this well, the code should read like a story.

Declare1 variable per line, comment expected range of valid values.

Don'truneverythingtogether - put some spaces around operators.

Don't have global variables. Limit the scope of variables as much as possible, at least declare this variable in main:


std::vector<criminal> CriminalData; //use vector rather than array


void main ()

It's always int main() , that's mandated in the C++ standard.

Put menu display into their own functions. Declare the functions before main, put the definitions of them after main.

Line 29: std::cout statements can be built up in stages, no need to have a massive long statement that is 3 screens wide:

1
2
std::cout << "\n\n\nWelcome To The F.B.I Criminal Record Page, Information Beyond This Point Is Highly " ;
std::cout << "Confidential, Are You Sure You Want To Continue?"<<"\tEnter Y To Continue ";


I am not a fan of do loops, just because they always execute once shouldn't be a reason to prefer them. All 3 looping statements can be converted from one to another, so I would have a while loop for this. Notice you have the logic to continue is asked twice?

With switch statements, consider having each case to call a function, this will tidy up your code considerably.

Lines 71 to 86 , you have this twice, so put it in a function.

Good Luck !!
Last edited on
Topic archived. No new replies allowed.