|
-
May 5th, 2011, 02:19 PM
#16
Re: Function problem
 Originally Posted by fuzzylr
I would like some advice. I would like to reduce the use of goto in this function even though it's fairly simple. What if I wanted to reuse bits and pieces of code? Can I do that or once I get into If / else or While loops do I need to move the code pieces inside the loop till it breaks out or what not. It seems kind of pointless to duplicate the code. I guess that is why the goto is so appealing in this type of instance.
Code:
int add()
{
char answer;
int a,b;
int total;
start:
std::cout<<"Enter first number: ";
std::cin>>a;
std::cout<<"Enter Second number: ";
std::cin>>b;
total = a+b;
std::cout<<"\nSum: "<< total <<"\n\n";
question:
std::cout<<"Do you wish to continue: Y/N ";
std::cin>>answer;
switch (answer)
{
case 'y': goto start;
case 'Y': goto start;
case 'n': goto end;
case 'N': goto end;
default:
std::cout<<"Invalid characters. Use Y/N or y/n.\n\n";
goto question;
}
end:
std::cout<<"Returning to menu";
sleep(3);
return 0;
}
would it be bad if I did cases like this?
Code:
case 'y' || 'Y': code sample here
My Goal is to consolidate the code down as small as possible while still being functional.
My god, this is C++ not BASIC. Remove those gotos and replace them with loops and conditionals.
C++ supports 3 types of loop, they are counted (for loop), top tested (while loop ) and bottom tested (do/while loop).
Code:
for( int i = 0; i < 10; ++i )
{
// executed 10 times
}
while( variable != value )
{
// executes while variable is not equal to value
}
do
{
// always executes once, and maybe more
} while ( variable != value );
All better choices than a goto!
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
May 5th, 2011, 02:22 PM
#17
Re: Function problem
 Originally Posted by fuzzylr
I would like some advice. I would like to reduce the use of goto in this function even though it's fairly simple.
No, you should eliminate all of the goto's, not just reduce them.
Secondly, a program riddled with goto's are not "simple". It's simple because you put them in, but most programmers who look at your code will not put any effort into understanding it if it has goto's in it.
Look at this example (not compiled):
Code:
#include <iostream>
char getResponse()
{
char answer;
bool inputOK = false;
do
{
std::cout<<"Do you wish to continue: Y/N ";
std::cin>>answer;
switch (answer)
{
case 'y':
case 'Y':
case 'n':
case 'N':
inputOK = true;
break;
default:
std::cout<<"Invalid characters. Use Y/N or y/n.\n\n";
}
} while ( !inputOK );
return answer;
}
int add()
{
char answer;
int a,b;
int total;
do
{
std::cout<<"Enter first number: ";
std::cin>>a;
std::cout<<"Enter Second number: ";
std::cin>>b;
total = a+b;
std::cout<<"\nSum: "<< total <<"\n\n";
answer = getResponse();
} while (answer == 'Y' || answer == 'y');
std::cout<<"Returning to menu";
return 0;
}
Just add a function to get the response. Then the code doesn't turn into spaghetti using goto's.
http://en.wikipedia.org/wiki/Spaghetti_code
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 5th, 2011 at 02:27 PM.
-
May 5th, 2011, 02:37 PM
#18
Re: Function problem
 Originally Posted by Paul McKenzie
