float variable (i input the value is a letter),Error syntax!

when i want to input the value of equation,example is length and then i enter it a letter to the value,the program became error.Please help me to fix this program?Thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream.h>
#include <conio.h>
#include <cstring.h>
int main ()
{
begin:
clrscr ();

	float x,y,result;
	cout<<" Welcome to Logic & algorithm program"<<endl;
	cout<<" Choose the equation : "<<endl;
	cout<<" 1 Triangle equation"<<endl;
	cout<<" 2 Rectangular equation"<<endl;
	cout<<" 3 Square equation"<<endl;
   back:
	cout<<" type in here : "; char h ;cin>>h;
 	if(h == '1')
	 {
	   cout<<"Enter base : ";
 		cin>>x;
 		cout<<"Enter height : ";
      cin>>y;
      result=0.5*x*y;
      cout<<"Triangle area = "<<result<<ends;
 }
  else if(h=='2')
  {
   cout<<"Enter length : ";
   cin>>x;
   cout<<"Enter width : ";
   cin>>y;
   result=x*y;
   cout<<"rectangular area = "<<result<<ends;
 }
 else if(h=='3')
  {
  cout<<"Enter side : ";
  cin>>x;
  result=x*x;
  cout<<"Square area = "<<result<<ends;
 }
 else
  {
 cout<<"Wrong number you entered!";
 goto back;

 }
 repeat:
    cout<<"Repeat : [Y/N] ? "  ;
    char m ;
    cin>>m;
        if (m == 'Y')
            goto begin ;
        else if (m == 'N')
            { cout<<"Press enter to exit.";
            goto end;
            }
        else
            {
            cout<<"Wrong keywords"<<endl;
            goto repeat;
            }
 end:

getch ();
return 0;
}
Last edited on

what is result? - I don't see it declared anywhere?
oh,sorry i forgot to change it.:)

Yeah and now you removed hasil which is also used throughout your code :)

Anyway, just leave out result, change lines 24, 33, 40 to use hasil instead of result, or you could change all the hasil to result.
when i want to input the value of equation,example is length and then i enter it a letter to the value,the program became error.


The C++ streams know you can't enter a character into a numeric value so if you try the stream will be put into an error state and no further processing can be done with that stream until you clear() the stream error and clean out the input buffer of all the incorrect values.

Something like:
1
2
cin.clear();
cin.ignore(2000,'\n'); 


And goto? Really? You need to learn to use some of the other control statements, and loops. The goto statement is really a bad idea in this program.
Last edited on
1
2
3
cin.clear();
cin.ignore(2000,'\n'); 

how to use this codes?please give me some examples.I'm new in here,i learn c++ just last week.So, i just know few structure.What kind of control statements do you mean jlb?Give me some example please.Can i replace goto statement with another statement that is work properly?
Did you try to use your favourite search engine and search for cin.clear() or cin.ignore()? There several good links within the first 5 links, most providing good examples.

I just have used the functions,it is worked nicely.Thank you man,you save my life.Nice answer from jlb.
Topic archived. No new replies allowed.