help me figure out error..

I am very new to c++.After running the program It showed null error. It asks for both the inputs ie., x1,y1 and x2,y2. But when I press enter for output it returns to the coding screen.

#include<iostream.h>
#include<stdlib.h>
#include<math.h>
int main*(
{
system("cls");
double distance,x1,y1,x2,y2;
cout<<Enter coordinates (x y) for point1:";
cin>>x1>>y1;
cout<<Enter coordinates (x y) for point2:";
cin>>x2>>y2;
distance=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
cout<<"The distance between two points is <<distance<<endl;"
return 0;
}

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
int main*(
{
system("cls");
double distance,x1,y1,x2,y2;
cout<<"Enter coordinates (x y) for point1:";
cin>>x1>>y1;
cout<<"Enter coordinates (x y) for point2:";
cin>>x2>>y2;
distance=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
cout<<"The distance between two points is <<distance<<endl;"
return 0;
}
Last edited on
The code still has syntax errors, such as on line 13.
Error compiling: Line 5 : Expected '(' before the main body '{'
And oh wait! int main* (?
@ Matri X
Its int main()..
thats writting mistake . I am sorry for it
Last edited on
@smac89:
I tried that . Output is still not coming it goes to coding page,
On line 8 from Smac's code, try:
 
cout << "Enter coordinates (x, y) for point 1: " << std::flush;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include<stdlib.h>
#include<math.h>
int main()
{
   system("cls");
   double distance,x1,y1,x2,y2;
   std::cout<<"Enter coordinates (x y) for point1: ";
   std::cin>>x1>>y1;
   std::cout<<"Enter coordinates (x y) for point2: ";
   std::cin>>x2>>y2;
   distance=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
   std::cout<<"The distance between two points is " <<distance<<std::endl;
   return 0;
}
The distance between two points is 2.23607
@Daleth
Using std::flush with std::cout is really not necessary, std::cout already incorporate the std::flush method, so after every output with it, it automatically flushes the output stream.
Last edited on
It happened before with some users where std::cout did not flush the buffer as expected.
I thought it was std::endl that does the flush?

http://www.cplusplus.com/reference/ostream/endl/


HTH
Last edited on
Oops, I admit I was wrong! I had always think it was the std::cout that does the "flushing", I'm sorry for misleading the original writer
Last edited on
@TheIdeasMan
From what I understand, std::flush only flushes the buffer while std::endl flushes and adds a newline character. In the line where I suggested using std::flush, one wouldn't need to add a newline character.
Is the actual problem that the console closes before you have a chance to read the output?

http://www.cplusplus.com/forum/beginner/1988/
had always think it was the std::cout that does the "flushing",

close, actually, in the code
1
2
std::cout<<"Enter coordinates (x y) for point1: ";
std::cin>>x1>>y1;

it's std::cin that does the flushing of std::cout. std::flush would be quite redundant.


when I press enter for output it returns to the coding screen.

Sounds like Chevril's link is the answer.
Topic archived. No new replies allowed.