Calling Function from Functions

Hello everyone. My goal in this program is to create three functions, the first to output menu only. The second to ask user for their choice. And the third to calculate the equations and then to finally output them. My question is for guidance as to where the variables should be. I tried different combinations but it didnt work. Any help would be appreciated.

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
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void menu ()
{
	cout<<"Main Menu"<<endl;
	cout<<"Please enter the number for one of these options"<<endl;
	cout<<"1 - Mean"<<endl;
	cout<<"2 - Variance"<<endl;
	cout<<"3 - Standard Deviation"<<endl;
	cout<<"Your Choice: ";
	int choice;
	cin>>choice;	
}
void userChoice ()
{	
	int a;
	if(a==1)
		{
			cout<<"mean"<<endl;
			
		}
	if(a==2)
		{
			cout<<"Variance"<<endl;
		}
	if(a==3)
		{
			cout<<"Dev"<<endl;
		}
}
float stat()
{
	int line;
	int array[1000];
	int counter = 0;
	string file  =  ("numbers.txt");
	ifstream num (file.c_str());
	if (num.is_open())
		{
			while (!num.eof())
				{
					num>>line;
					array[counter]=line;
					counter++;
				}
		cout<<array[1]<<endl;
		}
}
int main ()
{
	menu();
	userChoice();
	stat();
	return 0;
}
Well, the way you put the variables doesn't really make sense. In menu() you're declaring an integer choice and inputting into it, but it's just deleted at the end of the function. In userChoice() you're declaring int a, but using it before you assign anything to it. From what I can see in stat, you're loading a file, but you aren't doing anything with it. Again, all of the data will be deleted at the end of the function.

There are 2 ways that I can think of to make this work. You can declare a global variable (one outside of any function), or pass values to each function. Here's an example for each:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int num;//global variable

void outputnum(){
    cout<<num;}

int main(){
    num=4;//assign 4 to num
    outputnum();
    return 0;}
4

1
2
3
4
5
6
7
8
9
10
#include <iostream>

void outputnum(int num){//outputnum takes an int
    cout<<num;}

int main(){
    int num=4;
    outputnum(num);//pass the contents of num to outputnum
    outputnum(4);//pass 4 to outputnum
    return 0;}
44
Last edited on
Answer I was looking for. Thanks for your time.
Topic archived. No new replies allowed.