Syronix
04-10-2010, 10:04 AM
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.
// 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!
// 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!