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; }
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.
Bye using 'cin.getline' did you mean replace 'cin' with 'cin.getline'? because that, err, didn't work.
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.
Ok, i got it . 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?
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 but in DevC++ it works just fine...
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?
Sorry, ignore that, the mention of VS2005 makes it clear you're using Windows. These could be what you're after: http://msdn.microsoft.com/en-us/library/ms682073.aspx
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];