| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Strings input
is there any way to include whitespace in string inputs without using character arrays ?
without using this ? char name[50] getline(name,50); ? cout<<"enter first and last name"; cin>>name cout<<name "John Smith" |
|
#2
|
||||
|
||||
|
use this
Code:
string str; getline(cin,str); this should work fine as long as you have a newer compiler.
__________________
---Official Member Of The Itsacon Fan Club--- ![]() Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer. |
|
#3
|
|||
|
|||
|
dev -C++ from bloodshed.net fall into that "newer compiler" category?
|
|
#4
|
||||
|
||||
|
Yes that should do fine, thats the one I use and it works properly.
Just plug that in and see what you get. Just add a cout<<str<<endl; to print the results. |
|
#5
|
|||
|
|||
|
hey since you use that maybe you could help me with a problem, everytime i compile something after it outputs, the dos window automatically closes, how do i set it to where it asks to press any key to manually close the dos window like visual basic c++ ?
|
|
#6
|
||||
|
||||
|
The reason being is because after your code is finished the program sends a return 0; to the computer which in short means everything went ok, so close. If you put a system("PAUSE"); in there or another cin>> statement in there it will pause until the user says so. For example.
Code:
int main()
{
char ch;
string str;
cout<<"Enter some text\n";
getline(cin,str);
cout<<"You entered:\n";
cout<<str<<endl;
cout<<"Would you like to exit the program now? (Y/N) : ";
cin>>ch;
if(ch == 'N' || ch == 'n')
{
return 0;
}
else
{
cout<<"Oh well Im closing anyways, he he\n";
}
cout<<"Now look at what system(\"PAUSE\") does \n";
system ("PAUSE");
return 0;
}
here is an example that shows two ways to exit a program without it closing automatically on you. |
|
#7
|
|||
|
|||
|
coo thanx
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Strings input |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|