No, you should eliminate all of the goto's, not just reduce them.
Secondly, a program riddled with goto's are not "simple". It's simple because you put them in, but most programmers who look at your code will not put any effort into understanding it if it has goto's in it.
Look at this example (not compiled):
Code:
#include <iostream>
char getResponse()
{
char answer;
bool inputOK = false;
do
{
std::cout<<"Do you wish to continue: Y/N ";
std::cin>>answer;
switch (answer)
{
case 'y':
case 'Y':
case 'n':
case 'N':
inputOK = true;
break;
default:
std::cout<<"Invalid characters. Use Y/N or y/n.\n\n";
}
} while ( !inputOK );
return answer;
}
int add()
{
char answer;
int a,b;
int total;
do
{
std::cout<<"Enter first number: ";
std::cin>>a;
std::cout<<"Enter Second number: ";
std::cin>>b;
total = a+b;
std::cout<<"\nSum: "<< total <<"\n\n";
answer = getResponse();
} while (answer == 'Y' || answer == 'y');
std::cout<<"Returning to menu";
return 0;
}
Just add a function to get the response. Then the code doesn't turn into spaghetti using goto's.
http://en.wikipedia.org/wiki/Spaghetti_code
Regards,
Paul McKenzie
Thank you. That is definitely some food for thought. Great examples.
Sean
Programing 1
Programing 2 (Spring this year)
Eager Game Developer.
-
May 5th, 2011, 04:02 PM
#19
Re: Function problem
I used your code as a base. Pretty much used it entirely. For practice. I have seem to hit a snag. Not sure why. The last part of the code is being skipped.
Code:
int add()
{
char answer;
int a,b;
int total;
do
{
std::cout<<"Enter first number: ";
std::cin>>a;
std::cout<<"Enter Second number: ";
std::cin>>b;
// Add to Int's together.
total = a+b;
std::cout<<"\nSum: "<< total <<"\n\n";
answer = getResponse();
} while (answer == 'Y' || answer == 'y');
std::cout<<"Returning to menu";
sleep(3);
return 0;
}
This code is only partically being processed...
Code:
while (answer == 'Y' || answer == 'y');
std::cout<<"Returning to menu"; <---- this piece
sleep(3);
return 0;
}
What is odd is the sleep is being processed but the cout is not.
Sean
Programing 1
Programing 2 (Spring this year)
Eager Game Developer.
-
May 5th, 2011, 04:21 PM
#20
Re: Function problem
std::cout<<"Returning to menu" << std::endl;
-
May 5th, 2011, 04:34 PM
#21
Re: Function problem
 Originally Posted by Richard.J
std::cout<<"Returning to menu" << std::endl;
Is there any difference between
and
When should which be used?
http://www.cplusplus.com/forum/beginner/7350/
I found the link above but I can't seem to find anything saying that they are really all that different.
Sean
Programing 1
Programing 2 (Spring this year)
Eager Game Developer.
-
May 5th, 2011, 04:38 PM
#22
Re: Function problem
they both add a newline to the buffer, but std::endl flushes the buffer. I.e. when using \n, you'll never know when the buffer is actually sent to the console (assuming std::cout is used) whereas std::endl enforces this.
-
May 5th, 2011, 04:45 PM
#23
Re: Function problem
 Originally Posted by Richard.J
they both add a newline to the buffer, but std::endl flushes the buffer. I.e. when using \n, you'll never know when the buffer is actually sent to the console (assuming std::cout is used) whereas std::endl enforces this.
Thank you very much.
Sean
Programing 1
Programing 2 (Spring this year)
Eager Game Developer.
-
May 5th, 2011, 05:10 PM
#24
Re: Function problem
 Originally Posted by fuzzylr
This code is only partically being processed...
Code:
while (answer == 'Y' || answer == 'y'); <---- semi colon here is bad
std::cout<<"Returning to menu"; <---- this piece
sleep(3);
return 0;
}
What is odd is the sleep is being processed but the cout is not.
.....
-
May 5th, 2011, 05:12 PM
#25
Re: Function problem
In most cases it doesn't really matter when the output buffer gets flushed, since it will certainly be flushed at the end of the program or upon trying to get any user input. There are a few cases where it does matter (inter-process communication, for instance), so it's good to know what flushing is all about.
-
May 5th, 2011, 05:19 PM
#26
Re: Function problem
 Originally Posted by Amleto
.....
Yes it does belong there. It will throw compiler errors or should I say it did toss compiler errors.
also note stated reference below that do-while loops are built that way.
http://www.cplusplus.com/doc/tutorial/control/
Sean
Programing 1
Programing 2 (Spring this year)
Eager Game Developer.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|