1

In my program at the end I have used getline(cin, answer5) gets closed. I want to save the strings that I use as answers for the questions with using spaces between words to a notepad.

 #include <iostream>
 #include <string>
 #include <fstream>

 using namespace std;
 int main()
 {
    string ques1 = "Client's Name :";
    string ques2 = "Client's Address :";
    string ques3 = "Mobile Number :";
    string ques4 = "ID No :";
    string ques5 = "Job Description :";

    string answer1;
    string answer2;
    int answer3;
    string answer4;
    string idc;
    string answer5;

    cout << ques1 << endl;    
    getline(cin, answer1);

    cout << ques2 << endl;   
    getline(cin, answer2);

    cout << ques3 << endl;
    cin >> answer3;

    if (cin.fail()) {
        cout << "Enter Numbers Only.." << endl;
    }
    else {
        cout << ques4 << endl;
    }

    cin >> answer4;

    if (answer4.length() != 9) {
       cout << "Incorrect ID No.." << endl;
    }
    else
    {    
       idc.append(answer4);
       idc.append("v");
    }

    cout << ques5 << endl;
    getline(cin, answer5);

    ofstream saveFile("Save1.txt");

    saveFile << answer1 << endl
             << answer2 << endl
             << answer3 << endl
             << idc << endl
             << answer5;

    saveFile.close();

    system("pause>null");  
    return 0;
}
0x499602D2
  • 87,005
  • 36
  • 149
  • 233
WESTRUK
  • 113
  • 1
  • 1
  • 5

1 Answers1

0

put cin.ignore(); after this line :

cin>>answer4;
uchar
  • 2,396
  • 4
  • 24
  • 47