Hi, just learning and experimenting away at C++ and I was wondering if you could suggest any ways I can make my code more efficient. Well here's my code. Beware, it is VERY simple and serves no real function, just an experiment to learn.
I don't really know, something like this would be a little more efficient. You could effectively recode this completely, but being a short and pretty useless code there's not much to do. In a code like this I don't think using 'case' is the most efficient method, takes a ton more lones of code than the way I did it. Also, I never use loops with 'goto' and neither do most people. Duoas or someone will probably laugh at my code and re-write it perfect, but for now here you go lol
I agree, and I hear that goto is considered bad practice.
Also I think that the switch/case is what you should use since I assume if this were an actual program you would be doing different things based on the user's choice.
As for the goto you could do what kryptonite suggested, but I would have just used a while loop like this:
I can't really remember the reason. Something about the jump depending on the value of the variable instead of doing comparisons. Of course, it has the disadvantage of having to list all the possible cases, so only use it for short lists.
I never use using namespace, so I'm so used to typing std::cout that it's actually faster if I do. And the compiler doesn't complain if you use it with using namespace, so it's alright.
I was suggesting replacing the if with the switch. Calling main() is just stupid, no offense. goto is a lot (I'll emphasize this. A LOT) better than infinite recursion.
ISO C++ forbids you to call main().
In any case, it is a very dangerous thing to do. Even if nothing else goes wrong, you'll eventually exhaust the stack.
Yah, your right. Duoas do you reply to PM's? I honestly jumped straight from coding nothing to advanced programs and have never had a situation where I needed to loop something like main. I know a lot of stuff about memory functions and more advanced functions than I do about the basics - which can be good but obviously not good at the same time lol
Well I changed some stuff up however a function I'm using isn't working. It's supposed to wait for the input number of seconds before running the next line of the program, however I get this error: 1>c:\documents and settings\brad\my documents\visual studio 2008\projects\switch\switch\switch.cpp(11) : fatal error C1903: unable to recover from previous error(s); stopping compilation However, I used this same function in another program and it worked fine. Here is my current code:
[code=cpp]#include "stdafx.h"
#include <iostream>