C++ 为什么我尝试在一个if语句中调用两个function,但在第一个if语句完成后它会忽略另一个函数?(C+;+;)
我已经在墙上敲了几个小时的头,仔细研究线程和视频,试图找出一些关于这段代码的东西。除非我在if语句中调用加密和解密函数之前输入“int”,否则它不会编译。但是,如果我这样做,程序将在到达加密/解密部分之前终止,并表现得好像它不存在一样。否则,它会给我诸如“未定义对encrpyt()的引用;”之类的错误。我已经阅读了关于调用函数的指南,但似乎我不能让它工作。我错过了什么C++ 为什么我尝试在一个if语句中调用两个function,但在第一个if语句完成后它会忽略另一个函数?(C+;+;),c++,C++,我已经在墙上敲了几个小时的头,仔细研究线程和视频,试图找出一些关于这段代码的东西。除非我在if语句中调用加密和解密函数之前输入“int”,否则它不会编译。但是,如果我这样做,程序将在到达加密/解密部分之前终止,并表现得好像它不存在一样。否则,它会给我诸如“未定义对encrpyt()的引用;”之类的错误。我已经阅读了关于调用函数的指南,但似乎我不能让它工作。我错过了什么 #include <iostream> #include <stdio.h> #include <
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int readNumber();
int encrypt();
int decrypt();
char choice;
int main()
{
cout << endl;
cout << "Please select one of the following: " << endl;
cout << "1. Encrypt a number " << endl;
cout << "2. Decrypt a number " << endl;
cout << endl;
cout << "Enter choice: ";
cin >> choice;
cout << endl;
if (choice == '1')
{
readNumber();
encrypt();
}
else if (choice == '2')
{
readNumber();
decrypt();
}
else
{
cout << "Invalid choice. Try again. " << endl;
cout << endl;
}
}
int readNumber()
{
int number;
cout << "Enter the four digit number: ";
cin >> number;
if (number > 9999)
{
cout << endl;
cout << "Please input a four digit, non-negative number. " << endl;
cout << endl;
return readNumber();
}
cout << endl;
return 0;
}
int encrypt(int number)
{
int Num1,Num2,Num3,Num4;
Num1 = number / 1000;
Num2 = number / 100 - 10*Num1;
Num4 = number%10;
Num3 = (number%100 - Num4) / 10;
cout << "The encrypted number is: "
<< (Num4 + 7)%10
<< (Num3 + 7)%10
<< (Num2 + 7)%10
<< (Num1 + 7)%10
<< endl;
return 0;
}
int decrypt(int number)
{
int Num1,Num2,Num3,Num4;
Num1 = number / 1000;
Num2 = number / 100 - 10*Num1;
Num4 = number%10;
Num3 = (number%100 - Num4) / 10;
cout << "The decrypted number is: "
<< (Num1 + 3)%10
<< (Num2 + 3)%10
<< (Num3 + 3)%10
<< (Num4 + 3)%10
<< endl;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int readNumber();
int encrypt();
int decrypt();
字符选择;
int main()
{
cout代码不能完全生成程序,因为转发声明
using namespace std;
int readNumber();
int encrypt();
int decrypt();
...
不匹配下面实现的函数,以便编译器可以找到声明的转发
int encrypt();
int decrypt();
但是链接器只能找到
int encrypt(int number)
及
执行如下
但是
将编译,因为它不做任何操作。它声明函数而不是调用它们。它有效地重复了您在文件顶部所做的操作
你要申报吗
using namespace std;
int readNumber();
int encrypt(int number);
int decrypt(int number);
...
匹配下面实现的功能,然后
int toBeEncrypted = readNumber();
int encryptedNumber = encrypt(toBeEncrypted);
调用函数并使用它们返回的值
附录:检查所有函数中的返回值。您似乎总是返回零。您的声明函数加密和解密不带参数,使用/调用它们不带参数,但定义/实现函数带参数。因此,链接器会出错或者使用未定义的函数
当您在if
语句中的函数调用之前添加int
时,将函数调用替换为与main
之前相同的函数声明
也许你想要
// before main
int encrypt(int number);
int decrypt (int number);
...
// in if statements
int number = readNumber();
encrypt(number);
...
int number = readNumber();
decrypt(number);
把int
放在那里,你需要它来编译函数。至于“表现得好像encrypt
不存在”,那是因为你的程序正在崩溃。使用调试器来找出发生了什么(提示:看看这个返回0;
函数中的语句)。你需要注意编译器警告。它们会帮助你的。
int toBeEncrypted = readNumber();
int encryptedNumber = encrypt(toBeEncrypted);
// before main
int encrypt(int number);
int decrypt (int number);
...
// in if statements
int number = readNumber();
encrypt(number);
...
int number = readNumber();
decrypt(number);