learning function prototyping and lost

If appropriate, I would like to have askForInterger function store the value from the cin as an interger and use it in the main function or another function. how can i do this? or what would be a better way to do this?


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

#include <iostream>

using namespace std;

void printClassInfo(void);

void printAssignmentInfo(void);

void askForInterger(void);


int main() {


	printClassInfo() ;
	
	printAssignmentInfo() ;
	
	askForInterger();
	
	return 0;
}	

void askForInterger() {
int iVal;
	
	cout << "\nPlease enter an interger: ";  
	cin >> iVal ;
	
	cout << iVal << endl;		/** prints the cin value */
	cout << iVal % 2<< endl;    /** just test.. checking the output value. is even by printing zero for even or 1 for odd */
	return ;
}

void printAssignmentInfo() {
	cout << "Assignment Info --"
		 << "\n Assignment Number:"    
		 << "\n Written By:	"		
		 << "\n Due Date: \n" << endl; 
	return;		 
}	
As it stands, the function askForInterger() doesn't return anything. If you want to be able to use the variable outside of that function, it needs return type int

int askForInterger(void)

Secondly, the variable iVal has only local scope, so it doesn't exist outside of the function. You can fix this by moving the delcaration to a global position (outside of a function body; I put my global variables right under the using directive)

Just remember that if main calls another function to do anything with iVal, that function needs to have the variable as an argument
Topic archived. No new replies allowed.