简体   繁体   English

Printf打印错误的字符和0

[英]Printf printing wrong char and 0

Sorry if this is a stupid question, I'm new to C++. 抱歉,如果这是一个愚蠢的问题,我是C ++的新手。 Why is it not copying all of the input to the output correctly? 为什么不能将所有输入正确复制到输出?

#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

using namespace std;

int main() {
    int num1;
    long num2;
    long long num3;
    char char1;
    float num4;
    double num5;
    scanf("%d %ld %lld %c %f %lf", &num1, &num2, &num3, &char1, &num4, &num5);
    //input: 211916801 452082285 97592151379235457 p 19856.992 -5279235.721231465
    printf("%d %ld %lld %c %f %lf", num1, num2, &num3, &char1, &num4, &num5);
    //expected output: 211916801 452082285 97592151379235457 p 19856.992 -5279235.721231465
    //actual output: 211916801 452082285 68674564278975280 c 0.000000 0.000000
    system("pause");
    return 0;
}

Because you have & for some of the variables which is not what you intended. 因为您有&的某些变量不是您想要的。

printf("%d %ld %lld %c %f %lf", num1, num2, &num3, &char1, &num4, &num5);

should be 应该

printf("%d %ld %lld %c %f %lf", num1, num2, num3, char1, num4, num5);

Your compiler should have warned about this as the values you pass don't match the format specifiers. 编译器应该对此有所警告,因为您传递的值与格式说明符不匹配。 If not, turn on/increase the compiler warnings. 如果不是,请打开/增加编译器警告。

您应该将值本身而不是指针传递给printf

printf("%d %ld %lld %c %f %lf", num1, num2, num3, char1, num4, num5);

In Printf(in most cases) you don't use & in your variables. 在Printf中(大多数情况下),您不在变量中使用&。

printf("%d %ld %lld %c %f %lf", num1, num2, num3, char1, num4, num5);

Plus, if you're new to C++, there's a better way to perform I/O Operations. 另外,如果您不熟悉C ++,则有更好的方法来执行I / O操作。 Use "cin" instead of "scanf" and "cout" instead of "printf" 使用“ cin”代替“ scanf”,并使用“ cout”代替“ printf”

#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

using namespace std;

int main() {
    int num1;
    long num2;
    long long num3;
    char char1;
    float num4;
    double num5;

   //scanf("%d %ld %lld %c %f %lf", &num1, &num2, &num3, &char1, &num4, &num5);
   cin >> num1;
   cin >> num2;
   cin >> num3;
   cin >> char1;
   cin >> num4;
   cin >> num5;
   //input: 211916801 452082285 97592151379235457 p 19856.992 -5279235.721231465

   //printf("%d %ld %lld %c %f %lf", num1, num2, &num3, &char1, &num4, &num5);
   cout << num1;
   cout << num2;
   cout << num3;
   cout << char1;
   cout << num4;
   cout << num5;

   //expected output: 211916801 452082285 97592151379235457 p 19856.992 -5279235.721231465
   //actual output: 211916801 452082285 68674564278975280 c 0.000000 0.000000

  system("pause");
  return 0;
}

While using printf(...) , primitive types are passed by value not by reference. 使用printf(...) ,原始类型按值传递,而不是按引用传递。 Use below code: 使用以下代码:

#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

using namespace std;

int main() {
    int num1;
    long num2;
    long long num3;
    char char1;
    float num4;
    double num5;
    scanf("%d %ld %lld %c %f %lf", &num1, &num2, &num3, &char1, &num4, &num5);
    printf("%d %ld %lld %c %f %lf", num1, num2, num3, char1, num4, num5);
    system("pause");
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM