do while loop is not working in my program someone plz help

case 9:
show_in_out_info();
break;
}


getch();

}


void input_student_info()

{

do


{
cout<<"enter record of student"<<endl;
cout<<"enter student_id:"<<endl;
cin>>s_id[i].id;
cout<<"enter name:"<<endl;
cin>>s_id[i].name;
cout<<"enter room_no:"<<endl;
cin>>s_id[i].room_no;
cout<<"enter department:"<<endl;
cin>>s_id[i].department;
cout<<"enter cell.no:"<<endl;
cin>>s_id[i].cell_no;
cout<<"<_____address of student_____>"<<endl;
cout<<"enter country_name:"<<endl;
cin>>s_id[i].d;
cout<<"enter province_name:"<<endl;
cin>>s_id[i].d;
cout<<"enter city_name:"<<endl;
cin>>s_id[i].d;
cout<<"enter street:"<<endl;
cin>>s_id[i].d;
cout<<"enter street_no:"<<endl;
cin>>s_id[i].d;
cout<<"enter h_no:"<<endl;
cin>>s_id[i].d;
cout<<"do u want to enter another record(y/n)?"<<endl;
cin>>ch;

}
while(ch=='y'|| ch=='Y');



}
its a part of my program not the complete program.
Do you get an error message? Post it here.

Does it not work as ju expected? What does the program do and what did you expect?
I see nothing wrong in this do-while loop except that variable 'i' is not changed. So in the next iteration you will fill the same objects.
no there is no error ... it only displays the function once only

my program is on microsoft visual c++ 6.0
You did not show how ch is defined. If it is defined as int when there will be an input error and ch never will be equal 'y' or 'Y'. ch shall be defined as char.
Last edited on
This works, but could do with a lot of improvement, as I'm sure you don't want all your struct/class members to be std::string. Better to use a for loop in which you increment 'i' and also check the value of 'ch' on each iteration:

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

using namespace std;

struct Student
{
	string id;
	string name;
	string room_no;
	string department;
	string cell_no;
};

const int CLASS_SIZE(30);
Student s_id[CLASS_SIZE];

void input_student_info()
{
	char ch('y');

	for (int i = 0; i < CLASS_SIZE && ch == 'y'|| ch == 'Y'; ++i)
	{
		cout<<"enter record of student"<<endl;
		cout<<"enter student_id:"<<endl;
		cin>>s_id[i].id;
		cout<<"enter name:"<<endl;
		cin>>s_id[i].name;
		cout<<"enter room_no:"<<endl;
		cin>>s_id[i].room_no;
		cout<<"enter department:"<<endl;
		cin>>s_id[i].department;
		cout<<"enter cell.no:"<<endl;
		cin>>s_id[i].cell_no;
		cout<<"<_____address of student_____>"<<endl;
		cout<<"enter country_name:"<<endl;
		//cin>>s_id[i].d;
		//cout<<"enter province_name:"<<endl;
		//cin>>s_id[i].d;
		//cout<<"enter city_name:"<<endl;
		//cin>>s_id[i].d;
		//cout<<"enter street:"<<endl;
		//cin>>s_id[i].d;
		//cout<<"enter street_no:"<<endl;
		//cin>>s_id[i].d;
		//cout<<"enter h_no:"<<endl;
		//cin>>s_id[i].d;
		cout<<"do u want to enter another record(y/n)?"<<endl;
		cin>>ch;
	}
}

int main()
{
	input_student_info();

	return 0;
}
Topic archived. No new replies allowed.