Jump to content

Recommended Posts

Posted

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;
}
Posted

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?

Posted

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 :P

  • Like 2
Posted

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.

  • Like 1
Posted

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 :P

It's not really etiquette, it's to help stop problems in the future.

  • Like 2
Posted

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.

Posted

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 :P

 

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.

Posted

I didn't "chastise" you, I just said that you'd be better off statically referencing functions :P

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. :P

Posted (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 operators
basic input/output
logic (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 by InfectiousBubbles
  • Like 1
Posted

Now I just gotta figure out the next two parts. But my helper isn't online. :(

  • Leader
Posted

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.

  • Like 1
Posted

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++. :)

Posted

@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 :P

Posted

LOL. I realize what I need to use, just how to format it. I think what you provided helps.

Posted (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 by soulJAHmon
  • Like 2

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.