Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12

Thread: Code VS Code

  1. #1
    Join Date
    Oct 2014
    Location
    Gillingham Kent
    Posts
    15

    Default Code VS Code

    hi guys , just so i could practice using cout and cin functions i made a simple program that asks the user for their name , and then says hello followed by their name back to them. here is the program i made

    Example 1

    // practice cin & cout (what is youre name?)
    #include <iostream>
    #include <string>

    using namespace std;

    int main()

    {string name;

    cout << "What is your name? ";
    cin >> name;
    cout << "Hello, " << name << "!\n";
    }

    I have also been learning the other way of doing this and made an alternate version of the same program

    Example 2

    // practice cin & cout (what is youre name?)
    #include <iostream>
    #include <string>

    int main()
    {
    std::string name;
    std::cout << "What is your name? ";
    getline (std::cin, name);
    std::cout << "Hello, " << name << "!\n";
    }

    NOW my question is they both accomplish the exact same thing just in slightly different ways, so how do i know which way to do things? is there a right and wrong answer?? or is it simply up to the programmers personal preference?

    The reason i ask is because to me although i understand both ways, example 1, in my opinion is much easier to read, i find it a lot easier to code, and understand, and yet i nearly always see in every example i read that other people use the method shown in example 2. Can anyone explain why please and am i better off learning to code like this?

    thank you for you're time

    EniGma
    You come to me with an idea, and I will bring it to life - Enigma

  2. Default

    Each cin that you use only reads one word up until it hits a space, so if you wrote your first and last names seperated by a space, only the first would be read. Getline reads all the words you type, until it reaches a new line.

  3. #3
    Join Date
    Oct 2014
    Location
    Gillingham Kent
    Posts
    15

    Default

    Quote Originally Posted by Rainz View Post
    Each cin that you use only reads one word up until it hits a space, so if you wrote your first and last names separated by a space, only the first would be read. Getline reads all the words you type, until it reaches a new line.
    thanks for the quick reply i see what you mean now , so i am better using the other method in example 2, but it is situational dependent on how i need the program to use the data.

    just out of interest taking on board what you said i then made this program lets call it example 3

    // practice cin & cout (what is youre name?)
    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {

    string firstName;
    string lastName;
    string fullName =firstName + lastName;

    cout << "What is your Full name? ";

    getline (cin, fullName);
    cout << "Hello, " << fullName << "!\n";
    }



    now this one works exactly the same as the first two, and uses the "getline" function. i find it a lot easier to read and understand, and to write it like this.

    so which is better now example 2 still , or this example 3??
    and also as i have demonstrated there are often many different ways in programming to accomplish the same task , so my real main question related to all this is how do i know which one is right or which one to use or does this just come from experience.

    thanks again
    Last edited by EniGma; 12-01-2014 at 09:20 PM.
    You come to me with an idea, and I will bring it to life - Enigma

  4. #4
    Join Date
    Oct 2014
    Location
    Gillingham Kent
    Posts
    15

    Default

    Ok someone else explained to me now something which I had overlooked and this is speed and efficiency of the code. Something that is very important. Basically although all 3 examples are perfectly fine and work if I were to put them all in a loop and repeat the operation multiple times example 2 would be the fastest and most efficient code because it uses less memory allocation and streams to display the info. Anyway thanks for you're help guys
    You come to me with an idea, and I will bring it to life - Enigma

  5. Default

    A further exercise you might want to do (just for coding practice) is to put a character limit in and have the program give the message "Sorry, you've exceeded the character limit, please try again."

    Follow me on Twitter and Facebook!

  6. #6
    Join Date
    Oct 2014
    Location
    Gillingham Kent
    Posts
    15

    Default

    Quote Originally Posted by MuirDragonne View Post
    A further exercise you might want to do (just for coding practice) is to put a character limit in and have the program give the message "Sorry, you've exceeded the character limit, please try again."
    Awesome idea cheers for the suggestion. But how would I go about achieving this?? here is what i have come up with so far but it doesn't work properly.

    1)If i enter my name with the correct character length the first time the program runs and displays the text "hello" followed by my "name" and exists fine.
    2) if i enter my name incorrectly the program identifies that i have made a validation mistake and asks me to re-enter my name
    3) once i re-enter my name with the correct character length instead of displaying the text "hello" followed by my "name" which is what it is supposed to do the program exits.
    4) the program also exits the second time even if i re-enter an invalid length name.

    how can i make it so the program repeats the validation check and only exits once the correct text has been displayed?? or have i gone about this all completely wrong??


    Example

    // practice cin & cout (what is youre name?)
    #include "stdafx.h"
    #include <iostream>
    #include <string>


    int main()
    {
    std::string name;

    std::cout << " Please enter youre full name,\n Names must be no longer than 20 characters in length . . ." "\n""\n";

    getline(std::cin, name);

    if (name.length() <= 20)
    {

    std::cout << "Hello, " << name << "!\n";

    }
    else if (name.length() >= 20)
    {

    std::cout << " sorry you have exceeded the character limit\n Please try again . . . ""\n""\n";

    getline(std::cin, name);

    }
    else

    return 0;
    }

    Thanks for you're help EniGma
    Last edited by EniGma; 12-02-2014 at 09:15 PM.
    You come to me with an idea, and I will bring it to life - Enigma

  7. Default

    You need a way to keep looping while the input entered is too long. Here is one way you could do it.

    int main
    {
    std::string name;

    std::cout << " Please enter youre full name,\n Names must be no longer than 20 characters in length . . ." "\n""\n";

    getline(std::cin, name);

    while(name.lenght() >= 20)
    {
    std::cout << " sorry you have exceeded the character limit\n Please try again . . . ""\n""\n";

    getline(std::cin, name);
    }
    std::cout << "Hello " << name << std::endl ;
    return 0;

  8. #8
    Join Date
    Oct 2014
    Location
    Gillingham Kent
    Posts
    15

    Default

    Quote Originally Posted by Rainz View Post
    You need a way to keep looping while the input entered is too long. Here is one way you could do it.

    int main
    {
    std::string name;

    std::cout << " Please enter youre full name,\n Names must be no longer than 20 characters in length . . ." "\n""\n";

    getline(std::cin, name);

    while(name.lenght() >= 20)
    {
    std::cout << " sorry you have exceeded the character limit\n Please try again . . . ""\n""\n";

    getline(std::cin, name);
    }
    std::cout << "Hello " << name << std::endl ;
    return 0;
    I tried using a while loop exactly like what you have suggested to do here but as soon as you enter a name that is more than 20 characters in length it makes the while statement true and the program enters an endless loop where it just keeps printing the error message to cout and doesn't wait to let the user input the correct name. I think I need some way of making the while statement false so the loop exits but I'm not sure maybe I just coded it wrong.
    Last edited by EniGma; 12-03-2014 at 05:30 AM.
    You come to me with an idea, and I will bring it to life - Enigma

  9. Default

    Hi,
    My guess would be, that your problem is that after you put too many characters in the name, and it asks you to try again, you're expecting it to jump back up a few lines after the 2nd getline function, but you dont tell it to.. So the program just reads it from top to bottom.

    Your string "name" does get changed, but it doesnt go through the "if(name.length() <= 20)" check and just continues to the bottom of the code, so you need to specifically tell it to return to top again basically.

    this is a way to do it:

    ----------------------------------------------------------------------------------------------------

    #include <iostream>
    #include <string>

    void getInput();

    int main()
    {
    std::cout << " Please enter youre full name,\n Names must be no longer than 20 characters in length . . ." "\n""\n";

    getInput();
    }

    void getInput()
    {
    std::string name;

    getline(std::cin, name);

    if (name.length() >= 20)
    {
    std::cout << " sorry you have exceeded the character limit\n Please try again . . . ""\n""\n";

    getInput();
    }
    else
    {
    std::cout << "Hello, " << name << "!\n";
    }

    }

    ----------------------------------------------------------------------------------------------------

    Obviously the program starts with main, and does the initial "Please enter name" etc.. it then calls the function "getInput()", which does what you were doing before, except when the character limit is exceeded, it calls the getInput() function again,
    meaning it goes back to the top of the getInput() function to get a new input and put it through the character length checks again.

    It's probably by no means the best way, to do things, but i'm no expert (yet ), and its 5:30 in the morning

    Anyways hope i helped

    Simon
    Last edited by Morzie; 12-03-2014 at 05:45 AM.

  10. #10
    Join Date
    Oct 2014
    Location
    Gillingham Kent
    Posts
    15

    Default

    Quote Originally Posted by Morzie View Post
    Hi,
    My guess would be, that your problem is that after you put too many characters in the name, and it asks you to try again, you're expecting it to jump back up a few lines after the 2nd getline function, but you dont tell it to.. So the program just reads it from top to bottom.

    Your string "name" does get changed, but it doesnt go through the "if(name.length() <= 20)" check and just continues to the bottom of the code, so you need to specifically tell it to return to top again basically.

    this is a way to do it:

    ----------------------------------------------------------------------------------------------------

    #include <iostream>
    #include <string>

    void getInput();

    int main()
    {
    std::cout << " Please enter youre full name,\n Names must be no longer than 20 characters in length . . ." "\n""\n";

    getInput();
    }

    void getInput()
    {
    std::string name;

    getline(std::cin, name);

    if (name.length() >= 20)
    {
    std::cout << " sorry you have exceeded the character limit\n Please try again . . . ""\n""\n";

    getInput();
    }
    else
    {
    std::cout << "Hello, " << name << "!\n";
    }

    }

    ----------------------------------------------------------------------------------------------------

    Obviously the program starts with main, and does the initial "Please enter name" etc.. it then calls the function "getInput()", which does what you were doing before, except when the character limit is exceeded, it calls the getInput() function again,
    meaning it goes back to the top of the getInput() function to get a new input and put it through the character length checks again.

    It's probably by no means the best way, to do things, but i'm no expert (yet ), and its 5:30 in the morning

    Anyways hope i helped

    Simon
    Cheers I will take a look tonight when I get home. I see what you mean about calling the function but what does the "Void" bit mean I havnt come across that yet.?? Thanks

    I've been thinking about it all day and I came up with this

    //instead of just sending the error to cout, create a bool function
    bool show_error() {
    return std::cout << "The input is too long please try again.\n";
    }
    //set the string variable length
    int main() {
    static const int maxchar = 20;
    std::string fullName;

    do {
    std::cout << "What is you're full name\n";
    } while (std::cin >> fullName && fullName.size() > maxchar && show_error());
    }


    Just need to add in the hello & name cout and I think it will work. Will try it when I get home
    Last edited by EniGma; 12-03-2014 at 01:46 PM.
    You come to me with an idea, and I will bring it to life - Enigma

Similar Threads

  1. Dev looking for code project
    By Dooby in forum T2G Students
    Replies: 7
    Last Post: 02-27-2012, 03:55 PM
  2. Code help
    By Dooby in forum T2G Developer & App, Technical Discussions
    Replies: 13
    Last Post: 01-31-2012, 03:59 PM
  3. Code and OCD
    By Meedley in forum T2G Developer, General Discussions
    Replies: 13
    Last Post: 04-16-2010, 11:11 AM
  4. GM7 Code
    By Glenfiddich in forum Game Maker
    Replies: 21
    Last Post: 03-22-2010, 03:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •