Making the Enter Key do a Return

This is a discussion on Making the Enter Key do a Return within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm writing a text editor program, but I need the enter key to go down a line instead of ...

  1. #1
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47

    Making the Enter Key do a Return

    Hi,
    I'm writing a text editor program, but I need the enter key to go down a line instead of entering in data. Does anyone have any ideas? This is what I have:

    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {
    char mytext[256];
    cin.getline(mytext, 255);

    cout<<mytext<<endl;
    }

    Thanks

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Since getline stops entering info into your array everytime a newline is inputted (based on default behaviour) you could place one manually everytime it returns. As you're using C-style strings you can strcat a '\n' each time.

  3. #3
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47
    Could you give me an example?
    Thanks

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    #include <iostream>
    #include <cstdlib>

    using namespace std;

    int main()
    {
    char mytext[256];
    cin.getline(mytext, 255);
    strcat(mytext,"\n");

    cout<<mytext<<endl;

    return 0;
    }

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    also, you'll probably have to add

    #include <cstring>

    to the preprocessor stuff.

  6. #6
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47
    Okay this is what I have now:

    #include <iostream.h>
    #include <stdlib.h>
    #include <string>


    using namespace std;

    int main()
    {
    char mytext[256];
    cin.getline(mytext, 255);
    strcat(mytext,"\n");

    system("PAUSE");
    return 0;
    }

    It still goes right to the system("PAUSE") and doesn't do a return.

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Oh, I think I understand what you mean. You'll need a character to terminate input otherwise your wordprocessor will never end -

    #include <iostream>
    #include <cstdlib>

    using namespace std;

    int main()
    {
    char mytext[256];
    // ! ends the word processor
    cin.getline(mytext, 255,'!');

    cout << mytext << endl;

    return 0;
    }

    Bear in mind your documents are limited to 255 characters with no ability to edit lines that you've already entered.

  8. #8
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47
    Hey that works perfect!
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21