Compile and Test the Program
Now that we have completed our C++ program, it's time to compile and run it. Compiling converts our human-readable code into a machine-executable program.
Compile the Program
Open a terminal in the WebIDE by clicking on "Terminal" in the menu and selecting "New Terminal".
In the terminal, navigate to the project directory:
cd ~/project
Compile the program using the g++ compiler:
g++ main.cpp -o perfect_square_checker
This command tells the compiler to:
- Take our source file
main.cpp
- Compile it into an executable named
perfect_square_checker
If the compilation is successful, you will not see any output. If there are errors, read the error messages, fix the issues in your code, and try compiling again.
Run the Program
After successfully compiling the program, run it using:
./perfect_square_checker
The program will start and prompt you to enter a positive integer.
Let's test our program with different inputs to verify it works correctly:
Test Case 1: A Perfect Square
Enter 16 when prompted. You should see output similar to:
Please enter a positive integer: 16
The number 16 is a perfect square!
It is equal to 4 × 4
Thank you for using the Perfect Square Checker Program!
Test Case 2: Not a Perfect Square
Run the program again and enter 10 when prompted. You should see output similar to:
Please enter a positive integer: 10
The number 10 is not a perfect square.
Thank you for using the Perfect Square Checker Program!
Test Case 3: Another Perfect Square
Run the program again and enter 25 when prompted. You should see output similar to:
Please enter a positive integer: 25
The number 25 is a perfect square!
It is equal to 5 × 5
Thank you for using the Perfect Square Checker Program!
By testing with different inputs, you can verify that your program correctly identifies perfect squares and non-perfect squares.
Congratulations! You have successfully created a C++ program that determines whether a number is a perfect square.