0

been studying some c++ lately and i recently bumped into classes and object, was making this database for cars and i stepped in a problem: in the for cycle, if i write a lil too much in the string sequence it just writes every question rapidly without giving me time to answer the other one, for example if i write "alfa romeo" it just write all the question down and closes the program.

#include <iostream>
#include <fstream>
using namespace std;

class Car {
public:
string mc, m;
int ye;
};

int main() {
    int x, y;
    ofstream myfile;
    myfile.open("Lista Auto");
    cout<<"Quante autovetture vuoi memorizzare?"<<endl;
    cin>>x;
    Car autox;
    
    for(y=1;y <= x; y++){
        cout<<"Inserire la marca dell'autovettura."<<endl;
        cin>>autox.mc;
        cout<<"Inserire il modello dell'autovettura."<<endl;
        cin>>autox.m;
        cout<<"Inserire l'anno di produzione."<<endl;
        cin>>autox.ye;
        myfile<<autox.mc<<" "<<autox.m<<" "<<autox.ye<<endl;
        
    }
    cout<<"Il tuo file è memorizzato nella cartella.";
    myfile.close();
    system("pause");
    return 0;
}
Lazarus
  • 9
  • 1
  • `cin>>autox.mc;` -- This stops at the first space character, so "alfa romeo" will only be "alfa" when you inspect `autox.mc`. – PaulMcKenzie Oct 06 '21 at 21:02

0 Answers0