Thread: Help!

  1. #16
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    This is what I ended up with after an hour of trying to get it to tell me how many negatives i've put it, lol... its kinda funny. Help!
    Code:
    #include <iostream>
    int main()
    {
        std::cout <<"Enter numbers: "<<std::endl;
        int value;
        std::cin >> value;
        std::cin.ignore();
        std::cin.get();
        while (std::cin >> value)
        
        if (value < 0)
        {
              value < 0;
              std::cout <<"There are " << value << "negative numbers" << std::endl;
              std::cin.ignore();
              std::cin.get();
               }
               else
               {
                   value >= 0;
                   std::cout << "This is positive" << std::endl;
                   std::cin.ignore();
                   std::cin.get();
                   }
                     value = 0;
                     std::cin.ignore();
                     return 0; 
                     }

  2. #17
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Why do you have this:

    while (std::cin >> value)

    ??

    This is all you need (IMO):

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main( void )
    {
    	int value;
    
    	cin >> value;
    
    	if 		( valie < 0 )  cout<< "NEG!";
    	else if 	( value > 0 )  cout<< "POS!!";
    	else if 	( value == 0 ) cout<< "ZERO!";
    	else cout<< "I don't know what's wrong!";
    
    	return 0;
    }


    I you had the while loop there so that you could do it a lot, all you need do is the following:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main( void )
    {
    	int value = 1;
    
    	while ( value > 0 )
    	{
    		cin >> value;
    		// Do stuff with value here!
    	}
    
    	return 0;
    }
    EDIT - all compiled with MS Notepad, so ware or warnings and errors!
    Last edited by twomers; 07-25-2006 at 11:23 PM.

  3. #18
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    book im using makes it look somuch morecomplicated then that rofl... dang, thanks forhelp!

  4. #19
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Quote Originally Posted by Mrzach3590
    Problem Solved!
    Thank you for asking a question, getting your answer, and then deleting the question so people are playing jeopardy trying to guess what your question was.

  5. #20
    Me
    Join Date
    Jul 2006
    Posts
    71
    Quote Originally Posted by Loctan
    Thank you for asking a question, getting your answer, and then deleting the question so people are playing jeopardy trying to guess what your question was.
    I agree, I hate people that do that, but they never listen, so I just quit trying...

  6. #21
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Meh. Who cares? Well obviously ye two do, but what difference does it make?

    Mrzach3590 - it's commonly considered 'not cricket' to edit your posts after they have been solved. Others can learn from your questions too! So refrain from doing it in the future to keep us all happy!

    Now, where's that 'Thread Solved', button they were talking about ... ?

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Problem Solved!
    I was half-way to putting this thread in the trash-can until I saw more replies.
    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.

  8. #23
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I am sure Mrzach is in page 10 of his/her book.
    And one question: Why you say or just write "then that" instead of "than that"??
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #24
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    Oops im sorry didnt realize.

  10. #25
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    Original question:
    Code:
    #include <iostream>
    int main()
    {
        int x, y;
        std::cout << "Enter 2 numbers: " << std::endl;
       
        std::cin >> x >> y;
       
        std::cout << " The sum of "<< x << " and " << y << " is " << x + y << std::endl;
     
        return 0;
    }
    It compiles andruns but wont stay open for more then a millisecond so im not sureif its working or not!

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Common question with a more detailed answer in the FAQ about waiting for a keypress. Simple solution is to add std::cin.ignore() to ignore the newline from when the user hits enter after inputting the numbers, then add std::cin.get() to wait for the user to input another character by pressing enter again. Those should go just before the return 0.

Popular pages Recent additions subscribe to a feed