我是C ++语言的新手,正在上课。我有一个需要三个函数的编程任务。
功能一是显示菜单并获取用户数据。
功能二是根据用户数据的选择来计算用户数据。
功能三是输出数据。
我一直在尝试按照教程,但我没有抓住它。
这是我的代码:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
double userNumber = 0;
double calcNum_one = 0;
double calcNum_two = 0;
void menu();
double calculator(double, double);
void solution();
int main() {
do {
menu();
calculator(answer);
solution();
}
while (userNumber != 7);
return 0;
}
void menu () {
cout << "Welcome to the CIS 151 calculator." << endl;
cout << "To make a selection, enter the number and press enter." << endl;
cout << " 1. Add two numbers." << endl;
cout << " 2. Subtract one number from another." << endl;
cout << " 3. Multiply two numbers." << endl;
cout << " 4. Divide two numbers." << endl;
cout << " 5. Modulus of two numbers." << endl;
cout << " 6. Raise a number to a power." << endl;
cout << " 7. Quit the program." << endl;
cout << endl;
cout << "Number: ";
cin >> userNumber;
}
void calculator(double calcNum_one, double calcNum_two) {
if(userNumber == 1) {
system("CLS");
cout << "You have chosen to: add two numbers." << endl;
cout << "Please type in your numbers." << endl;
cout << endl;
cout << "First Number: ";
cin >> calcNum_one;
cout << "Second Number: ";
cin >> calcNum_two;
double answer = calcNum_one + calcNum_two;
return answer;
}
}
void solution() {
if(userNumber == 1) {
system("CLS");
cout << "The sum of " << calcNum_one << " and " << calcNum_two << " is " << answer << "." << endl;
cout << endl;
system("PAUSE");
}
}
注意:我只是将if语句放在一个可能的位置,以便于阅读。
有人可以清理我的代码以显示我如何正确使用计算器功能吗?我在CodeBlocks中获得error: 'answer' was not declared in this scope
。
谢谢大家!
答案 0 :(得分:1)
answer
未声明,因此无法使用。
您决定使用全球视觉效果,因此请始终如一地使用它们。
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
double userNumber = 0;
double calcNum_one = 0;
double calcNum_two = 0;
double answer = 0;
void menu();
void calculator();
void solution();
int main() {
do {
menu();
calculator();
solution();
}
while (userNumber != 7);
return 0;
}
void menu () {
cout << "Welcome to the CIS 151 calculator." << endl;
cout << "To make a selection, enter the number and press enter." << endl;
cout << " 1. Add two numbers." << endl;
cout << " 2. Subtract one number from another." << endl;
cout << " 3. Multiply two numbers." << endl;
cout << " 4. Divide two numbers." << endl;
cout << " 5. Modulus of two numbers." << endl;
cout << " 6. Raise a number to a power." << endl;
cout << " 7. Quit the program." << endl;
cout << endl;
cout << "Number: ";
cin >> userNumber;
}
void calculator() {
if(userNumber == 1) {
system("CLS");
cout << "You have chosen to: add two numbers." << endl;
cout << "Please type in your numbers." << endl;
cout << endl;
cout << "First Number: ";
cin >> calcNum_one;
cout << "Second Number: ";
cin >> calcNum_two;
answer = calcNum_one + calcNum_two;
}
}
void solution() {
if(userNumber == 1) {
system("CLS");
cout << "The sum of " << calcNum_one << " and " << calcNum_two << " is " << answer << "." << endl;
cout << endl;
system("PAUSE");
}
}
当然你不应该使用全局变量。某些人也憎恨using namespace std;
甚至using
。
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>
#include <cmath>
double menu();
double calculator(double&, double&, double);
void solution(double, double, double, double);
int main() {
double userNumber;
double calcNum_one;
double calcNum_two;
double answer;
do {
userNumber = menu();
answer = calculator(calcNum_one, calcNum_two, userNumber);
solution(calcNum_one, calcNum_two, answer, userNumber);
}
while (userNumber != 7);
return 0;
}
double menu () {
double userNumber;
std::cout << "Welcome to the CIS 151 calculator." << std::endl;
std::cout << "To make a selection, enter the number and press enter." << std::endl;
std::cout << " 1. Add two numbers." << std::endl;
std::cout << " 2. Subtract one number from another." << std::endl;
std::cout << " 3. Multiply two numbers." << std::endl;
std::cout << " 4. Divide two numbers." << std::endl;
std::cout << " 5. Modulus of two numbers." << std::endl;
std::cout << " 6. Raise a number to a power." << std::endl;
std::cout << " 7. Quit the program." << std::endl;
std::cout << std::endl;
std::cout << "Number: ";
std::cin >> userNumber;
return userNumber;
}
double calculator(double& calcNum_one, double& calcNum_two, double userNumber) {
double answer = 0;
if(userNumber == 1) {
system("CLS");
std::cout << "You have chosen to: add two numbers." << std::endl;
std::cout << "Please type in your numbers." << std::endl;
std::cout << std::endl;
std::cout << "First Number: ";
std::cin >> calcNum_one;
std::cout << "Second Number: ";
std::cin >> calcNum_two;
answer = calcNum_one + calcNum_two;
}
return answer;
}
void solution(double calcNum_one, double calcNum_two, double answer, double userNumber) {
if(userNumber == 1) {
system("CLS");
std::cout << "The sum of " << calcNum_one << " and " << calcNum_two << " is " << answer << "." << std::endl;
std::cout << std::endl;
system("PAUSE");
}
}