为什么我要尝试在一个if语句中调用两个fucntions,但是在第一个完成后它忽略了另一个功能?(C )
Why do I try to call two fucntions in one if statement, but it ignores the other function after the first is completed? (C++)
我已经将头撞在墙上了几个小时,浏览了线程和视频,试图弄清楚有关此代码的一些内容。除非我在调用我的加密并解密我的if语句中调用" int"并解密功能之前,它不会编译。但是,如果我这样做,则该程序将在该程序到达加密/解密部分之前终止,并好像不存在。否则,它会给我诸如"未定义的eNCRPYT(("之类的错误。我已经阅读了有关调用函数的指南,但是看来我无法正常工作。我想念什么?
#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;
}
您的声明函数 encrypt
和 decrypt
没有参数,请使用/调用无参数参数。因此,您会获得带有未定义功能的链接器错误。
当您在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);
代码不会完全产生程序,因为远期声明
using namespace std;
int readNumber();
int encrypt();
int decrypt();
...
不匹配下面实现的函数,以便编译器可以找到远期指定
int encrypt();
int decrypt();
但是链接器只能找到
int encrypt(int number)
和
int decrypt(int number)
在下面实现。
但是
if (choice == '1')
{
int readNumber();
int encrypt();
}
会编译,因为它无能为力。它声明功能而不是调用它们。它有效地重复了您在文件顶部所做的工作
您想声明
using namespace std;
int readNumber();
int encrypt(int number);
int decrypt(int number);
...
匹配下面实现的功能,然后
int toBeEncrypted = readNumber();
int encryptedNumber = encrypt(toBeEncrypted);
调用函数并使用它们返回的值。
附录:检查所有功能中的返回值。您似乎总是返回零。
相关文章:
- 获取向量C++中第一个值和最后一个值的和
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 如何在创建自定义迭代器时获得 std::p air 的第一个和第二个?
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- WinAPI 在单击第一个对话框上的按钮控件并销毁第一个对话框后创建第二个对话框
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- 这个RLE解码功能只解码第一个字符,我做错了什么?
- 为什么我要尝试在一个if语句中调用两个fucntions,但是在第一个完成后它忽略了另一个功能?(C )
- C (我的代码卡在第一个功能中)
- 元功能获取模板列表的第一个模板
- 模板的C 功能将第一个参数设置为第二个参数为默认参数
- 用于存储由第一个功能创建的指针的设计模式将在以后使用
- 指向功能和std ::功能:第一个编译而不是第二个指针
- 第一个交换功能有什么问题?