C++ number manipulator programer

gaille

New Member
#1
I'm a beginner at C++ and function, Been getting the error "linker command failed with exit code 1" wondering anyone could give me some insight on how to resolve this problem, and on my overall code organization. Thank you very much for your time and opinion on this matter :)

ignore the below statement

(Due to lack of space I had to remove the #include statements which included the following iostream, iomanip, string and cmath).
Mã:
#include<iostream>
#include<iomanip>
#include<string>
#include<cmath>
void displayHeader();
double getNumber();
int showMenu();
void menu();
void processMenuChoice();
void isPosNeg();
int squareRoot();
void isOddEven();
void findNumDigits();
void findDigitAtPosition();   
using namespace std;
int main()
{
    int choice = 0;
    displayHeader();
    getNumber();
    menu();
    showMenu();
    processMenuChoice();
    while (choice != 0) {
        processMenuChoice();}
    return 0;
}
void displayHeader(){
    cout << "Number Manipulator\n"; }
double getNumber() {
    double num;
    cout << "Enter a number to continue: ";
    cin >> num;
    return num; }
void menu(){
    cout << "\nHere are your choices:";
    cout << endl << setw(29) << "1) Is it even or odd?";
    cout << endl << setw(38) << "2) Is it positive or negative?";
    cout << endl << setw(49) << "3) What is the square root?";
    cout << endl << setw(41) << "4) How many digits in the number?";
    cout << endl << setw(54) << "5) What is the digit at a particular location?";
    cout << endl << setw(19) << "0) To Quit.";
}
int showMenu() {
    int choice;
    cout << "\n\nEnter your choice: ";
    cin >> choice;
    return choice; }
void processMenuChoice(int choice) {
    switch (choice) {
        case 1: isPosNeg();
            break;
        case 2: isOddEven();
            break;
        case 3: squareRoot();
            break;
        case 4: findNumDigits();
            break;
        case 5: findDigitAtPosition();
            break;
        default:
            cout << "This is not a valid choice. Please try again!";
            showMenu();
            break; }}
void isPosNeg(int num) {
    if (num > 0) cout << "Number is Positive\n";
    if (num < 0) cout << "Number is Negative\n";
    if (num == 0) cout << "Number is Zero\n"; }
void isOddEven(int num) {
    if (num%2 == 0) cout << "Number is even";
    else cout << "Number is odd"; }
void squareRoot(int num) {
    int numSqrt;
    numSqrt=sqrt(num);
    cout << "Square root of " << num << " is " << numSqrt; }
void findNumDigits(int num) {
    int numDigits=0;
    do {
        num /= 10;
        numDigits++;
    } while(num);
    cout << "The number of digits in " << num << " is " << numDigits; }
void findDigitAtPosition(int num) {
    int pos;
    cout << "What Position?: ";
    cin >> pos; }
    //need to be completed
 

Admin

Administrator
Thành viên BQT
#2
Here is the fix to make your code work. Note, there are some other issues that you need to fix.

Hint: in void squareRoot(int num), the result is an integer...

Mã:
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
void displayHeader();
double getNumber();
int showMenu();
void menu();
void processMenuChoice(int choice, int num);
void isPosNeg(int num);
void squareRoot(int num);
void isOddEven(int num);
void findNumDigits(int num);
void findDigitAtPosition(int num);   
using namespace std;
int main()
{
    int choice = 0;
    displayHeader();
    int num = getNumber();
    menu();
    choice = showMenu();
    while (choice != 0) {
        processMenuChoice(choice, num);
        choice = showMenu();
    }
    return 0;
}
void displayHeader(){
    cout << "Number Manipulator\n"; }
double getNumber() {
    double num;
    cout << "Enter a number to continue: ";
    cin >> num;
    return num; }
void menu(){
    cout << "\nHere are your choices:";
    cout << endl << setw(29) << "1) Is it even or odd?";
    cout << endl << setw(38) << "2) Is it positive or negative?";
    cout << endl << setw(49) << "3) What is the square root?";
    cout << endl << setw(41) << "4) How many digits in the number?";
    cout << endl << setw(54) << "5) What is the digit at a particular location?";
    cout << endl << setw(19) << "0) To Quit.";
}
int showMenu() {
    int choice;
    cout << "\n\nEnter your choice: ";
    cin >> choice;
    return choice; }
void processMenuChoice(int choice, int num) {
    switch (choice) {
        case 1: isPosNeg(num);
            break;
        case 2: isOddEven(num);
            break;
        case 3: squareRoot(num);
            break;
        case 4: findNumDigits(num);
            break;
        case 5: findDigitAtPosition(num);
            break;
        default:
            cout << "This is not a valid choice. Please try again!";
            showMenu();
            break; }}
void isPosNeg(int num) {
    if (num > 0) cout << "Number is Positive\n";
    if (num < 0) cout << "Number is Negative\n";
    if (num == 0) cout << "Number is Zero\n"; }
void isOddEven(int num) {
    if (num%2 == 0) cout << "Number is even";
    else cout << "Number is odd"; }
void squareRoot(int num) {
    int numSqrt;
    numSqrt=sqrt(num);
    cout << "Square root of " << num << " is " << numSqrt; }
void findNumDigits(int num) {
    int numDigits=0;
    do {
        num /= 10;
        numDigits++;
    } while(num);
    cout << "The number of digits in " << num << " is " << numDigits; }
void findDigitAtPosition(int num) {
    int pos;
    cout << "What Position?: ";
    cin >> pos; }
    //need to be completed
 
Top