我的 cout 上有一个奇怪的输出,它把答案放在第一位,然后在我调用它的地方放一个奇怪的输出.我该怎么办?
I am getting a weird output on my cout, it puts the answer first then puts a weird output where I call it. What do I do?
#include <iostream>
using namespace std;
//MainFunctions
int computeDiscount(int);
int mainProgram();
int main()
{
mainProgram();
}
int computeDiscount(int total)
{
if (total >= 10 && total <= 19)
{
total = (total-(total * .2));
cout << total;
}
}
int mainProgram()
{
int t;
cout << "What is the total amount for today? " << endl << ">>>";
cin >> t;
cout << "The total is: " << computeDiscount(t);
}
Output:
What is the total amount for today?
10
7The total is: 5007456
我该怎么办?我想让七个去"5007456"出现的地方
如果我把函数放在 cout 之外,它可以工作......不确定
我想在 cout 中调用函数
这是因为您实际上并没有从computeDiscount
函数返回计算值。由于未定义的行为,您在字符串"The total is: "
之后看到的是垃圾号码。隐式返回的值的性质是不确定的。g++
添加一个额外的标志-Wall
可以让您捕获该警告([-Wreturn-type]
(
该函数应该返回如下值
int computeDiscount(int total)
{
if (total >= 10 && total <= 19)
{
total = (total-(total * .2));
}
return total;
}
同样,您应该在末尾有一个mainProgram()
的返回值,以指定程序的成功终止。
另请参阅为什么要避免在程序中using namespace std
。请参阅为什么using namespace std;
被视为不良做法?。同样在C++中,您完全可以将total
变量作为引用传递并对其进行操作。类似的东西
void computeDiscount(int&);
void computeDiscount(int& total)
{
if (total >= 10 && total <= 19)
{
total = (total-(total * .2));
}
}
int mainProgram()
{
int t;
std::cout << "What is the total amount for today? " << std::endl << ">>>";
std::cin >> t;
computeDiscount(t);
std::cout << "The total is: " << t << std::endl;
return 0;
}
int computeDiscount(int total)
{
if (total >= 10 && total <= 19)
{
total = (total-(total * .2));
cout << total;
}
}
在此方法中,您不会返回任何假设存在的整数,并且在 main 函数中,您正在尝试打印输出,这将为您提供垃圾值。
#include <iostream>
using namespace std;
//MainFunctions
int computeDiscount(int);
int mainProgram();
int main()
{
mainProgram();
}
void computeDiscount(int *total)
{
if (*total >= 10 && *total <= 19)
{
*total = (*total-(*total * .2));
}
}
int mainProgram()
{
int t;
cout << "What is the total amount for today? " << endl << ">>>";
cin >> t;
computeDiscount(&t);
cout << "The total is: " << t;
return 0;
}
或者你可以传递对函数的引用
代替cout << total;
是return total;
int computeDiscount(int total)
{
if (total >= 10 && total <= 19)
{
total = (total-(total * .2));
return total;
}
}
并正确地执行,即采用最具 C/C++ 优势的功能
int computeDiscount(int total)
{
if (total >= 10 && total <= 19)
return total -= total / 5 ;
return
}
在函数 computeDiscount(int&( 中使用引用传递
int computeDiscount(int&);
int computeDiscount(int& total)
{
if (total >= 10 && total <= 19)
{
total = (total-(total * .2));
cout << total;
}
}
相关文章:
- 当使用通配符和null指针调用函数时,对输出的说明
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 使用静态变量的递归调用的不同输出
- cmake add_custom_command + Xcode:多输出 = 多命令调用
- 调用 cout 时如何在结构中输出常量文本?
- 我的 cout 上有一个奇怪的输出,它把答案放在第一位,然后在我调用它的地方放一个奇怪的输出.我该怎么办?
- 函数调用导致没有输出
- 使用 Node.js N-API 调用 C 函数时,不会显示预期的输出
- 如何调用继承的重载运算符<<并在派生类的输出中添加更多文本?
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 当我从头文件和实现文件调用我的函数到我的主文件时,我没有得到任何输出
- 空指针(为缺少的输出数组调用的 create() 在 create,文件 /home/m/OpenCV/modules/
- 为什么在杂注 omp 关键之后多次调用 printf 会产生乱码输出?
- std::cout 如果从自定义分配器 (Visual Studio 2019) 调用,则不会输出
- 将 Linux system() 调用命令的输出重定向到仅可变的
- 干净取消在输入/输出调用中被阻止的 std::线程
- 不同的输出调用clang上的malloc应用编译器选项-00和-03
- 从缺少 printf 语句输出C++调用 C API
- 如何使用 Matlab 的多个输出调用C++函数
- 无法使用重定向标准输出调用 g++