When I used a simple code example from my text book:
This is how the program reacted to the white space in the string "Angry Grandpa":
![Name: WhitespaceError.jpg
Views: 57
Size: 23.4 KB]()
How do I fix this behavior, so that a white space is allowed (for both my friends' first and last name to be seen as one string)?
Code:
//This program writes user input to a file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream outputFile;//create object for writing data to file
string name1, name2, name3;
//open an output file:
outputFile.open("Friends.txt");
//Get the names of three friends:
cout << "Enter the names of three friends.\n";
cout << "Friend 1: ";
cin >> name1;
cout << "Friend 2: ";
cin >> name2;
cout << "Friend 3: ";
cin >> name3;
//Write the names to the file:
outputFile << name1 << endl;
outputFile << name2 << endl;
outputFile << name3 << endl;
cout << "The names were saved to a file.\n";
//Close the file:
outputFile.close();
system("pause");
return 0;
}
How do I fix this behavior, so that a white space is allowed (for both my friends' first and last name to be seen as one string)?