سؤال
لقد كنت أعمل في هذا البرنامج لفترة من الوقت ، وأخيراً تخلصت من أخطاء التجميع. ولكن عندما جربته ، تخطى البرنامج أساسًا خطًا من الكود.
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(){
string nameOfFile = "";
char index;
char title[100];
char name[100];
char copyright[100];
cout << "Welcome, and hello to the html templating software" << endl;
cout << "Is this your index page?\ny/n" << endl;
cin >> index;
if (index=='n'){
cout << "Enter the prefered name of this file" << endl;
getline(cin, nameOfFile, '\n');
}
cout << "What will the title of the page be?" << endl;
cin.getline(title, 100);
cout << "What is your name?" << endl;
cin.getline(name, 100);
cout << "What is the copyright?" << endl;
cin.getline(copyright, 100);
cin.get();
return 0;
}
ترى كيف بعد السؤال عما إذا كانت هذه هي صفحة الفهرس الخاصة بك ، فإنها تتخطى التالي cin.getline
وظيفة بغض النظر عن السيناريو.
المحلول
عندما دخل المستخدم الفهرس ، قاموا أيضًا بكتابة سطر جديد ، لكن CIN لم يزيله من دفق الإدخال. لذا ، فإن مكالمتك إلى Cin.getline تعود فورًا بسبب Leftover Newline.
أضف مكالمة إلى Cin.ignore قبل cin.getline لطردها.
نصائح أخرى
استبدل GetLine (CIN ، NameOffile ، ' n')
مع
CIN >> nameOffile
لا تنتمي إلى StackOverflow