help me figure out error..

Jun 18, 2013 at 3:54am
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;
}

Jun 18, 2013 at 4:32am
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 Jun 18, 2013 at 4:33am
Jun 18, 2013 at 4:37am
The code still has syntax errors, such as on line 13.
Jun 18, 2013 at 4:44am
Error compiling: Line 5 : Expected '(' before the main body '{'
And oh wait! int main* (?
Jun 18, 2013 at 5:07am
@ Matri X
Its int main()..
thats writting mistake . I am sorry for it
Last edited on Jun 18, 2013 at 5:07am
Jun 18, 2013 at 5:09am
@smac89:
I tried that . Output is still not coming it goes to coding page,
Jun 18, 2013 at 5:22am
On line 8 from Smac's code, try:
 
cout << "Enter coordinates (x, y) for point 1: " << std::flush;
Jun 18, 2013 at 6:55am
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
Jun 18, 2013 at 7:02am
@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 Jun 18, 2013 at 8:59am
Jun 18, 2013 at 7:12am
It happened before with some users where std::cout did not flush the buffer as expected.
Jun 18, 2013 at 7:25am
I thought it was std::endl that does the flush?

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


HTH
Last edited on Jun 18, 2013 at 7:27am
Jun 18, 2013 at 8:54am
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 Jun 18, 2013 at 8:56am
Jun 18, 2013 at 3:30pm
@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.
Jun 18, 2013 at 3:42pm
Is the actual problem that the console closes before you have a chance to read the output?

http://www.cplusplus.com/forum/beginner/1988/
Jun 18, 2013 at 4:28pm
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.