-
Ivalue annoyance
Been moving thru Accelerated C++ and I’m a little far thru, this problem annoys me though. This is the program that you make in chapter 2 which adds a frame to an inputted name etc etc Also says hello. Dev C++ says that there’s an error with it. Being that there is no I value in the assignment. The line under the comment: are we on the border?. Can someone help me with this I always seem to trip up on the I value stuff. Can I get an explanation on it as well for future reference. Thanks very much.:)
Code:
#include<iostream>
#include<string>
//say what standard-library names we use
using std::cin; using std::endl;
using std::cout; using std::string;
int main()
{
//ask for persons name
cout<<"Please enter your first name: ";
//read the name
string name;
cin>>name;
//build the message that we intend to write
const string greeting="Hello, "+name+ "!";
//the number of blanks surrounding the greeting
const int pad=1;
//the number of rows and columns to write
const int rows=pad *2 +3;
const string::size_type cols=greeting.size() + pad * 2 + 3;
//write blank line of seperation
cout<<endl;
//write rows rows output
//invariant:we have written r rows so far
for(int r=0; r !=rows; ++r){
string::size_type c=0;
//invariant: we have written c characters so far in the current row
while(c !=cols){
//is it time to write the greeting?
if(r==pad + 1 && c==pad + 1){
cout<<greeting;
c +=greeting.size();
}else{
//are we on the border?
if(r==0||r==rows-1||c==0||c=cols-1)
cout<<"*";
else
cout<<" ";
++c;
}
}
cout<<endl;
}
return 0;
}
-
Code:
if(r==0||r==rows-1||c==0||c == cols-1)
-
> if(r==0||r==rows-1||c==0||c=cols-1)
Perhaphs it's the missing = in c == cols-1
-
It's L-Value(elle-value) not I-Value(eye-value). The L stands for left. A left-value is a value that can appear on the left side of an assignment (left = right). The reason that you get a non l-value warning on this line:
Code:
if(r==0||r==rows-1||c==0||c=cols-1)
is because of the C order of precedence. The logical OR operator ( || ) has a higher precedence than the assignment operator ( = ). We can add brackets to show how the compiler tries to evaluate this line:
Code:
if( ((((r==0) || (r==rows-1)) || (r==0)) || c) = (cols-1) )
Obviously, we can see that the value on the left side of the assignment operator (highlighted) is not an l-value. Assuming we really wanted to do an assignment we could add brackets to force the correct order of precedence:
Code:
if(r==0||r==rows-1||c==0|| (c=cols-1))