getline will not let me enter text

1

I am using the following piece of code in which I enter a string and it is saved in the file but it does not leave any character in the string because it does not pause, it executes the instructions that go after

 const size_t longitud = 100;
 char nota[longitud];
 fstream archivo(NOMBRE_ARCHIVO.c_str());
 if(archivo.good()) {
   archivo.open(NOMBRE_ARCHIVO.c_str(), fstream::in | fstream::out | fstream::app);
 } else {
   archivo.open(NOMBRE_ARCHIVO.c_str(), fstream::in | fstream::out | fstream::trunc);
 }

 cout << "\n\t  Ingrese la nota porfavor" << " ." << endl;
 cin.getline(nota, longitud);
 archivo << nota << endl;
 archivo.close();
 break;
    
asked by soldat25 18.02.2017 в 18:58
source

1 answer

1

I think your mistake is here:

fstream archivo(NOMBRE_ARCHIVO.c_str());

change it for example by fstream archivo; .

link

This is for you to do a check is a simple test based on your code, if this works maybe the error is in another part of your code.

#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, char** argv) {

    const size_t longitud = 100;
    char nota[longitud];

    fstream archivo; //<- Cambiar

    if(archivo.good()) {
       archivo.open("/home/SuHome/t.txt", fstream::in | fstream::out | fstream::app); //Ponga la ruta de su home para test

     } else {

       archivo.open("/home/SuHome/t.txt", fstream::in | fstream::out | fstream::trunc); //Ponga la ruta de su home para test
     }

     cout << "\n\t  Ingrese la nota porfavor" << " ." << endl;

     cin.ignore();// A mi que me funciono sin esto.

     cin.getline(nota, longitud);
     archivo << nota << endl;
     archivo.close();

return 0;
}

This should create a file without problems.

P.D: Check that you adjust this part for your SO - > archivo.open("/home/SuHome/t.txt".... using your user directory.

    
answered by 19.02.2017 / 02:15
source