C,C++,C#

Re: C,C++,C#

Post by Desoxena on Tue Oct 19, 2010 7:14 pm
([msg=47812]see Re: C,C++,C#[/msg])

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++
Desoxena
New User
New User
 
Posts: 25
Joined: Sun Oct 03, 2010 7:13 pm
Blog: View Blog (0)


Re: C,C++,C#

Post by LoGiCaL__ on Tue Oct 19, 2010 7:50 pm
([msg=47816]see Re: C,C++,C#[/msg])

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.
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: C,C++,C#

Post by OnlyHuman on Tue Oct 19, 2010 8:12 pm
([msg=47817]see Re: C,C++,C#[/msg])

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.
OnlyHuman
Poster
Poster
 
Posts: 191
Joined: Sat Aug 22, 2009 1:37 am
Blog: View Blog (0)


Re: C,C++,C#

Post by Desoxena on Sun Jan 02, 2011 12:16 am
([msg=51506]see Re: C,C++,C#[/msg])

I only really know about C++; but all i can say is it's an easy enough language to learn. :D
Desoxena
New User
New User
 
Posts: 25
Joined: Sun Oct 03, 2010 7:13 pm
Blog: View Blog (0)


Previous

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests