Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: Code VS Code

  1. Default

    Ah sorry.. void is just a return type for the function, which in this case means it doesnt return anything. Like "int main()" returns an int: "return 0" etc.
    Don't worry, functions will get covered quite early on in your learning.

    To remove that specific confusion i did a different way:

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

    #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)
    {
    do
    {

    std::cout << " sorry you have exceeded the character limit\n Please try again . . . ""\n""\n";
    getline(std::cin, name);

    } while (name.length() >= 20);

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


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

    Hopefully this is easier to follow, as it does what you were doing: if name is too long it enters the do/while loop which will repeat the "sorry too long try again" until name.length() is less than 20.

    If you get it right first time (as in name length less than 20) it skips the do/while and jumps straight to the "else" part: std::cout << "Hello, " << name << "!\n";
    but if you enter the do/while loop, and then get out by doing it right on a following attempt, it wouldn't then go to the "else" part,
    so when you escape from the do/while loop (which obviously means your name length is less than 20), there is the: std::cout << "Hello, " << name << "!\n";

    --

    Also, I don't believe your:
    bool show_error() {
    return std::cout << "The input is too long please try again.\n";
    }

    is valid, because like void/int etc from earlier, bool would be the return type of the function.. yet "cout" doesnt return a bool, so you will probably get errors

    --

    I didnt try Rainz's code until now, as i thought you did, but that work's too and is much more elegant than mine .. did you do Rainz code exactly as he wrote it?(but with the #includes and extra bracket/bracer obvioulsy),
    if your while loop just kept repeating the cout error without stopping to let you have another attempt, are you sure you put another "getline" function in the while loop?
    Last edited by Morzie; 12-03-2014 at 04:39 PM.

  2. #12
    Join Date
    Oct 2014
    Location
    Gillingham Kent
    Posts
    15

    Default

    Quote Originally Posted by Morzie View Post
    Ah sorry.. void is just a return type for the function, which in this case means it doesnt return anything. Like "int main()" returns an int: "return 0" etc.
    Don't worry, functions will get covered quite early on in your learning.

    To remove that specific confusion i did a different way:

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

    #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)
    {
    do
    {

    std::cout << " sorry you have exceeded the character limit\n Please try again . . . ""\n""\n";
    getline(std::cin, name);

    } while (name.length() >= 20);

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


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

    Hopefully this is easier to follow, as it does what you were doing: if name is too long it enters the do/while loop which will repeat the "sorry too long try again" until name.length() is less than 20.

    If you get it right first time (as in name length less than 20) it skips the do/while and jumps straight to the "else" part: std::cout << "Hello, " << name << "!\n";
    but if you enter the do/while loop, and then get out by doing it right on a following attempt, it wouldn't then go to the "else" part,
    so when you escape from the do/while loop (which obviously means your name length is less than 20), there is the: std::cout << "Hello, " << name << "!\n";

    --

    Also, I don't believe your:
    bool show_error() {
    return std::cout << "The input is too long please try again.\n";
    }

    is valid, because like void/int etc from earlier, bool would be the return type of the function.. yet "cout" doesnt return a bool, so you will probably get errors

    --

    I didnt try Rainz's code until now, as i thought you did, but that work's too and is much more elegant than mine .. did you do Rainz code exactly as he wrote it?(but with the #includes and extra bracket/bracer obvioulsy),
    if your while loop just kept repeating the cout error without stopping to let you have another attempt, are you sure you put another "getline" function in the while loop?

    Quote Originally Posted by Morzie View Post
    Ah sorry.. void is just a return type for the function, which in this case means it doesnt return anything. Like "int main()" returns an int: "return 0" etc.
    Don't worry, functions will get covered quite early on in your learning.

    To remove that specific confusion i did a different way:

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

    #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)
    {
    do
    {

    std::cout << " sorry you have exceeded the character limit\n Please try again . . . ""\n""\n";
    getline(std::cin, name);

    } while (name.length() >= 20);

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


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

    Hopefully this is easier to follow, as it does what you were doing: if name is too long it enters the do/while loop which will repeat the "sorry too long try again" until name.length() is less than 20.

    If you get it right first time (as in name length less than 20) it skips the do/while and jumps straight to the "else" part: std::cout << "Hello, " << name << "!\n";
    but if you enter the do/while loop, and then get out by doing it right on a following attempt, it wouldn't then go to the "else" part,
    so when you escape from the do/while loop (which obviously means your name length is less than 20), there is the: std::cout << "Hello, " << name << "!\n";

    --

    Also, I don't believe your:
    bool show_error() {
    return std::cout << "The input is too long please try again.\n";
    }

    is valid, because like void/int etc from earlier, bool would be the return type of the function.. yet "cout" doesnt return a bool, so you will probably get errors

    --

    I didnt try Rainz's code until now, as i thought you did, but that work's too and is much more elegant than mine .. did you do Rainz code exactly as he wrote it?(but with the #includes and extra bracket/bracer obvioulsy),
    if your while loop just kept repeating the cout error without stopping to let you have another attempt, are you sure you put another "getline" function in the while loop?

    no like i said earlier i was at work and hadn't had a chance to try the code i was just theoretically trying to do it lol. I just tried both versions of code and they work perfectly. thanks for you're help both of you. i guess when i tried to do my own version i had my while statement in the wrong place.

    NOW a couple more things i really wanted to do with this program.

    1)firstly it occurred to me , great we are validating the length to stop someone spamming. but we are not validating the actual letters. for example i could enter my name as 11111 and as long as it met the condition of being under 20 char in length the program would cout "hello" "11111" and the same if i entered random symbols like @@@;;;;;;
    so is there a way we can make it so they can only enter the letters a to z and space???

    2)Secondly say i wanted the user to be able to enter more than one name. and create a list of names, have the names stored, and then when the user wanted to , either print them in a table to the output window altogether or select any single individual name from the stored names. eg Name 1, name 2, name 3, . a dialogue comes up asking would you like to print all names, if i enter yes it prints all 3 names , if i say no it then asks which names would you like to print.

    i think this would maybe require an array or maybe a multi array but i have not really dabbled in anything like that yet. so if anyone has any tips or can point me in the right direction. i would be great full. thanks
    Last edited by EniGma; 12-03-2014 at 05:17 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
  •