程序跳过一行代码 [英] program skipping a line of code
问题描述
我一直在这个程序工作了一段时间,我终于摆脱了编译错误。但是当我尝试时,程序基本上跳过了一行代码。
I have been working on this program for a while and I finally got rid of the compile errors. But when I tried it, the program basically skipped a line of code.
#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;
}
你看到如何在询问这是你的索引页后,跳过下一个<
You see how after asking if this is your index page it skips the next cin.getline
function no matter the scenario.
推荐答案
当用户输入时,输入code> cin.getline 索引,他们也键入换行符,但您的cin没有从输入流中删除它。因此,您对cin.getline的调用将因为剩余的换行符而立即返回。
When the user entered the index, they also typed a newline, but your cin didn't remove it from the input stream. So, your call to cin.getline returns immediately because of the leftover newline.
添加对 cin.ignore 之前,cin.getline将其刷新。
Add a call to cin.ignore before the cin.getline to flush it out.
这篇关于程序跳过一行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!