Programs with if statements not working

From: Ken (pianoqst_at_aol.com)
Date: 01/06/04


Date: 5 Jan 2004 16:26:22 -0800

Hi,
    I wonder if anyone can tell me why the following listings from
SAMS Teach Yourself C++ in 21 Days do not work. They ask for the input
and after I type it in the program finishes without going through the
if statements. I tried three separate listings, all with if statements
and none of them work.

// Listing 4.4 - demonstrates if statement used with relational
operators
#include <iostream>
int main ()
{
    using std::cout;
    using std::cin;
    
    int MetsScore, YankeesScore;
    cout << "Enter the score for the Mets: ";
    cin >> MetsScore;
    
    cout << "\nEnter the score fo the Yankees: ";
    cin >> YankeesScore;
    
    cout << "\n";
    
    if (MetsScore > YankeesScore)
        cout << "Let's Go Mets!\n";
        
    if (MetsScore < YankeesScore)
    {
        cout << "Go Yankees!\n";
    }
     
     if (MetsScore == YankeesScore)
     {
      cout << "A tie? Naah, can't be.\n";
      cout << "Give me the real score for the Yanks: ";
      cin >> YankeesScore;
      
      if (MetsScore > YankeesScore)
        cout << "Knew it! Let's Go Mets!\n";
        
      if (YankeesScore > MetsScore)
        cout << "Knew it! Go Yanks!";
       
      if (YankeesScore == MetsScore)
        cout << "Wow, it really was a tie!";
      }
      
      cout << "\nThanks for telling me.\n";
      cin.get ();
      return 0;
}
       
*************************************************************
// Listing 4.6
#include <iostream>
int main()
{
  using namespace std;
  
  int firstNumber, secondNumber;
  cout << "Enter two numbers.\nFirst: ";
  cin >> firstNumber;
  cout << "\nSecond: ";
  cin >> secondNumber;
    cout << "\n\n";
    
  if (firstNumber >= secondNumber)
  {
    if ( (firstNumber % secondNumber) == 0)
    {
        if (firstNumber == secondNumber)
           cout << "They are the same!\n";
        else
           cout << "They are evenly divisible!\n";
     }
     else
        cout << "They are not evenly divisible!\n";
   }
   else
      cout << "Hey! The second one is larger!\n";
  return 0;
}

*********************************************************
//Listing 4.8
#include <iostream>
int main()
{
  using namespace std;
  
  int x;
  cout << "Enter a number less than 10 or greater than 100: ";
  cin >> x;
  cout << "\n";
    
  if (x >= 10)
  {
    if (x > 100)
        cout << "More than 100, Thanks!\n";
     }
     else
        cout << "Less than 10, Thanks!\n";
  cin.get ();
  return 0;
}

Thanks for any help,
Ken



Relevant Pages