Is it possible

Hi, is it possible to put this part of my program in a single function?
I would like it to be within a function it would make my main only consist of functions
The thing I'm not sure about is that there is a lot of variables so if I made it a void function would I have to define everything in the function prototype(I think its called prototype you know its the heading. Thanks much.

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
	while(!fin.eof())
	{
		str1 = "";
		str2 = "";
		str3 = "";
		Printchoice=PrintSelection();
		GetAddress(str1,str2,str3);
		Echo(str1,str2,str3);
		Displayprintselection(Printchoice);
	

	//Control of selection
	switch(Printchoice)
	{
	
	case 1: PrintAddress(str1,str2,str3);
			cout << "\n" << endl;
		    break;

	case 2: PrintFormattedAddress(str1,str2,str3);
		    cout << "\n" << endl;
		    break;

	case 3: PrintAddress(str1,str2,str3);
		    cout << '\n' << endl;
			PrintFormattedAddress(str1,str2,str3);
			cout << "\n" << endl;
			break;

	default: "\n";
	}
	}
system("pause");



}




Maybe just the control of selection part on second thoughts. But still there is a lot there how would I handle all the variables just define everything in the function heading?
Yeah can. But you should be careful in passing your paramters

void all_function(ifstream& fin,string& str1,string& str2,string& str3);
{
your code
}

or you can declare 3 strings as local variables of that functions

void all_function(ifstream& fin)
{
string str1,str2,str3;
your code
}
OK would but it is possible.

So in c++ its OK that there are functions within a function? You know above I have a printaddress function and so forth if you look at the cases. So my question is I would just put those in the heading of the function like a variable? If that is I was to make the control of selection chunck of code into a function?
Sorry I just get confused because of the functions that are being used by that switch and I think to myself ... " how to I take care of that?" Thanks.
Yeah there can be function inside the function. You can call a function inside another code. For example, if we rewrite you code, it will be similar like this

all_function function definition

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
void all_function(ifstream& fin)
{
string str1,str2,str3;
fin.open("abcd.txt");
while(!fin.eof())
	{
		str1 = "";
		str2 = "";
		str3 = "";
		Printchoice=PrintSelection();
		GetAddress(str1,str2,str3);
		Echo(str1,str2,str3);
		Displayprintselection(Printchoice);
	

	//Control of selection
	switch(Printchoice)
	{
	
	case 1: PrintAddress(str1,str2,str3);
			cout << "\n" << endl;
		    break;

	case 2: PrintFormattedAddress(str1,str2,str3);
		    cout << "\n" << endl;
		    break;

	case 3: PrintAddress(str1,str2,str3);
		    cout << '\n' << endl;
			PrintFormattedAddress(str1,str2,str3);
			cout << "\n" << endl;
			break;

	default: "\n";
	}
	}
system("pause");
}



Main

1
2
3
4
5
6
int main()
{
ifstream in;
all_function(in);
return 0;
}



Does this make clear?
Last edited on
Oh thats cool. Thanks a lot sir. Yes it is more clear but I always have questions ha. I see what is going on but I do not need to define any of the functions in the heading? That is the only thing that is confusing. It just works that way because I already wrote the functions seperatly so calling it in the function is no different then If I called it in main? Also I have a question about loops really quick ...


What happens if you have a loop and it is say like ..


int number=0;
cin number;
while(number<2)
{
cout<< HI<<end;
}


int number=0;
cin number;
while(number<2)
{


cout<< HI<<end;


}
Ok what is happens if I have number in the loop once more versus if I have it just outside of the loop like the second example?

I cna't figure this out ..

Thanks for the last example very helpful

Looks like both codes you have given are same.

1
2
3
4
5
6
7
8
9
10
int number=0;
cin>> number;
while(number<2)
{


cout<< HI<<end;


}


even though you initialised number to zero, later number value will be changed to from zero value to the value you key in as input.

Result
1. If you enter number greater than 2, program terminates.
2. But if you enter number less than 2, the program will be printing "HI" endlessly
Topic archived. No new replies allowed.