【问题标题】:int : redefinition (C++)int : 重新定义 (C++)
【发布时间】:2015-07-26 02:32:36
【问题描述】:
#include <iostream> 
#include <stdio.h>
#include <string>

using namespace std;

int x, y;
int main()

{

    cout << "Please give me a number:";
    int x = (cin, x);

    cout << "Please give me another number:";
    int y = (cin, y);

    cout << "The sum of " << x;
    cout << "and " << y;
    cout << "is " << x+y;
}

谁能告诉我为什么(就这么简单)这没有添加? 我不太确定如何返回用户输入的数字等。刚开始学这个。

【问题讨论】:

    标签: c++ add calculator redefinition


    【解决方案1】:

    我相信而不是这个:

    int x = (cin, x);
    

    你想要这个:

    cin >> x;
    

    cin (console input) 与cout (console out) 的工作方式几乎相同put),你使用得当。

    您可能想了解更多信息:

    另外,您不需要在main() 中重新定义xy,因为它们是全局变量。

    【讨论】:

    • 我想说你不需要将xy 定义为全局变量;它们仅用于main()
    • @RogerLipscombe 涉及大量使用全局变量的不良编码实践是完全不同的方面。不过,好点子。
    【解决方案2】:

    正确的代码是:

    #include <iostream> // for cin,cout we use iostream
    #include <stdio.h> // you don't need this header file in this program
    #include <string> // also you don't need this header
    
     using namespace std;
    
     int main()
     {
       int x,y;
       cout<<"Please give me a number : ";
       cin>>x;
       cout<<"Please give me another number : ";
       cin>>y;
       cout<<"The sum of "<<x<<" and "<< y<<" is "<<x+y;
       return 0;
     }
    

    阅读Basic_Syntax

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多