Thread: Help with my notepad.

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    16

    Help with my notepad.

    I'm making a little notepad, and what I got is a single line input. Now I want it to be multiline. Basically, when the user presses the enter key, it should create a new line on the output too. It may sound vague, here is the source code:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    string text;
    string e;
    
    int write() {
    cin >> text;
    ofstream file ("text.txt", ios::app);
    file << text << " ";
    }
    
    int main() {
          cout << "Text: ";
          write();
    while (text != e) {
          write();
    }
    cin.get ();
    return 0;
    }
    Can I let it recognise an input of the "enter" key?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    string e;
    
    ...
    
    while (text != e)
    Where is e initialized?

    Quote Originally Posted by thelimpkid
    Can I let it recognise an input of the "enter" key?
    You'll have to use a different method of getting input. cin >> will strip any whitespace (tabs/spaces/newlines) when attempting to read a string. Maybe you could try getline(cin,text). If string is zero length, then user entered a newline.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Here's some changes/things I noticed.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    string text;
    string e; //<-- 'e' is never given a value
    
    int write() {
    cin >> text;
    ofstream file ("text.txt", ios::app);
    file << text << " ";
    }
    
    int main() {
          cout << "Text: ";
          write();
    while (text != e) { //<-- since 'e' is never given a value it's almost like saying "while not equal
          write();            // to nothing"
    }
    cin.get ();
    return 0;
    }
    Using what hk_mp5 said try this...

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    char text[255];
    string e;
    
    int write() {
    cin.getline(text,255);
    ofstream file ("text.txt", ios::app);
    file << text << " ";
    }
    
    int main() {
          cout << "Text: ";
          write();
    while (text != e) { //i dont know what you're trying to do with e here so i left this
          write();
    }
    cin.get ();
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Using what hk_mp5 said try this...
    No reason to change to a C style string. There is a version of getline for C++ strings.
    Code:
    getline(cin, text);
    Of course, now istead of outputting a space after the text you would output the newline character.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    16
    wow, thanks alot! I got it all wrong I see. Well I wanted to create a loop that would never end. That's what I tried with e.

    Thanks alot for the help!

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well I wanted to create a loop that would never end.
    Code:
    while (true) {
        ...
        if (...) break;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    or
    Code:
     for(;;)
    -- just as an alternative - they are pretty much the same thing... Some people seem to think that whiel(1)/while(true) is easier to read/understand - but if you spend enough time programming and reading other peoples code, it's just a matter of time until you find a for-statement with "nothing" in the parenthesis but semicolons - and that makes a loop with no end condition.

    --
    Mats

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Ideally there would be a "forever" keyword, as in
    Code:
    forever {
      // code
    }
    with no "arguments" (is that the right word?), but since there isn't, while (true), which has just one, is conceptually the closest thing available, so I prefer that, though the for version has fewer characters.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    16
    Here's what I've got now:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    string text;
    
    int write() {
    getline(cin, text);
    if (text == "newline") {
    ofstream file ("text.txt", ios::app);
    file << endl;
    }
    else {
    ofstream file ("text.txt", ios::app);
    file << text << " ";
    }
    }
    int main() {
          cout << "Text: ";
          write();
    while (true) {
          write();
    }
    cin.get();
    return 0;
    }
    Can I replace "newline" with the "[enter key]"?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Go back to your original write() function at the top of this thread.

    Change cin >> text to getline (cin, text). That will change your code from reading in a single word to reading in a whole line.

    Then, change the file << text << " "; line to output text followed by the newline character instead of the space character. You're using " " there, but you don't want to do that, you want to use '\n' because '\n' is the newline character. The only reason you need this is because getline discards the newline character from the user input. The reason you had " " before was because cin >> discards spaces from the user input.

    Those two things are all you needed to change in your write() function.

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    16
    Hmm, I got that right now:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    string text;
    
    int write() {
    cin >> text;
    ofstream file ("text.txt", ios::app);
    file << text << endl;
    }
    
    int main() {
          cout << "Text: ";
          write();
    while (true) {
          write();
    }
    cin.get ();
    return 0;
    }
    I want to write lines, and I want to break the line when I press the enter key. I don't want to break the line after every word. I'm sorry if I wasn't clear enough about it. I just want a basic notepad so I can develop it to a news updating app.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You still need to use getline instead of cin >>. The other part is correct.

  13. #13
    Registered User
    Join Date
    Jul 2007
    Posts
    16
    woops, yea, so this:
    Code:
    int write() {
    getline (cin, text);
    ofstream file ("text.txt", ios::app);
    file << text;
    }
    Now it's still single line. How can I make it multiline?

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why did you change the second (output) part? It was correct.

  15. #15
    Registered User
    Join Date
    Jul 2007
    Posts
    16
    Code:
    int write() {
    getline (cin, text);
    ofstream file ("text.txt", ios::app);
    file << text << endl;
    }
    If you mean this, it's not what I want to do with it. I don't want it to have each word on a new line. I want to create a newline when I press the "enter" key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Annoying Escape Character Squares in Notepad
    By firetheGlazer in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 07-18-2008, 02:17 AM
  2. Strange problem with notepad
    By IceDane in forum C Programming
    Replies: 15
    Last Post: 09-10-2007, 09:46 AM
  3. text files & notepad problem
    By bigtamscot in forum C Programming
    Replies: 2
    Last Post: 05-01-2003, 04:41 PM
  4. notepad
    By denny in forum C Programming
    Replies: 1
    Last Post: 04-15-2002, 11:32 AM