C ++编号操纵器程序员
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C ++编号操纵器程序员相关的知识,希望对你有一定的参考价值。
我是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 << "
Here 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
答案
在快速查看代码的同时,我注意到函数processMenuChoice被声明为void processMenuChoice()
,但它被定义为void processMenuChoice(int choice)
。由于函数需要paramater,因此需要将while循环更改为
processMenuChoice(choice);
while(choice != 0)
{
processMenuChoice(choice);
}
另一答案
这是使您的代码工作的修复程序。请注意,您还需要解决一些其他问题。
提示:在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
"; }
double getNumber() {
double num;
cout << "Enter a number to continue: ";
cin >> num;
return num; }
void menu(){
cout << "
Here 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 << "
Enter 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
";
if (num < 0) cout << "Number is Negative
";
if (num == 0) cout << "Number is Zero
"; }
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
以上是关于C ++编号操纵器程序员的主要内容,如果未能解决你的问题,请参考以下文章