[C++]"post maker" help

This is a discussion on [C++]"post maker" help within the C++ Programming forums, part of the General Programming Boards category; I am making a very simple "post maker". I am not asking you to write the whole program for me!(mentioning ...

  1. #1
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76

    Question [C++]"post maker" help

    I am making a very simple "post maker".

    I am not asking you to write the whole program for me!(mentioning since when people see a beginning like that they think the poster didnt do a thing)

    The question that I have is if there is a way to make a thread keep "reading" even after a space is entered into the program?

    I need this to make the person using the program to input the message and it will be added and formated automaticly to a HTML file.
    But this doesnt work since it only registers the first word in the message.

  2. #2
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Can no one help?

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    You must be talking about console program. I should assume you are using std cout and cin. So i will help with this.

    If you have this kind of setup

    Code:
    string answer;
    
    cout << "This is sample input name: " << endl;
    cin >> answer;
    that code will only allow 1 word no spaces. But if you use this instead you can use spaces

    Code:
    string tmp;
    
    cout << "This is a even better sample input name: " << endl;
    getline(cin, tmp);

  4. #4
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    What is TMP?
    sorry hadnt seen the string variable declaration.

    but why do I need to put it in the () of the getline function?
    Last edited by jerimo; 01-26-2010 at 07:42 PM.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    That is just a example. You said you want what they say to be put in the html file. So you need to make there input a string. So getline(cin, tmp) sets what they input to tmp. You could have it String answer or String input if you want it does not matter. If you still need help post some of you code here for me to see

  6. #6
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    I did what you told me to do but now it either gives an error or it doesnt put the info into the file at all.

    Anyway to fix this?
    (Don't have the code with me right now but will post it in not to long)

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by jerimo View Post
    I did what you told me to do but now it either gives an error or it doesnt put the info into the file at all.

    Anyway to fix this?
    (Don't have the code with me right now but will post it in not to long)
    Posting the code will help; posting the error will help even more

    I recently (last three years) wrote something like this and the logic I set it up to hand was to read until three carriage-returns were detected. I know it sounds weird but in this exact situation it was needed. So rather that just reading a cin >> stream I just read all keyboard input, accumulated it in a string, then for each carriage return I got I bumped a counter; if something else came in during this (character for example) I reset the counter to zero. BTW here is another hint: after you think you have detected the end of the message, walk the string you have accumulated and replace \n\r with <br> for the sake of the HTML.

    I had written my own CMS which was an interesting project but eventually there were far better ones available for free and at the end of the day, I needed a tool I could use, not something else I needed to maintain.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    This the code to my project, which doesnt work properly:
    Code:
    /*********************************************
    Project: Post Maker
    Description:
    A fairly simple C++ program used to make posts
    easily for non HTML programmers, those who
    are can write directly in the file.
    Programmer: James Oliver (AKA Jerimo)
    *********************************************/
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    char name[50];
    string post;
    
    void name_maker()
    {
        ofstream chat ( "[TE]Chat.html" );
        //Checks to make sure that the chat file was opened successfully
        if ( !chat.is_open() )
        {
            cout << "A problem just happened" << '\n';
            cout << "Problem (35): File could not be opened" << '\n';
            return;
        }
        else
        {
        }
    
        chat << "<HR>" << name << "<HR>";
    }
    
    void write()
    {
        ofstream chat ( "[TE]Chat.html" );
        //Checks to make sure that the chat file was opened successfully
        if ( !chat.is_open() )
        {
            cout << "A problem just happened" << '\n';
            cout << "Problem (35): File could not be opened";
        }
        else
        {
            //file can be used safely
        }
        name_maker();
        chat << post << "<br>";
    }
    
    int main()
    {
        //to get the user's Name
        log_in_prot();
        //To get the user's Post
        cout << "Please enter your post, " << '\n';
        getline ( cin , post );
    
        //So it writes the input in the chat file
        write();
    }
    Last edited by jerimo; 01-27-2010 at 07:41 PM.

  9. #9
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Quote Originally Posted by jeffcobb View Post
    Posting the code will help; posting the error will help even more

    I recently (last three years) wrote something like this and the logic I set it up to hand was to read until three carriage-returns were detected. I know it sounds weird but in this exact situation it was needed. So rather that just reading a cin >> stream I just read all keyboard input, accumulated it in a string, then for each carriage return I got I bumped a counter; if something else came in during this (character for example) I reset the counter to zero. BTW here is another hint: after you think you have detected the end of the message, walk the string you have accumulated and replace \n\r with <br> for the sake of the HTML.

    I had written my own CMS which was an interesting project but eventually there were far better ones available for free and at the end of the day, I needed a tool I could use, not something else I needed to maintain.
    Not sure i understand what you meant when you described the system you used(Im only starting the advanced part of the tutorials on this site)

  10. #10
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Noone has an answer to my problem

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h>
    using namespace std;
    
    int main()
    {
        int blanks=0;
        string post, name, line;
        ofstream outf("post.html");
    
        if(!outf)
        {
    	cerr << "couldn't open file" << endl;
    	exit(1);
        }
    
        //get name
        cout << "Enter your name: ";
        getline(cin, name);
        outf << "<HR>" << name << "<HR>" << endl;
    
        cout << "Enter your post (add 2 blank lines to finish) " << endl;
        //get post (reads till empty line)
        while(1)
        {
    	getline(cin, line);
    	if(line == "")
    	{
    	    blanks++;
    	    if(blanks==2)
    		break;
    	}
    	else
    	{
    	    blanks=0;
    	    outf << line << endl;
    	}
    	outf << "<BR />" << endl;
        }
        outf.close();
        return 0;
    }

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Be more specific - WHY does it not work?
    Is it a compile error (if so, what error)?
    Does it not behave as it should when you run (if so, what is happening)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    I had posted a whole load of logic and stuff up for this earlier and the last crash/restore ate it. Sadly I really don't feel like retyping all of that ATM...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    And so you thought you would make a reply about that you lost everything or you thought you'd reserve a post to type it up all again at a later date?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by Elysia View Post
    And so you thought you would make a reply about that you lost everything or you thought you'd reserve a post to type it up all again at a later date?
    Yes. I was extremely tired when I wrote that, not that it is an excuse for being oblique. I guess I was saying I would retype it if no one else helped him/her in advance. Maybe 10pct of that was frustration of seeing his/her plaintive "why won't someone help me?" when I had put a decent amount of effort into doing so only to find it flushed down the toilet...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need a programmer [C or C++] for free
    By carlo67 in forum Projects and Job Recruitment
    Replies: 11
    Last Post: 08-24-2007, 05:02 PM
  2. [C# COM development] output by Regasm
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 07-22-2007, 01:12 PM
  3. [C Video Tutorial Collection]
    By Chernobyl in forum C Programming
    Replies: 17
    Last Post: 04-07-2007, 01:24 AM
  4. [C colors]
    By FEAR in forum C Programming
    Replies: 1
    Last Post: 06-06-2006, 02:24 PM

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