2010-11-22 17 views
4

私はこのプログラムをしばらく使っていましたが、ついにコンパイルエラーを取り除きました。しかし、私が試したとき、プログラムは基本的にコード行をスキップしました。あなたは、これはあなたのインデックスページであるかどうかを尋ねる後、それは関係なく、シナリオ次cin.getline機能をスキップしていないかを確認プログラムがコード行をスキップする

#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; 
} 

答えて

5

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

cin.getlineの前にcin.ignoreに電話をかけてフラッシュしてください。

0

私はこれを行うことはできません

CIN >> nameOfFile

+0

でのgetline(CIN、nameOfFile、 '\ n' は)

を置き換える私はスペースを取得する必要があります –

関連する問題