 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Chris Schumacher Guest
|
Posted: Sat Sep 27, 2003 12:30 am Post subject: Strange switch loop. |
|
|
I've been working on a simple calculation program, which involves
entering the two operands and the operator. I used a switch to test
for each operator, and used the default to allow the user to attempt
another entry if the operator is invalid.
The program works, but I've noticed that I get as many answers at the
end (all the same thing) for each time I went through the loop. I
can't quite figure out why this is happening.
Source is below, any help is greatly appreciated.
(oh, I'm using the Visual C++ 6.0 compiler, so this might be an MS
problem...)
//begin
#include <iostream>
#include <cmath>
using namespace std;
void main()
{
int op1, op2, ans;
char opr;
cout << "Enter first value: n";
cin >> op1;
cout << "Enter the second value: n";
cin >> op2;
cout << "Enter the operator: n";
cin >> opr;
switch(opr)
{
case '+': ans = op1 + op2;
break;
case '-': ans = op1 - op2;
break;
case '/': ans = op1 / op2;
break;
case '*' : ans = op1 * op2;
break;
case 'x' : ans = op1 * op2;
break;
case 'X' : ans = op1 * op2;
break;
case '^': ans = pow (op1, op2);
break;
case '%': ans = op1 % op2;
break;
default: cout<< "Invalid Operatorn";
main();
}
cout << op1 << opr << op2 << "= " << ans << endl;
}
-==Kensu==-
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Sat Sep 27, 2003 2:14 pm Post subject: Re: Strange switch loop. |
|
|
"Chris Schumacher" <kensu_ (AT) hotmail (DOT) com> wrote in message
| Quote: | //begin
#include <iostream
#include
using namespace std;
void main()
{
int op1, op2, ans;
char opr;
cout << "Enter first value: n";
cin >> op1;
cout << "Enter the second value: n";
cin >> op2;
cout << "Enter the operator: n";
cin >> opr;
switch(opr)
{
case '+': ans = op1 + op2;
break;
case '-': ans = op1 - op2;
break;
case '/': ans = op1 / op2;
break;
case '*' : ans = op1 * op2;
break;
case 'x' : ans = op1 * op2;
break;
case 'X' : ans = op1 * op2;
break;
case '^': ans = pow (op1, op2);
break;
case '%': ans = op1 % op2;
break;
default: cout<< "Invalid Operatorn";
main();
}
cout << op1 << opr << op2 << "= " << ans << endl;
}
|
Each call to main() prints a line. In your default clause you call main()
recursively, so you print one or more lines in the recursive call, plus
another at the end of main.
Per 3.6.1.3, you cannot call main from your program.
As a usage issue, your program prompts for all 2 numbers and the operator
again when the operator is wrong. Do you want to prompt for just the
operator?
BTW, you can combine multiple case statements. Instead of
| Quote: | case 'x' : ans = op1 * op2;
break;
case 'X' : ans = op1 * op2;
break;
|
try
case 'X' : case 'x' : ans = op1 * op2;
break;
You could also try switch (tolower(opr)).
Here's one structure that might work
int main()
{
using std::cout;
using std::cin;
cout << "Enter first value: n";
int op1;
cin >> op1;
cout << "Enter the second value: n";
int op2;
cin >> op2;
char opr;
int ans;
while (true)
{
bool breakout = true;
cout << "Enter the operator: n";
cin >> opr;
switch(opr)
{
case '+': ans = op1 + op2; break;
case '-': ans = op1 - op2; break;
default: cout<< "Invalid Operatorn"; breakout = false;
}
if (breakout) break;
}
cout << op1 << opr << op2 << "= " << ans << std::endl;
}
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Sat Sep 27, 2003 2:15 pm Post subject: Re: Strange switch loop. |
|
|
On Fri, 26 Sep 2003 20:30:41 -0400, Chris Schumacher wrote:
| Quote: | I've been working on a simple calculation program, which involves
entering the two operands and the operator. I used a switch to test
for each operator, and used the default to allow the user to attempt
another entry if the operator is invalid.
The program works, but I've noticed that I get as many answers at the
end (all the same thing) for each time I went through the loop. I
can't quite figure out why this is happening.
Source is below, any help is greatly appreciated.
(oh, I'm using the Visual C++ 6.0 compiler, so this might be an MS
problem...)
//begin
#include <iostream
#include
using namespace std;
void main()
{
int op1, op2, ans;
char opr;
cout << "Enter first value: n";
cin >> op1;
cout << "Enter the second value: n";
cin >> op2;
cout << "Enter the operator: n";
cin >> opr;
switch(opr)
{
case '+': ans = op1 + op2;
break;
case '-': ans = op1 - op2;
break;
case '/': ans = op1 / op2;
break;
case '*' : ans = op1 * op2;
break;
case 'x' : ans = op1 * op2;
break;
case 'X' : ans = op1 * op2;
break;
case '^': ans = pow (op1, op2);
break;
case '%': ans = op1 % op2;
break;
default: cout<< "Invalid Operatorn";
main();
|
Invalid opreation, so main is called again. However, if there is an
invalid operation again, main will be called again. Now, finally, when a
valid input is found, the stack will unwind, and even the output for all
the invalid inputs will be shown. Unless VC6 does not initialize the stack
frame for each recursive call of main(), you will get the same output for
every return of main form itself.
Compile this, and as input, put these values: 2, 3, 4, 5, 7, 9, 0
And see if the ouput is: 0, 9, 7, 5, 4, 3, 2
If it is not, the compiler is messed.
#include
using namespace std;
int main ()
{
int i;
cout<
cin>>i;
if (i) main();
cout<<"The Integer is: "<
return 0;
}
| Quote: | }
cout << op1 << opr << op2 << "= " << ans << endl;
}
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jakob Bieling Guest
|
Posted: Sat Sep 27, 2003 2:23 pm Post subject: Re: Strange switch loop. |
|
|
"Chris Schumacher" <kensu_ (AT) hotmail (DOT) com> wrote
| Quote: | I've been working on a simple calculation program, which involves
entering the two operands and the operator. I used a switch to test
for each operator, and used the default to allow the user to attempt
another entry if the operator is invalid.
The program works, but I've noticed that I get as many answers at the
end (all the same thing) for each time I went through the loop. I
can't quite figure out why this is happening.
Source is below, any help is greatly appreciated.
(oh, I'm using the Visual C++ 6.0 compiler, so this might be an MS
problem...)
//begin
#include <iostream
#include
using namespace std;
void main()
|
'main' should always return 'int'.
| Quote: | {
int op1, op2, ans;
char opr;
cout << "Enter first value: n";
cin >> op1;
cout << "Enter the second value: n";
cin >> op2;
cout << "Enter the operator: n";
cin >> opr;
switch(opr)
{
case '+': ans = op1 + op2;
break;
case '-': ans = op1 - op2;
break;
case '/': ans = op1 / op2;
break;
case '*' : ans = op1 * op2;
break;
case 'x' : ans = op1 * op2;
break;
case 'X' : ans = op1 * op2;
break;
case '^': ans = pow (op1, op2);
break;
case '%': ans = op1 % op2;
break;
default: cout<< "Invalid Operatorn";
main();
|
'main' must not be called. Create an infinite loop (for (; or while(1)
etc) instead of using the recursion. Also, you should provide an exit
condition to quit the program.
| Quote: | }
cout << op1 << opr << op2 << "= " << ans << endl;
}
|
hth
--
jb
(replace y with x if you want to reply by e-mail)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Sat Sep 27, 2003 2:23 pm Post subject: Re: Strange switch loop. |
|
|
Chris Schumacher wrote:
| Quote: | I've been working on a simple calculation program, which involves
entering the two operands and the operator. I used a switch to test
for each operator, and used the default to allow the user to attempt
another entry if the operator is invalid.
The program works, but I've noticed that I get as many answers at the
end (all the same thing) for each time I went through the loop. I
can't quite figure out why this is happening.
|
Me neither, because the code you show doesn't feature any loops. However,
here are two things I noticed with your code:
- main()'s return-type, see the FAQ
- failure-modes are sticky, ane failed input will cause all subsequent
inputs to fail.
- calling main() recursively is, well, not a good idea. I don't think that
you should do it... Instead, move all the code to a function and call
that.
Uli
--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sat Sep 27, 2003 2:45 pm Post subject: Re: Strange switch loop. |
|
|
In article <a477nvstn7gp5qlnpvbd71ko6vvc44pk2f (AT) 4ax (DOT) com>, Chris
Schumacher <kensu_ (AT) hotmail (DOT) com> writes
| Quote: | default: cout<< "Invalid Operatorn";
main();
}
There is the cause because every recursive call will result in printing |
an 'answer'. However your compiler should have diagnosed the call to
main as an error because in C++ (not in C) main() may not be called.
However as you return void from main you are already outside the
requirements of the C++ Standard and the compiler is entitled to do
whatever it likes (as long as it diagnosed the wrong return type from
main)
As a general guideline, avoid recursive calls unless you understand
their import.
--
Francis Glassborow ACCU
If you are not using up-to-date virus protection you should not be reading
this. Viruses do not just hurt the infected but the hole community.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jakob Bieling Guest
|
Posted: Sat Sep 27, 2003 4:53 pm Post subject: Re: Strange switch loop. |
|
|
"Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote
| Quote: | Compile this, and as input, put these values: 2, 3, 4, 5, 7, 9, 0
And see if the ouput is: 0, 9, 7, 5, 4, 3, 2
If it is not, the compiler is messed.
#include <iostream
using namespace std;
int main ()
{
int i;
cout<
cin>>i;
if (i) main();
cout<<"The Integer is: "<
return 0;
}
|
It is invalid code. You may not call main in your program. Use a loop
instead.
regards
--
jb
(replace y with x if you want to reply by e-mail)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Sat Sep 27, 2003 4:55 pm Post subject: Re: Strange switch loop. |
|
|
Chris Schumacher <kensu_ (AT) hotmail (DOT) com> writes:
| Quote: | I've been working on a simple calculation program, which involves
entering the two operands and the operator. I used a switch to test
for each operator, and used the default to allow the user to attempt
another entry if the operator is invalid. The program works, but
I've noticed that I get as many answers at the end (all the same
thing) for each time I went through the loop. I can't quite figure
out why this is happening. Source is below, any help is greatly
appreciated. (oh, I'm using the Visual C++ 6.0 compiler, so this
might be an MS problem...)
|
Sort of...
| Quote: | //begin
#include <iostream
#include
using namespace std;
void main()
|
Should be "int main()". "void main()" is a Microsoft extension.
| Quote: | {
int op1, op2, ans;
char opr;
cout << "Enter first value: n";
cin >> op1;
cout << "Enter the second value: n";
cin >> op2;
cout << "Enter the operator: n";
cin >> opr;
switch(opr)
{
case '+': ans = op1 + op2;
break;
case '-': ans = op1 - op2;
break;
case '/': ans = op1 / op2;
break;
case '*' : ans = op1 * op2;
break;
case 'x' : ans = op1 * op2;
break;
case 'X' : ans = op1 * op2;
break;
case '^': ans = pow (op1, op2);
break;
case '%': ans = op1 % op2;
break;
default: cout<< "Invalid Operatorn";
main();
|
In C++, you are not allowed to call main recursively. I'm surprised
that nothing worse is happening.
| Quote: | }
cout << op1 << opr << op2 << "= " << ans << endl;
|
Supposing the recursive call to main actually works, this line gets
executed for each call. A recursive call is only a loop if it is the
last thing in the function.
--
James Kanze mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France +33 1 41 89 80 93
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Sun Sep 28, 2003 10:24 pm Post subject: Re: Strange switch loop. |
|
|
"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote in message
| Quote: | As a general guideline, avoid recursive calls unless you understand
their import.
|
Really? In some programming languages, like LISP and Scheme, recursive is
the first thing you learn. They say it is easier to prove the correctness
of recursive programs. Some say recursive programs are easier to
understand, but the fact that you have to spend a day studying the
correctness of the factorial program makes one wonder (maybe it's just a
barrier to overcome, then recursive programs are easy to understand).
Anyway, are you saying the same teaching guideline applies to C++?
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Mon Sep 29, 2003 6:21 am Post subject: Re: Strange switch loop. |
|
|
"Jakob Bieling" <netsurf (AT) gmy (DOT) net> wrote
| Quote: | It is invalid code. You may not call main in your program. Use a loop
instead.
|
Out of curiosity, may namespaces define a function main(), with no arguments
or argc and argv? As any regular functions, you can call these functions or
take the address of them.
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Agent Mulder Guest
|
Posted: Mon Sep 29, 2003 6:25 am Post subject: Re: Strange switch loop. |
|
|
| Quote: | As a general guideline, avoid recursive calls unless you understand
their import.
|
<Siemel Naran>
| Quote: | Really? In some programming languages, like LISP and Scheme, recursive is
the first thing you learn. They say it is easier to prove the correctness
of recursive programs. Some say recursive programs are easier to
understand, but the fact that you have to spend a day studying the
correctness of the factorial program makes one wonder (maybe it's just a
barrier to overcome, then recursive programs are easy to understand).
/ |
Allow me to throw in my favorite recursive here:
#include <iostream>
static const int N=21;
int fibonacci(int a)
{
return
a==0?1:
a==1?1:
a==2?2:
fibonacci(a-1)+fibonacci(a-2);
}
int main()
{
cout<<
"nFibonacci and the original problem about rabbits where the series
nfirst appears, the family trees of cows and bees, the golden ratio
nand the Fibonacci series, the Fibonacci Spiral and sea shell
nshapes, branching plants, flower petal and seeds, leaves and petal
narrangements, on pineapples and in apples, pine cones and leaf
narrangements. All involve the Fibonacci numbers - and here's how
nand why.
nnhttp://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fib.htmln";
for(int a=0;a
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Spangenberg Guest
|
Posted: Mon Sep 29, 2003 9:27 pm Post subject: Re: Strange switch loop. |
|
|
Hello, Siemel Naran
Siemel Naran schrieb:
| Quote: | "Jakob Bieling" <netsurf (AT) gmy (DOT) net> wrote in message
news:bl468u$qa8$06$1 (AT) news (DOT) t-
It is invalid code. You may not call main in your program. Use a loop
instead.
Out of curiosity, may namespaces define a function main(), with no arguments
or argc and argv? As any regular functions, you can call these functions or
take the address of them.
|
Yes, they are allowed to do so. 3.6.1/p. 3 says:
"The function main shall not be used (3.2) within a program. The linkage (3.5)
of main is
implementationdefined. A program that declares main to be inline or static is
illformed.
The name main is not otherwise reserved. [Example: member functions, classes,
and enumerations can be
called main, as can entities in other namespaces. ]"
Yours,
Daniel Spangenberg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Mon Sep 29, 2003 9:30 pm Post subject: Re: Strange switch loop. |
|
|
On Sat, 27 Sep 2003 12:53:12 -0400, Jakob Bieling wrote:
| Quote: | It is invalid code. You may not call main in your program. Use a loop
instead.
|
Oh, ok thanks. I was under the impression that it was fine for main to be
reentrant.
Regards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Mon Sep 29, 2003 9:30 pm Post subject: Re: Strange switch loop. |
|
|
In article
<NXHdb.162444$3o3.11668853 (AT) bgtnsc05-news (DOT) ops.worldnet.att.net>, Siemel
Naran <SiemelNaran (AT) REMOVE (DOT) att.net> writes
| Quote: | "Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote in message
As a general guideline, avoid recursive calls unless you understand
their import.
Really? In some programming languages, like LISP and Scheme, recursive is
the first thing you learn. They say it is easier to prove the correctness
of recursive programs. Some say recursive programs are easier to
understand, but the fact that you have to spend a day studying the
correctness of the factorial program makes one wonder (maybe it's just a
barrier to overcome, then recursive programs are easy to understand).
Anyway, are you saying the same teaching guideline applies to C++?
|
For certain mathematical uses recursive functions are closer to the
problem domain and so are clearly easier to understand. However using
recursion without understanding is always likely to cause problems and
doubly so if the language you are using does not provide special support
for recursion.
It is interesting to note that both the languages you quote were
designed with recursion in mind, indeed it is just about essential for
LISP. I hardly think that it is a coincidence that LISP (a very powerful
and mind expanding language) is among the worst taught computer
languages. Many instructors have never made the paradigm shift required
to write LISP correctly.
Anyway my guideline was for C and C++ and note the unless... which is
surely true in LISP and Scheme as well (except in those languages you
just about have to grasp the nettle and understand recursion in all its
manifest forms).
--
Francis Glassborow ACCU
If you are not using up-to-date virus protection you should not be reading
this. Viruses do not just hurt the infected but the whole community.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthew Collett Guest
|
Posted: Mon Sep 29, 2003 9:34 pm Post subject: Re: Strange switch loop. |
|
|
In article <bl7t5m$mkn$1 (AT) news2 (DOT) tilbu1.nb.home.nl>,
"Agent Mulder" <mbmulder_remove_this_ (AT) home (DOT) nl> wrote:
| Quote: | As a general guideline, avoid recursive calls unless you understand
their import.
Allow me to throw in my favorite recursive here:
int fibonacci(int a)
{
return
a==0?1:
a==1?1:
a==2?2:
fibonacci(a-1)+fibonacci(a-2);
}
|
Which takes exponential time; an iterative implementation would require
only linear time. QED .
(One fix is of course to cache the results of each call. See e.g.
Section 5.3 of Sedgewick "Algorithms in C++".)
Best wishes,
Matthew Collett
--
Those who assert that the mathematical sciences have nothing to say
about the good or the beautiful are mistaken. -- Aristotle
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|