Results 1 to 4 of 4

Thread: Help with my C++ code

  1. #1
    Join Date
    Apr 2010
    Location
    Rogers, Arkansas
    Posts
    13
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Post Help with my C++ code

    I'm learning c++ and I'm using a book and what not, but I can't get one of the projects to compile have been looking for what's wrong but for the life of me I can't figure it out.
    Code:
    	// Listing 4.4 - A complex, nested if statement
    	#include <iostream>
    	
    	int main()
    	{
    		// Ask for two numbers
    		// Assign the numbers to bigNumber and littleNumber
    		// If bigNumber is bigger than littleNumber,
    		// see if they are evenly divisible
    		// if they are, see if they are the same number
    		
    		int firstNumber, secondNumber;
    		std::cout << "Enter two numbers.\nFirst: ";
    		std::cin >> firstNumber;
    		std::cout << "\nSecond: ";
    		std::cin >> secondNumber;
    		std::cout << "\n\n";
    		
    		if (firstNumber % secondNumber)
    		{
    			if ( (firstNumber % secondNumber) == 0) // evenly divisible?
    				std::cout << "They are the same!\n";
    			else
    				std::cout << "They are evenly divisible!\n";
    		}
    		else
    			std::cout << "They are not evenly divisible:\n";
    	}
    	else
    		std::cout << "Hey! The second one is larger!\n";
    	return 0;
    }
    Here is the compiler error
    nestedif.cpp:29: error: expected unqualified-id before ‘else’
    nestedif.cpp:31: error: expected unqualified-id before ‘return’
    nestedif.cpp:32: error: expected declaration before ‘}’ token

    Any help would be greatly appreciated!
    Last edited by Syronix; 04-10-2010 at 10:05 AM. Reason: Forgot to add compile errors =O

  2. #2
    Join Date
    Dec 2008
    Posts
    209
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    The problem is that you've got a random 'else' statement, and the code that you've got isn't really correct.... for example:
    Code:
    if ( (firstNumber % secondNumber) == 0) // evenly divisible?
      std::cout << "They are the same!\n";
    that's not correct, you even say that you're checking whether they are evenly divisible in your comment.

    added a few more things for you to learn:

    Code:
    #include <iostream>
    
    int main() {
      int a, b;
      
      std::cout << "Enter two numbers." << std::endl << std::endl;
      std::cout << "First: ";
      std::cin >> a;
      
      std::cout << "Second: ";
      std::cin >> b;
      
      if (!a % b)  // 0 is classed as a falsy, so if the answer is 0, !false = true, so this will be evaluated
        std::cout << "They are evenly divisible!";
      else if (a > b)
        std::cout << "a is greater than b!";
      else if (a < b)
        std::cout << "b is greater than a!";
      else
        std::cout << "They are the same!"
        
      return 0;
    }
    ~ Craig`

  3. #3
    Join Date
    Apr 2010
    Location
    Rogers, Arkansas
    Posts
    13
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    woohoo finally it makes sense ty ty ty ty

  4. #4
    Join Date
    Feb 2008
    Location
    S
    Posts
    57
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Code:
    // Listing 4.4 - A complex, nested if statement
    #include <iostream>
    
    using namespace std;
    	
    int main(int argc, char *argv[]) {
    	// Ask for two numbers
    	// Assign the numbers to bigNumber and littleNumber
    	// If bigNumber is bigger than littleNumber,
    	// see if they are evenly divisible
    	// if they are, see if they are the same number
    		
    	int firstNumber, secondNumber;
    	cout << "Enter two numbers.\nFirst: ";
    	cin >> firstNumber;
    	cout << "\nSecond: ";
    	cin >> secondNumber;
    	cout << "\n\n";
    		
    	if((firstNumber % secondNumber) == 0) {
    		cout << "They are evenly divisible!";
    	} else if(firstNumber > secondNumber) {
    		cout << "The first number is greater than the second number!";
    	} else if(firstNumber < secondNumber) {
    		cout << "The first number is smaller than the second number!";
    	} else {
    		cout << "They are the same!";
    	}	
    
    	return 0;
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •