int : redefinition (C++)

int : redefinition (C++)

本文关键字:C++ redefinition int      更新时间:2023-10-16
#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;
}

谁能告诉我为什么(尽管简单)这没有添加?我真的不确定如何返回用户输入的数字和类似的东西。刚开始学这个

我相信,而不是这样:

int x = (cin, x);

你想要这个:

cin >> x;

cin (console input)的工作方式与cout (console output)几乎相同,您可以正确使用。

你可能想要更多地了解他们:

  • std:: cout
  • std:: cin

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

正确代码为:

#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