Pergunta

Trabalho neste programa há um tempo e finalmente me livrei dos erros de compilação. Mas quando eu tentei, o programa basicamente pulou uma linha de código.

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

Você vê como depois de perguntar se esta é a sua página de índice, ele pula o próximo cin.getline função não importa o cenário.

Foi útil?

Solução

Quando o usuário inseriu o índice, eles também digitaram uma nova linha, mas seu CIN não o removeu do fluxo de entrada. Portanto, sua chamada para Cin.getLine retorna imediatamente por causa da restante nova linha.

Adicionar uma chamada para Cin.ignore Antes do Cin.GetLine para liberá -lo.

Outras dicas

Substitua GetLine (CIN, Nameoffile, ' n')

com

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top