My wannabe chat program has some errs...

Discussion in 'C++' started by Seismosaur, Jun 28, 2007.

  1. Seismosaur

    Seismosaur New Member

    Joined:
    Jun 28, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    I am witing a pretty basic program that just displays your name in brackets, and what you typed. It is a console program (At least, that is what i THINK you call it :/) but when you type something in, it leaves what you typed, and only let's you type one word at a time. I'm pretty sure the second problem is because the variable for your text is a String variable, but i didn't know any other kind of variable that would work. Here is the code:
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        int in = 1;
        string name;
        string sentence;
        char go;
            
        cout << "Welcome to chat one, please enter your name: " << endl;
        cin >> name;
        
        cout << "Welcome " << name << ", I hope you enjoy the chat ^_^ type y to begin." << endl;
        cin >> go;
        
        system(("Cls"));
        
        while (in == 1)
        {
              cin >> sentence;
              
              cout << "\a[" << name << "]:"<< sentence << endl;
        }
        
    return 0;    
    
    }
    
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    The thing you are pretty sure off is not the correct thing. It should be string but for getting a line you should be using cin.getline and cin>> will just discard anything after the space.

    Now after that if you have problems that your program does not wait for the input then you should use the flush to clear the input buffer.
     
  3. Seismosaur

    Seismosaur New Member

    Joined:
    Jun 28, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Flush? (I said i was a n00b :rolleyes:) O, ill do that, thanx.
     
  4. Seismosaur

    Seismosaur New Member

    Joined:
    Jun 28, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Bye using 'cin.getline' did you mean replace 'cin' with 'cin.getline'? because that, err, didn't work.
     
  5. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    I thought you have some documentation with the compiler you are using and so letting you know the function will help you out in knowing. Here is the signature of how getline looks

    istream& getline (char* s, streamsize n );
    istream& getline (char* s, streamsize n, char delim );
    char* s - string or character array
    streamsize n - Size of the array or something that can hold the length of the sentence.
    char delim - should be \n for newline as is in your case.
     
  6. Seismosaur

    Seismosaur New Member

    Joined:
    Jun 28, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    ugn... what does that mean?
     
  7. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    How you should be using the cin.getline
     
  8. Seismosaur

    Seismosaur New Member

    Joined:
    Jun 28, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Ok, i got it :D. But now i have one more question. How do you make the cursor stay in one spot?
    like:
    | <--- <the cursor starts here.> Hello| <--- cursor ends here with the inputted text.
    ________________________
    |[<<name<<]:Hello <--- text output

    How do i make the input text disappear, and the cursor go back to its original position?
     
  9. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    That would be pretty complex for the thingy you mentioned earlier.
     
  10. Seismosaur

    Seismosaur New Member

    Joined:
    Jun 28, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Yers i know, but how could it be done? I can't find any tuts on this, lol.
     
  11. mutantkeyboard

    mutantkeyboard New Member

    Joined:
    Nov 30, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    well I have had similar problems with cin.get and cin.getline
    before I included cstring
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std; // for example
    
    this was happening in visual studio 2005 and I still don't know why :D
    but in DevC++ it works just fine...
     
  12. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    This is dependent on your OS, such functions are not present in standard C so you will need to use some platform specific library. What OS are you using?
     
  13. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
  14. hkp819

    hkp819 New Member

    Joined:
    Dec 4, 2008
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    I am confident about that your problem is in declaration of
    string name;
    string sentence;

    the correct declaration of string name and and string sentence is :------

    char name[30];
    char sentence[80];
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice