Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
Just gave you an example use of pass by reference and how to call it.

Test it out see what happens!

I saw that, thats what is confusing me. Lol i am hopeless i think.

----------

should i just scrap everything i have done and start over? or is there some hope for what i did already? i really dont even know WHERE Its wrong. ugh
 
No! You're pretty close.

Try this example!

Code:
#include <iostream>

void give_me_a_number(int & number)
{
     number = 5;
}

int main()
{
    int     receiving_variable = 15000;
    give_me_a_number(receiving_variable); 
    
    std::cout   << "We received the number: "
                << receiving_variable
                << "\n";
}
 
should i just scrap everything i have done and start over? or is there some hope for what i did already? i really dont even know WHERE Its wrong. ugh

The "meat" of your posted code seems fine, but it's your interfaces that need work. (Which is the point of the exercise).

B
 
@lloyddean: I ran that and understand it, but its when things get difficult i get confused.

im so frustrated :(!! and i dont want you guys to feel pity for me and give me the answer btw, because i want to feel accomplished when i finally figure it out but i feel like im at a dead end hahaha
 
You might think of references a non global way to share a local variable with subroutine.

:confused:
confusing. we spent about 20 seconds on global variables and local variables.
i wish my professor wasnt so terrrrible. im really trying here guys i promise
 
local variables to a function are not available to any code outside the function. Yet something magical just happened! What was it?

EDIT: Slight corrections to syntax and spelling.
 
local variable to a function are not available to any code outside the function. Yet something magical just happened! What was it?

im not sure of what this magical-ness is you speak of because im lost LOL.
 
Specifically, the value of receiving_variable before and after
Code:
give_me_a_number(receiving_variable);

B
 
What was the result of running the example code?

the number that was outputted was outside of the main function?

----------

the more im trying to fix this the more errors im getting :(

----------

am i getting closer or farther away ?

Code:
#include <iostream>
using namespace std; 
#include <cmath>

double polar (double &, double &, double, double);

int main ()
{
	double & x, & y, r, angle;
	
	cout << "Enter two numbers for polar coordinates: " << endl;
	cin >> r, angle; 

	polar (x, y, r, angle);

	cout << "The corresponding rectangular coordinates are (" << x << ", " << y << ")" << endl;
	
	return 0; 
}

double polar (double & x, double & y, double r, double angle) 
{

	x = r * cos(angle);
	y = r * sin(angle);
	
	return x, y;
}
 
it returns an integer? because its an integer data type??

----------

on a side note, the errors i get when i run this code confuse me.


polar.cpp:9:11: error: declaration of reference variable 'x' requires an
initializer
double & x, & y, r, angle;
^
polar.cpp:9:16: error: declaration of reference variable 'y' requires an
initializer
double & x, & y, r, angle;
^
polar.cpp:12:12: warning: expression result unused [-Wunused-value]
cin >> r, angle;
^~~~~
polar.cpp:27:9: warning: expression result unused [-Wunused-value]
return x, y;
^

.... Why do x and y have to be initialized, and what are those warnings saying? is this related to what you guys are talking about or no?
 
oh and also when do you use void? i know if you use void you dont have a return thing, but how do you know when to do it?

so what should they be initialized to..? -_-
 
Last edited:
Good question.

The problem with giving an answer is that not having been through your class I don't know what you've covered in class to date and thus don't have a shared context with which to craft a useful answer.
 
even if we did cover it in class, i probably didnt understand. this professor is a joke
 
um still not that great. i dont understandwhat i should initialize x and y to. i looked in my book and the only example on the reference stuff is with the quadratic equation and i cant follow it what so ever. so its not looking too great. im really not sure what else im supposed to change here! i feel like im close though

edit:
so i changed the main body of my code

Code:
int main ()
{
	double x;
	double y; 
	double r; 
	double angle;

because in the example i have it doesnt show the & in the initialization. when i do this the program runs, but its still wrong. ie: if i input 3, it spits 3 back out. so i know im not done, but was this a good move? is the & not supposed to be in the main body?
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.