Heretic121 Posted March 18, 2014 Posted March 18, 2014 I'm not sure about anyone else, but I prefer code examples when it comes to learning things. It allows me to learn at my own pace. I've literally just typed this up, and it's my first attempt at C++. It works as intended. #include <iostream> void helloWorld () { std::cout << "Hello world!\n"; } int main() { char answer = 0; while( answer == 0 ) { helloWorld(); std::cout << "Close this application?\n"; std::cin >> answer; switch(answer){ case ('y'): return 0; break; default: if ( answer != 0 ){ answer = 0; } break; } } return 0; } Quote
parrot Posted March 19, 2014 Posted March 19, 2014 My school was rather insistant on using namespace std, but I think your method is perhaps better practice. I assume the idea was to utilize as many different features as possible, rather than the most logical code? Quote
Heretic121 Posted March 19, 2014 Author Posted March 19, 2014 My school was rather insistant on using namespace std, but I think your method is perhaps better practice. http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice I assume the idea was to utilize as many different features as possible, rather than the most logical code? Spot on. However, I'm now questioning why I left that "return 0;" at the end 2 Quote
Midnight Posted March 19, 2014 Posted March 19, 2014 Yeah see Heretic chastised me yesterday for using "using namespace std" in my code. I'm glad I'm not the only one. Too much etiquette with this coding stuff. 1 Quote
Heretic121 Posted March 19, 2014 Author Posted March 19, 2014 Yeah see Heretic chastised me yesterday for using "using namespace std" in my code. I'm glad I'm not the only one. Too much etiquette with this coding stuff. I didn't "chastise" you, I just said that you'd be better off statically referencing functions It's not really etiquette, it's to help stop problems in the future. 2 Quote
Latino555 Posted March 19, 2014 Posted March 19, 2014 So, is programming something you liked from the start, or as you got better, you began to like it? Right now, I'm taking a class, and I hate it because it's takes to much time to make something so simple. Quote
Heretic121 Posted March 19, 2014 Author Posted March 19, 2014 Well C++ is a language I've only recently decided to learn. However I am proficient in PHP, a scripting language, and have coded quite a few different applications in that, both CLI and web apps. It's quite easy to learn, or at least I found it quite easy, but, as with all things, unless you enjoy it you're not likely to want to learn. Personally I think PHP is a good first language as a introduction to the world of coding. It doesn't take much to create simple applications, and doesn't fail hopelessly if something goes wrong As for liking programming; I enjoy creating things but I'm not very creative. For concentrating when programming I like to listen to music to help me focus. Quote
Midnight Posted March 19, 2014 Posted March 19, 2014 I didn't "chastise" you, I just said that you'd be better off statically referencing functions It's not really etiquette, it's to help stop problems in the future. I know I know. I just have to dramatize. As well, I started PHP and HTML after C++ and it was much easier and more direct in the sense of you can see your progress as you go. I just recently got the itch to go back to C++ and play with it. I forgot a lot of stuff. Quote
Midnight Posted March 19, 2014 Posted March 19, 2014 (edited) Something a lil easier for starters. I actually found the problem for this on the C++ forums. http://www.cplusplus.com/forum/articles/12974/ Requires:variables, data types, and numerical operatorsbasic input/outputlogic (if statements, switch statements)loops (for, while, do-while)Write a program that ccontinues to asks the user to enter any number other than 5 until the user enters the number 5.Then tell the user "Hey! you weren't supposed to enter 5!" and exit the program.★ Modify the program so that after 10 iterations if the user still hasn't entered 5 will tell the user "Wow, you're more patient then I am, you win." and exit.★★ Modify the program so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.) * hold up, mistake in code. Fixed! #include <iostream> int main () { int z = 0; while (z !=5) { std::cout << "Please enter a number other then 5.\n"; std::cin >> z; } std::cout << "Hey! You weren't supposed to enter 5." <<std::endl; return 0; } Edited March 19, 2014 by InfectiousBubbles 1 Quote
Midnight Posted March 19, 2014 Posted March 19, 2014 Now I just gotta figure out the next two parts. But my helper isn't online. Quote
Leader RedBaird Posted March 20, 2014 Leader Posted March 20, 2014 So, is programming something you liked from the start, or as you got better, you began to like it? Right now, I'm taking a class, and I hate it because it's takes to much time to make something so simple. You may like it more as you gain more knowledge of the tools and techniques available and begin to think of it as solving puzzles or as tracing a route through trackless forests. 1 Quote
staticwarp Posted March 20, 2014 Posted March 20, 2014 if i'm reading your code right, i think it's neat that you used a switch statement instead of an if statement. for such a simple task i probably would have just used if, but then i'm still pretty new to c++. Quote
Heretic121 Posted March 20, 2014 Author Posted March 20, 2014 @Infectious; The two "bonus" points appear quite easy to solve. You just need a counter. The first bonus point just needs a for() statement instead of a while() statement. for (int i = 0; i < 10; i++) { // Code to run... } For the second bonus point: Before the while() do; int counter = 0; Then in the while(), at the end, do; counter++; Then you just make use of the counter variable, including using an if() to exit the program when the right number is given. @Static; The first code example was an attempt to use as many different techniques as possible, not to make the shortest Quote
Midnight Posted March 20, 2014 Posted March 20, 2014 LOL. I realize what I need to use, just how to format it. I think what you provided helps. Quote
soulJAHmon Posted March 20, 2014 Posted March 20, 2014 (edited) Is C++ code when your hot teacher tells you, with a wink and a gleam in her eye, that you only have a C , but if you diddle her after school, you might get a C++? Edited March 20, 2014 by soulJAHmon 2 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.