4

私はしばらくの間このプログラムに取り組んできました、そして私はついにコンパイルエラーを取り除きました。しかし、私がそれを試したとき、プログラムは基本的にコード行をスキップしました。

#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、シナリオに関係なく次の関数をスキップする方法がわかります。

4

2 に答える 2

5

ユーザーがインデックスを入力すると、改行も入力されましたが、cinは入力ストリームから改行を削除しませんでした。したがって、改行が残っているため、cin.getlineへの呼び出しはすぐに戻ります。

cin.getlineの前にcin.ignoreの呼び出しを追加して、フラッシュします。

于 2010-11-22T01:53:34.293 に答える
0

getline(cin、nameOfFile、'\ n')を置き換えます

cin >> nameOfFile

于 2010-11-22T01:55:24.070 に答える