我应该如何处理输入时的这个错误?你能不能也建议一下如何让我的代码变得更好,我把它从C转换成了C++



此行中有错误

cin >> X >> Y;

这是它属于的功能

void InputData(int *X,int *Y)
{
cout << "Enter 2 integer values: ";
cin >> X >> Y;
}

以下是的完整代码

#include <iostream>
using namespace std;
void Message();
void InputData(int *X, int *Y);
void OutputData(int X, int Y, int Sum);
int ComputeSUM(int X, int Y);
int main()
{
int X1,X2,SUM;
Message();
InputData(&X1,&X2);
SUM=ComputeSUM(X1,X2);
OutputData(X1,X2,SUM);
return 0;
}
void Message()
{
cout << "This program computes and displays SUM of 2 integer values!" << endl <<endl;
}
void InputData(int *X,int *Y)
{
cout << "Enter 2 integer values: ";
cin >> X >> Y;
}
void OutputData(int X, int Y, int Sum)
{
cout << "The SUM of " << X << " and " << Y << " is " << Sum << endl;
}

int ComputeSUM(int X, int Y)
{
int Sum;
Sum=X+Y;                 //return(X+Y)
return(Sum);
}

查看终端上的内容

查看终端上的内容

------------------------------------------------------------以下是C语言的原始代码

#include <stdio.h>
#include <conio.h>
void Message();
void InputData(int *X, int *Y);
void OutputData(int X, int Y, int Sum);
int ComputeSUM(int X, int Y);
int main()
{
int X1,X2,SUM;
clrscr();
Message();
InputData(&X1,&X2);
SUM=ComputeSUM(X1,X2);
OutputData(X1,X2,SUM);
getch();
return(0);
}
void Message()
{
printf("This program computes and displays SUM of 2 integer values!nn");
}
void InputData(int *X,int *Y)
{
printf("Enter 2 integer values; ");
scanf("%d%d",X,Y);
}
void OutputData(int X, int Y, int Sum)
{
printf("The SUM of %d and %d is %dn",X,Y,Sum);
}

int ComputeSUM(int X, int Y)
{
int Sum;
Sum=X+Y;                 //return(X+Y)
return(Sum);
}

更改此项:

void InputData(int *X,int *Y)

对此:

void InputData(int &X,int &Y)

这个:

InputData(&X1,&X2)

对此:

InputData(X1,X2)

阅读"指针传递与引用传递"以更好地理解。

cin>gt;X>gt;Y->cin>gt*X>gt*Y

最新更新