I want to call the input function, which works when I run it, but then I want to display the "temp" and "windspeed" variables in the main function and I don't know how to do this, im looking online at tutorials but im still confused. I guess I shouldn't have missed class last week :(

Any help is appreciated, thanks!

//function example 
#include <iostream>
#include <cmath>
using namespace std;

int input();


int main ()
{

	input();
	
	return 0;
}


int input ()
{
	int temp, windspeed;
	cout << "Enter the temperature: "; 
	cin >> temp;
	cout << "\nEnter the windspeed: "; 
	cin >> windspeed;

	return (temp);
}

Recommended Answers

All 4 Replies

If you want to display those variables in main() then you will have to declare them in main() and pass them by value to input()

int main()
{
    int temp, windspeed;
    inpuut( &temp, &windspeed);
}

void input(int* temp, int* windspeed)
{
   *temp = 30;
   *windspeed = 20;
}
//function example 
#include <iostream>
#include <cmath>
using namespace std;

int input();


int main ()
{
	int temp, windspeed;
	input(&temp, &windspeed);
	
	return 0;
}


void input (int* temp, int* windspeed)
{

	cout << "Enter the temperature: "; 
	cin >> *temp;
	cout << "\nEnter the windspeed: "; 
	cin >> *windspeed;

	return;
}

Compiler says function does not take two arguments. Another question is what are the * and & signs in the code used for here?

you have to change the prototype on line 6.


The & makes the address of the integer, and the * is how to change the value of a pointer integer. If you don't know about pointers yet here is a tutorial by DaWei

Your prototype/declaration on line 6 does not match the definition. They must match exactly in order for the compiler to understand what you are doing.

The & and * are pointer operators. If you are just learning about functions, you probably haven't learned about them yet. I'm not too sure I should elaborate on them, to avoid confusing you more. If you would like to read about pointers, click here. The example AD gave you is one way of passing variables to a function by REFERENCE. In C, it was the only way to do it. C++ defines another simpler way, but both work:

void myFunc(int &, int &);

int main ()
{
	int iVar1=0, iVar2=0;
	myFunc(iVar1, iVar2);
	/*
	...
	*/
	return 0;
}

void myFunc (int &iRef1, int &iRef2)
{
	/*
	...
	*/
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.