In my college class were learning functions right now and im trying to write a program that takes a set of integers (aka 1,2,3,4,5) and displays them backwards.
I think i got it but im getting a syntax error that says: c:\ctheurer05\ctheurer05\ctheurer05.cpp(30) : error C2106: '=' : left operand must be l-value.
What does it mean by it must be l-value?
__________________
2004 Titan KC XE, 4x4, Preferred Pkg, OR, PRG 2" leveling kit, Cooper Discovery STT 33x12.50xR17LT tires on some Ultra Thunder 17x8 wheels.
Next Upgrade: Either trade in for a new one, or fix everything on this one...
Wheels, Tires, and a Lift. Dont bother me, im driving
I can help you A LOT more with a text copy of the code, just paste it in here or something, but it sounds like you need ==. (Test for equality) that, or the data type of your pointer is wrong.
And i thought i needed == as well, but when i did that it gave me a similar error- c:\ctheurer05\ctheurer05\ctheurer05.cpp(24) : warning C4553: '==' : operator has no effect; did you intend '='?
heres the function with the errors in it:
void reverse (int number)
{
int a, b, c, d;
1234 % 10 = a;
1234 % 100 / 10 = b;
1234 % 1000 / 100 == c;
1234 % 10000 / 1000 == d;
a, b, c, d == number;
}
__________________
2004 Titan KC XE, 4x4, Preferred Pkg, OR, PRG 2" leveling kit, Cooper Discovery STT 33x12.50xR17LT tires on some Ultra Thunder 17x8 wheels.
Next Upgrade: Either trade in for a new one, or fix everything on this one...
Wheels, Tires, and a Lift. Dont bother me, im driving
Well, there are a number of things wrong with this. I need a better explanation of what a,b,c,d is and (I assume from the syntax you are supposed to write your very own function) so I need you to describe what it is that you want to pass to your function and what you want it to do with that variable.
What is the format of the data you are being presented to sort? (Array, stringlist, read from a file?)
To answer the question of 'what is the errorhandler trying to tell me?':
1234 % 10 = a; // does nothing
a = 1234 % 10; // assigns the value of the calc to a
but this calculation doesn't make any sense.
Is there any chance you are trying to sort by number of digits?
Also,
If the function should return something, it is not void, void means don't return anything. If that is what you want, then at the end, it should be number = something. If you want to just return the result (maybe easier?) then put return(number); after the number = line and chance the function definition line to int reverse(whatever...
What is the format of the data you are being presented to sort? (Array, stringlist, read from a file?) Im not exactly sure what you're asking. What i did was just took the number 1234 and im trying to write a function (i know its the complicated way to do it, but thats the assignment) that will print 4321 to the screen.
To answer the question of 'what is the errorhandler trying to tell me?':
1234 % 10 = a; // does nothing
a = 1234 % 10; // assigns the value of the calc to a
but this calculation doesn't make any sense. Yeah, i caught that i had my variables backwards...oops. But by getting the modulus of 1234 (aka 1234/10= 123 remainer 4) i am getting rid of the other digits to just get 4, then inserting 4 into the variable a.
Is there any chance you are trying to sort by number of digits? Not necessarily sorting by the number of digits, but yes i am sorting the digits.
Thanks for the help this far, i tried to answer the questions as best i could in bold above.
EDIT: In reply to the void stuff. To return the integer backwards is actually a different problem. The book specifically said to start my assignment with void reverse(int number).
__________________
2004 Titan KC XE, 4x4, Preferred Pkg, OR, PRG 2" leveling kit, Cooper Discovery STT 33x12.50xR17LT tires on some Ultra Thunder 17x8 wheels.
Next Upgrade: Either trade in for a new one, or fix everything on this one...
Wheels, Tires, and a Lift. Dont bother me, im driving
Last edited by imaweirdo159; 10-29-2007 at 05:35 PM.
I think i may have found my problem...i may be reading the assignment wrong. Ive been thinking that they wanted number to be a variable but the example they give ("For example, reverse(3456) displays 6543.") its looking like they want me to insert the number there. Ill mess with the code a bit and get back to you guys.
Thanks again for the help this far!
__________________
2004 Titan KC XE, 4x4, Preferred Pkg, OR, PRG 2" leveling kit, Cooper Discovery STT 33x12.50xR17LT tires on some Ultra Thunder 17x8 wheels.
Next Upgrade: Either trade in for a new one, or fix everything on this one...
Wheels, Tires, and a Lift. Dont bother me, im driving
OOOOOOOOOOOOOoooooooooookay.
I understand the assignment now.
You are on the right track, though your approach is a little cumbersome.
So in the example, 1234 will be replaced by 'number'.
Be careful of rounding errors, I might use a rounding function to force predictable results. I don't know how far you are in this coding thing yet, but in case you don't know the limits of number, you might write something that does not need to know. ie
while (number > 0) AND index < ubound(array)
do
array[index] = number%10
number = number - array[index] / 10 //which will fix rounding for all cases
index++
Or something like that.
That would be limited by the size of the array only, not the size of the number you passed.
the other advantage to putting them in the array is that you can just retain the last index you used and start there, -1 each time, poof, numbers in reverse.
Let me know if you need more help, I been there.
And I taught C++ for 2 years in college, so I am only a little full of $hit.
OOOOOOOOOOOOOoooooooooookay.
I understand the assignment now.
You are on the right track, though your approach is a little cumbersome.
So in the example, 1234 will be replaced by 'number'.
Be careful of rounding errors, I might use a rounding function to force predictable results. I don't know how far you are in this coding thing yet, but in case you don't know the limits of number, you might write something that does not need to know. ie
while (number > 0) AND index < ubound(array)
do
array[index] = number%10
number = number - array[index] / 10 //which will fix rounding for all cases
index++
Or something like that.
That would be limited by the size of the array only, not the size of the number you passed.
the other advantage to putting them in the array is that you can just retain the last index you used and start there, -1 each time, poof, numbers in reverse.
Let me know if you need more help, I been there.
And I taught C++ for 2 years in college, so I am only a little full of $hit.
Arrays are the next chapter....so expect to see me back! haha
__________________
2004 Titan KC XE, 4x4, Preferred Pkg, OR, PRG 2" leveling kit, Cooper Discovery STT 33x12.50xR17LT tires on some Ultra Thunder 17x8 wheels.
Next Upgrade: Either trade in for a new one, or fix everything on this one...
Wheels, Tires, and a Lift. Dont bother me, im driving
I see what you were trying to do now... but you hard-coded your problem to require exactly 4 digits... and your function didn't perform as a function (it acted like a procedure), although it technically returns void.
have you learned the shift operators yet?
Quote:
Originally Posted by imaweirdo159
Thanks everybody for the help! I fixed it and it compiles and works beautifully now!
Code:
#include <iostream>
using namespace std;
void Reverse(int);
int main()
{
cout << "This program displays an integer reversed." << endl;
cout << "Enter the 4 digit integer you wish to have reversed." << endl;
int number=0;
cin >> number;
Reverse(number);
}
void Reverse(int number)
{
int a, b, c, d;
a = number % 10;
b = number% 100 / 10;
c = number % 1000 / 100;
d = number % 10000 / 1000;
cout << a << b << c << d <<endl;
}
I see what you were trying to do now... but you hard-coded your problem to require exactly 4 digits... and your function didn't perform as a function (it acted like a procedure), although it technically returns void.
have you learned the shift operators yet?
Yeah i didnt realize that the assignment was saying that i had to get user input, so that changed things up a bit.
Uhm i think weve talked about them, but not really gone over them. Gimme an example.
__________________
2004 Titan KC XE, 4x4, Preferred Pkg, OR, PRG 2" leveling kit, Cooper Discovery STT 33x12.50xR17LT tires on some Ultra Thunder 17x8 wheels.
Next Upgrade: Either trade in for a new one, or fix everything on this one...
Wheels, Tires, and a Lift. Dont bother me, im driving
Last edited by imaweirdo159; 10-30-2007 at 02:05 PM.
AutoForums.com is the premier network of enthusiast-owned
enthusiast-operated automotive communities.
We operate more than 100 automotive forums where our users consult peers for shopping information and advice, and share
experiences and opinions as a community.