Thread: loops with if else

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    loops with if else

    I almost have this working right....

    The program itself basically runs ok, but I want to try to get it to display a customized statement based on the input data.

    If there are >1 positive or negative value, the output statement should be in the plural form; if there is only 1 positive or negative value, the output should be singular. I put in the statements, but something is still wrong.

    Here's the part of the program this pertains to:

    Code:
    int calculations()
    {
    	int number;
    	int positiveValues;
    	int negativeValues;
    
    	for (int counter = 0; counter < 1; counter++)
    	{
    		positiveValues = 0;
    		negativeValues = 0;
    		do
    		{
    			cout << "Enter an integer (or zero to stop): ";
    			cin >> number;
    			
    			if (number > 0)
    			{
    				positiveValues++;
    			}
    			else if (number < 0)
    			{
    				negativeValues++;
    			}
    			else
    			{
    				cout << endl;
    			}
    
    		} while (number != 0);
    
    			if(positiveValues = 1)
    			{	
    				cout << "There was "
    					 << positiveValues
    					 << " positive value in your group of integers." << endl;
    			}
    			else if (negativeValues = -1)
    			{	
    				cout << "There was "
    						 << negativeValues
    						 << " negative value in your group of integers." << endl;
    			}
    			else
    				
    			cout << "There were " 
    				 << positiveValues 
    			     << " positive values in your group of integers." << endl;
    			cout << "There were " 
    			     << negativeValues 
    			     << " negative values in your group of integers." << endl << endl;
    
    	}
    	return 0;
    
    }
    any suggestions would be greatly appreciated.
    Thanks!!

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Code:
    int calculations()
    {
    	int number;
    	int positiveValues;
    	int negativeValues;
    
    	for (int counter = 0; counter < 1; counter++)
    	{
    		positiveValues = 0;
    		negativeValues = 0;
    		do
    		{
    			cout << "Enter an integer (or zero to stop): ";
    			cin >> number;
    			
    			if (number > 0)
    			{
    				positiveValues++;
    			}
    			else if (number < 0)
    			{
    				negativeValues++;
    			}
    			else
    			{
    				cout << endl;
    			}
    
    		} while (number != 0);
    
    			if(positiveValues ==  1)
    			{	
    				cout << "There was "
    					 << positiveValues
    					 << " positive value in your group of integers." << endl;
    			}
    			else if (negativeValues ==  1)
    			{	
    				cout << "There was "
    						 << negativeValues
    						 << " negative value in your group of integers." << endl;
    			}
    			else
    				{ 
    			cout << "There were " 
    				 << positiveValues 
    			     << " positive values in your group of integers." << endl;
    			cout << "There were " 
    			     << negativeValues 
    			     << " negative values in your group of integers." << endl << endl;
                           } 
    
    	}
    	return 0;
    
    }
    Last edited by pratip; 10-07-2003 at 03:16 AM.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Earlier code I have not compiled... This code is compiled successfully and it worked...

    Code:
    #include <iostream>
    using namespace std;
    
    int calculations()
    {
      int number;
      int positiveValues;
      int negativeValues;
    
      for (int counter = 0; counter < 1; counter++)
        {
          positiveValues = 0;
          negativeValues = 0;
          do
    	{
    	  cout << "Enter an integer (or zero to stop): ";
    	  cin >> number;
    			
    	  if (number > 0)
    	    {
    	      positiveValues++;
    	    }
    	  else if(number < 0)
    	    {
    	      negativeValues++;
    	    }
    	} while (number != 0);
    
          if(positiveValues == 1)
    	{	
    	  cout << "There was "
    	       << positiveValues
    	       << " positive value in your group of integers." << endl;
    	}
          else 
    	{
    	  cout << "There was "
    	       << positiveValues
    	       << " positive values in your group of integers." << endl;
    	}
          if (negativeValues == 1)
    	{	
    	  cout << "There was "
    	       << negativeValues
    	       << " negative value in your group of integers." << endl;
    	}
          else
    	{
    	  cout << "There were " 
    	       << negativeValues 
    	       << " negative values in your group of integers." << endl << endl;
    	}
        }
      return 0;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM