Thread: why it dont cout??

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    70

    Question why it dont cout??

    Code:
    #include <iostream>
    #define YES 1
    #define NO 0
    
    using namespace std;
    
    int main()
    {
        int x=YES;
        int y;
        if (x==YES){
                    cout<<"goed geantwoord";
                    cin>>y;
                    if(y==5){
                    x==NO;
                    }
                    }
        if (x==NO){
                   cout<<"x=no";
                   }
                   return 0;
                   }
    why isnt't it couting "x=no"????
    ps: it is couting "goed geantwoord"


    thnx in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Probably because you are comparing x to NO instead of assigning NO to x... and possibly also because 5 is not entered.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > x==NO;
    Because this is a useless comparison, rather than a meaningful assignment.
    Use = here.

    $ g++ -W -Wall foo.cpp
    foo.cpp: In function `int main()':
    foo.cpp:15: warning: statement has no effect
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    No==x dont work
    and the rare thing is it is printing the first thing:s
    i give it some other trie

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    Quote Originally Posted by Salem
    Use = here.
    It means you have to write
    Code:
    x=NO;

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    btw this isnt working too
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    int main()
    {
        
        int x;
         
        cout<<"goed geantwoord";
                    x=false;
                    
        if (x=false){
                   cout<<"x=no";
                   main();
                   }
                   cin.get();               
                   }

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    Quote Originally Posted by Overlord
    It means you have to write
    Code:
    x=NO;
    not working

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by hallo007
    btw this isnt working too
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    int main()
    {
        
        int x;
         
        cout<<"goed geantwoord";
                    x=false;
                    
        if (x=false){
                   cout<<"x=no";
                   main();
                   }
                   cin.get();               
                   }
    Code:
        if (x=false){
    And what does that line?

    BTW, your indentation is HORRIBLE.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (x=false)
    I didn't say do a global substitute of == to =

    Also, use a loop rather than calling main() recursively (which is illegal in C++)

    And echo maxorator on the code formatting.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    Quote Originally Posted by maxorator
    Code:
        if (x=false){
    And what does that line?

    BTW, your indentation is HORRIBLE.
    if i think what identitation means , (i dont know it sure)

    then you are right , i know it ,but when i change in 1minute much things and tried things it becum like this

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    43
    Indentation means proper alignment and spacing, as so:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
            bool x;
    
    	while ( true )
    	{
    		cout << "goed geantwoord";
    		x = false;
    	                
    		if ( x == false )
    			cout << "x=no";
    
    		cin.get();
    	}
    
    	return 0;
    }
    Although I really don't see any use for an unending program.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    it's for learning

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class methods to cout stream
    By shintaro in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2008, 07:27 PM
  2. cout vs std::cout
    By none in forum C++ Programming
    Replies: 10
    Last Post: 07-26-2004, 11:20 PM
  3. changing cout output
    By Yohumbus in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 04:09 AM
  4. Redirecting cout stream
    By Arrow Mk84 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 04:17 PM
  5. printf vs cout
    By RyeDunn in forum C++ Programming
    Replies: 5
    Last Post: 07-09-2002, 04:26 PM