//run
#include <iostream>
#include <cmath>
label start:
cout << "After any error press q for ERROR n Help"
cout << "enter the sides in small,long,hypt format"
cin >> (small);
cin >> (long);
cin >> (hypt);
func (hypteq)
{
sqrt
{
sqr small + sqr long
}
}
int main()
{
if {
small > hypt
cout << "ERROR 1 Not a right triangle";
{
if cin << q
go to E1
}
} else {
goto ( long check)
label long check: {
if {
long > hypt
cout << "ERROR 2 Not a right triangle."
{
if cin << q
go to E2
}
} else {
goto checks0
}
label checks0: {
if small < 1
cout << "ERROR 3 Not a right triangle."
{
if cin << q
go to E3
}
} else {
goto checkl0
}
label checkl0: {
if long < 1
cout << "ERROR 4 Not a right triangle"
{
if cin << q
go to E4
}
} else {
goto (checkh)
label checkh: {
if hypt != hypteq
cout << "ERROR 5 not a right triangle"
{
if cin << q
go to E5
}
} else {
cout << hypeq
}
label E1:
cout << "small side bigger then hypt try again";
go to (start)
label E2:
cout << "small size less then 1 try again";
goto (start)
label E3:
cout << "long side greater then hypt try again";
goto (start)
label E4:
cout << "long side less then 1 try again";
goto (start)
label E5:
cout: "hypt not equal to equation try again" ;
goto (start)
return = 0
}
switch instead of if else
Page 1 of 111 Replies - 233 Views - Last Post: 09 January 2013 - 08:58 PM
#1
switch instead of if else
Posted 09 January 2013 - 12:00 PM
Replies To: switch instead of if else
#2
Re: switch instead of if else
Posted 09 January 2013 - 12:10 PM
#3
Re: switch instead of if else
Posted 09 January 2013 - 12:31 PM
if cin << q cout << "small side bigger then hypt try again";
Better still, use named strings for your error message, or put them in an array and reference them by number. (if you #DEFINE a short name for the error number, that's even better)
#4
Re: switch instead of if else
Posted 09 January 2013 - 12:45 PM
jon.kiparsky, on 09 January 2013 - 12:31 PM, said:
i only asked for a awnser not uncalledfor critism. I know goto's are bad, thats why i asked if i could use a switch, so i could get rid of those. like i said, i made it on a notepad, not a scripter so all i wanted was a yes or no and small explination why. nothing about the rest of my script.
#5
Re: switch instead of if else
Posted 09 January 2013 - 12:50 PM
You do want it to be less bad, right? That's what you were asking for? Yes?
So what are you complaining about?
This post has been edited by jon.kiparsky: 09 January 2013 - 12:50 PM
#6
Re: switch instead of if else
Posted 09 January 2013 - 12:54 PM
Skydiver, on 09 January 2013 - 12:10 PM, said:
I'm sorry but i don't understand how semicolons answer my question about switches. please don't just comment about my script that i said i would work on.
jon.kiparsky, on 09 January 2013 - 12:50 PM, said:
You do want it to be less bad, right? That's what you were asking for? Yes?
So what are you complaining about?
No, I want it shorter, not someones opinion on "better" if the script works, its good enough for me. and anyone else wont be looking at the script, just the program related to it.
#7
Re: switch instead of if else
Posted 09 January 2013 - 01:03 PM
Incidentally, your ability to get good feedback on the forum is also sure to increase if you drop some of the sensitivity. I'm relatively new and I already scratch my head when I see people sign up, post a rather incoherent piece of code on the same day that shows little understanding of the core subject matter, and then get indignant when people bother to take the time to point out other ways to improve. If you can't take your lumps, don't ask for free advice. If you want someone to just smile and do exactly what you ask no matter what they actually think, you'll have better luck with a dog and a tennis ball.
#8
Re: switch instead of if else
Posted 09 January 2013 - 01:25 PM
Switters, on 09 January 2013 - 01:03 PM, said:
Incidentally, your ability to get good feedback on the forum is also sure to increase if you drop some of the sensitivity. I'm relatively new and I already scratch my head when I see people sign up, post a rather incoherent piece of code on the same day that shows little understanding of the core subject matter, and then get indignant when people bother to take the time to point out other ways to improve. If you can't take your lumps, don't ask for free advice. If you want someone to just smile and do exactly what you ask no matter what they actually think, you'll have better luck with a dog and a tennis ball.
I suppose the sensitivity is a little much but i was just saying people should read the question fully. if the first two did they would realize i knew the script could look better all i needed was what you answered perfectly.
#9
Re: switch instead of if else
Posted 09 January 2013 - 01:31 PM
ShadowScripter, on 09 January 2013 - 04:25 PM, said:
No, that was the minimum you needed. You got much better. Be happy, and use the advice.
#10
Re: switch instead of if else
Posted 09 January 2013 - 02:36 PM
CTphpnwb, on 09 January 2013 - 01:31 PM, said:
ShadowScripter, on 09 January 2013 - 04:25 PM, said:
No, that was the minimum you needed. You got much better. Be happy, and use the advice.
All i wanted was to know if the switch would work, not how it looked not to get rid of goto's, and not how to make it look better. Just if swapping a switch for the if...else would function.
#11
Re: switch instead of if else
Posted 09 January 2013 - 03:17 PM
This post has been edited by Skydiver: 09 January 2013 - 03:18 PM
#12
Re: switch instead of if else
Posted 09 January 2013 - 08:58 PM
One thing I noted about your code was the fact that you had used '<<' for a cin...well...it should have been '>>' as '<<' is only used for cout.
Another thing I noted was the use of your if conditions...you seem to be using if without any conditons...and then you seem to be giving conditions in the following '{' brackets
It would be very good if you could give the condition along with the if as follows...here's a small example.
32 if {
33 long > hypt
34 cout << "ERROR 2 Not a right triangle."
could be
32 if(long > hypt)
33 {
34 cout << "ERROR 2 Not a right triangle.";
35 }
and yes...a switch case can replace your if conditions if you make the if conditions as cases of your switch control...and for default, you can say...invalid.
edit:Small typo
regards,
Raghav
This post has been edited by raghav.naganathan: 09 January 2013 - 09:01 PM
|
|

New Topic/Question
Reply



MultiQuote






|