C ++ числовой манипулятор программист - PullRequest
0 голосов
/ 08 ноября 2018

Я новичок в C ++ и функции, Я получаю сообщение об ошибке "команда компоновщика завершилась неудачно с кодом выхода 1", интересно, кто-нибудь может дать мне некоторое представление о том, как решить эту проблему, и о моей общей организации кода. Большое спасибо за ваше время и мнение по этому вопросу :)

игнорировать приведенное ниже утверждение

(из-за недостатка места мне пришлось удалить операторы #include, в которые входили следующие iostream, iomanip, string и cmath).

#include<iostream>
#include<iomanip>
#include<string>
#include<cmath>
void header();
double number();
int menu();
void seeMenu();
void processChoice();
void negPos();
int squareRoot();
void evenOdd();
void numOfDigits();
void digitAtPos();
using namespace std;
int main()
{
    int choice = 0;
    header();
    number();
    menu();
    seeMenu();
    processChoice();
    while (choice != 0) {
        processChoice();}
    return 0;
}
void header(){
    cout << "Number Manipulator"<< endl; }
double number() {
    double num;
    cout << "Enter a number to continue: ";
    cin >> num;
    return num; }
int 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 << endl << endl <<"Enter your choice: ";
    cin >> choice;
    return choice; }
void processChoice(int choice) {
    switch (choice) {
        case 1: squareRoot();
            break;
        case 2: evenOdd();
            break;
        case 3: negPos();
            break;
        case 4: numOfDigits();
            break;
        case 5: digitAtPos();
            break;
        default:
            cout << "This is not a valid choice. Please try again!";
            showMenu();
        break; }}
void negPos(int num) {
    if (num > 0) cout << "Number is Positive"<< endl;
    if (num < 0) cout << "Number is Negative"<< endl;
    if (num == 0) cout << "Number is Zero"<< endl; }
void evenOdd(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 numOfDigits(int num) {
    int numDigits=0;
    do {
        num /= 10;
        numDigits++;
    } while(num);
    cout << "The number of digits in " << num << " is " << numDigits; }
void digitAtPos(int num) {
    int pos;
    cout << "What Position?: ";
    cin >> pos; }
//need to be completed

Ответы [ 2 ]

0 голосов
/ 08 ноября 2018

Вот исправление, заставляющее ваш код работать. Обратите внимание, что есть некоторые другие проблемы, которые вам нужно исправить.

Подсказка: в void squareRoot(int num) результат является целым числом ...

#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
0 голосов
/ 08 ноября 2018

Быстро просматривая ваш код, я заметил, что функция processMenuChoice объявлена ​​как void processMenuChoice(), но определена как void processMenuChoice(int choice). И поскольку функции нужен этот параметр, вам нужно изменить цикл while на

processMenuChoice(choice);
while(choice != 0)
{
    processMenuChoice(choice);
}
...