Page 4 of 4

Re: C,C++,C#

PostPosted: Tue Oct 19, 2010 7:14 pm
by Desoxena
OK, I just have one more question. So i decided to not try to confuse myself by making all of these complicated scripts (for my level understanding of the C++ languge anyway) so i decided to make a simpler script, here it is:

#include <iostream>

using namespace std;

int main () {
int x;
cout << "what is x?" << endl;
cin >> x >> endl;
if (x == 2)
cout << "you entered a two!" << endl;
} else {
cout << "you entered a one!" << endl;
cin.get();
return 0;
}
------------------------------------------------------------------

So i tried compiling and running this script, and it highlighted the "cin >> x >> endl;" line (line 8)
and the error message was "no match for 'operator>>' in '(&std::cin)->std::basic_istream<_CharT, _Traits>::operator>> [with _CharT = char, _Traits = std::char_traits<char>](((int&)(&x))) >> std::endl' ". Could someone tell me what this means? I have no idea at all why this wouldn't compile. BTW, i'm using the compiling program Dev-C++

Re: C,C++,C#

PostPosted: Tue Oct 19, 2010 7:50 pm
by LoGiCaL__
Looks like the bracket before your else statement is closing main() before it is meant to.

-- Tue Oct 19, 2010 7:55 pm --

Yeah, your brackets are in the wrong place. Try indenting to help yourself out. Also, something with your:
Code: Select all
cin >> x >> endl; 
is not needed and causing an error.

Re: C,C++,C#

PostPosted: Tue Oct 19, 2010 8:12 pm
by OnlyHuman
That message from the compiler is Cplusplusinese for 'std::endl is an output only entity'. You can only send it to output streams. You're trying to read it from std::cin, which just isn't going to happen. Also, LoGiCaL__ already pointed out a flaw in the bracketing. You included a closing bracket after the if statement, without first opening it. Then when you got to your else clause, you did the complete opposite. This is going to cause the compiler to get all pissy.

Here, this should work:

Code: Select all

#include <iostream>

using namespace std;

int main ()
{

   int x;

   cout << "what is x?" << endl;

   // can't read std::endl from std::cin
   cin >> x;

   // missing opening bracket fixed
   if (x == 2)
   {
      cout << "you entered a two!" << endl;
   }
   else
   {
      cout << "you entered a one!" << endl;
   }

   cin.get();

   return 0;

}



Hopefully that helps. Also, this ~>website<~ might help too. Be sure to check out the sourcecode section. There are tutorials available for download.

Re: C,C++,C#

PostPosted: Sun Jan 02, 2011 12:16 am
by Desoxena
I only really know about C++; but all i can say is it's an easy enough language to learn. :D