SRL Archive - C/C++ Help and Tutorials

Help with my C++ code


Syronix

2010-04-10 06:04:50

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 &#8216;else&#8217;
nestedif.cpp:31: error: expected unqualified-id before &#8216;return&#8217;
nestedif.cpp:32: error: expected declaration before &#8216;}&#8217; token

Any help would be greatly appreciated!

Craig`

2010-04-10 06:21:20

The problem is that you've got a random 'else' statement, and the code that you've got isn't really correct…. for example:

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:


#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` :)

Syronix

2010-04-10 06:35:01

woohoo finally it makes sense ty ty ty ty

trecool999

2010-04-10 06:52:18


// 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;
}