T
T
The_Beginer2020-06-15 14:57:02
C++ / C#
The_Beginer, 2020-06-15 14:57:02

How to add a char array of 2 or more words to a structure?

Actually, the problem with adding to the structure ... If I add one word to Enter the manufacturer, for example Ford, then everything will be displayed without problems.
But if I enter Lamborgini Diablo, then after pressing Enter, the program ends.
Tried cin.getline(kolcar->namecar), getline(cin,kolcar->namecar) but the error didn't go away

#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>

struct car 
{
  char namecar[80];
  int godvipuska;
};

using namespace std;

int main()
{
  setlocale(LC_ALL, "Russian");
  
  int kolvoavt = 0;
  string str1;
  cout << "Сколько автомобилей поместить в каталог ?: ";
  cin >> kolvoavt;
  car *kolcar = new car[kolvoavt];
  for (int it = 0; it < kolvoavt; it++)
  {
    cout << "Автомобиль #" << it + 1 << endl;
    cout << "Введите производителя: "<<endl;
    cin>>kolcar->namecar;
    cout << "Укажите год выпуска: "<<endl;
    cin >> (*kolcar).godvipuska;
    kolcar++;
  }
  cout << "Вот ваша коллекция :" << endl;
  for (int it = 0; it < kolvoavt; it++)
  {
    kolcar--;
    cout << kolcar->godvipuska <<" "<< kolcar->namecar<<endl;
  }
  delete kolcar;
  system("pause");
    return 0;
}


5ee76174a8632496408607.jpeg
5ee7617990592184451861.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Flysink, 2020-06-15
@The_Beginer

You need to use getline instead of char - string
getline(cin >> ws, kolcar->namecar);
And free the memory accordingly:
delete[] kolcar;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question