Reputation: 101
I'm having a problem understanding what I'm doing wrong in my code. What I'm trying to do is write a condition for a ternary operator and a do-while loop to recognize if one of my variables is above 1. Well, it is giving me an error that I don't know how to fix. What puzzles me the most is what I'll give an example of shortly. Here's my overall code. Keep in mind I'm a beginner, so there may be things that make you cringe or parts that could be improved.
#include <iostream>
using namespace std;
void getInfo(int&, int&, double&);
int main() {
int ordered, stock;
double charges = 10.00;
getInfo(ordered, stock, charges);
system("pause");
return 0;
}
void getInfo(int& ordered, int& stock, double& charges) {
do {
printf("Enter the amount of spools ordered, in stock, and handling charges: ");
scanf_s("%i %i %lf", &ordered, &stock, &charges);
printf((&ordered > 1 && &stock > 0 && &charges > 0) ? "All added!\n"
: "You messed one up. AGAIN!\n");
} while (&ordered > 1 && &stock > 0 && &charges > 0);
}
Now, the error I'm getting is specifically in the ternary and the while condition. It gives me an error where the >
is after ordered for both. Now, if I make it ordered
instead of &ordered
, the error goes away. Yet, I never get an error for &stock
or &charges
. I don't know why it's treating &ordered
differently. It also doesn't check ordered
correctly when I take off the &
, for reasons I'm not entirely sure on.
Thank you to whomever is willing to help!
Upvotes: 0
Views: 78
Reputation: 151
The &
operator does different things depending on where you put it. If it's in a type declaration (e.g. int& foo
), it means that the type is a reference. If however the &
is used as an unary operator in an expression it becomes the Address-of operator, and returns a pointer to the object it's used on. So for example int* bar = &spam
(assuming spam
is an integer) would assign a pointer to spam in in the pointer bar.
Note that reference types behaves identical to the real type. This is perhaps better illustrated with a piece of code:
#include <iostream>
int main() {
int foo = 12;
int& bar = foo; // a reference expects a variable of the same type in the initializer
bar = 24; // Once the reference has been made the variable behaves indentically to the
// to the variable it's a reference to.
std::cout << foo << std::endl; // outputs 24
// if you use the & operator on a reference you get the address the variable it is a
// reference to.
std::cout << &bar << ' ' << &foo << std::endl; // Outputs two equal addresses.
}
There is also a third meaning of &
in C++. As the bitwise and operator. foo & bar
would result in the bitwise and of the variable foo
and bar
.
Upvotes: 1
Reputation: 118340
...(&ordered > 1 && &stock > 0 && &charges > 0) ? "All added!\n"
Here, "&ordered" means "the address of the ordered
variable. You're obviously not trying to compare the address of ordered
, but rather ordered
itself. This should be
...(ordered > 1 && stock > 0 && charges > 0) ? "All added!\n"
The same problem is with your while() statement too.
In C++, "&" means two things. In declarations, it's used to declare a reference. In expression, it's the "address of" operator.
Once you declare a reference, like:
int &whatever;
Subsequently, using just whatever
refers to the referenced object itself.
: "You messed one up. AGAIN!\n");
Upvotes: 1