Home > Net >  What should I do with this error on input? Can you also suggest how to make my code better, I conver
What should I do with this error on input? Can you also suggest how to make my code better, I conver

Time:01-27

There is an error in this line

cin >> X >> Y;

This is the function it belongs into

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

Below is the whole code

#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);
}

See what's on the terminal

See what's on the terminal

------------------------------------------------------------ Below is the original code in C language

#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!\n\n");
}

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 %d\n",X,Y,Sum);
}


int ComputeSUM(int X, int Y)
{
  int Sum;

  Sum=X Y;                 //return(X Y)

  return(Sum);
}

CodePudding user response:

Change this:

void InputData(int *X,int *Y)

To this:

void InputData(int &X,int &Y)

And this:

InputData(&X1,&X2)

To this:

InputData(X1,X2)

And read about 'Passing By Pointer Vs Passing By Reference' for better understanding.

CodePudding user response:

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

  •  Tags:  
  • Related