Search
 
SCRIPT & CODE EXAMPLE
 

CPP

struct and array in c++

#include <iostream>

using namespace std;

struct book{
  string name;
  string author;
  int pages;
  float price;
};

int main(){
    book books[2];
    for(int i = 0; i < 2; i++){
        cout << "Book " << i+1 << ": ";
        getline(cin, books[i].name);
        getline(cin, books[i].author);
        cin >> books[i].pages;
        cin >> books[i].price;
        cin.ignore();
    }
    
      for(int i = 0; i < 2; i++){
          cout << "Book " << i+1 << ": " << endl;
        cout << "Name: " << books[i].name << endl;
        cout << "Author: " << books[i].author << endl;
        cout << "Number of pages: " << books[i].pages << endl;
        cout << "Price: " << books[i].price << endl;
    }
}
Comment

array of struct in C++

struct Car
{
	string name;
}

Car * car = nullptr;
int count = 100;

car = new Car[count];

for (int i = 0; i < count; i++)
{
	car[i].name = "car" + i;
}

Comment

create array of structs cpp

Customer::Customer(int id, string input_name): uid(id), name(input_name) {}
Customer customerRecords[2];
customerRecords[0] = Customer(25, "Bob Jones");
custoemrRecords[1] = Customer(26, "Jim Smith");
Comment

c++ arrays in structs

//add answer
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ get whole line 
Cpp :: factorial of large number 
Cpp :: strring length in c++ 
Cpp :: how to create a file in c++ 
Cpp :: C++ :: 
Cpp :: c++ client service ros 
Cpp :: double array size c++ 
Cpp :: how to write a template c++ 
Cpp :: system cpp 
Cpp :: c++ math 
Cpp :: lower bound and upper bound in c++ 
Cpp :: How to split a string by Specific Delimiter in C/C++ 
Cpp :: install qpid broker in ubuntu/linux 
Cpp :: c++ program to convert celsius to kelvin 
Cpp :: friend function in c++ 
Cpp :: how to remove the scroll bar in pyqt6 
Cpp :: print reverse number 
Cpp :: c++ regex count number of matches 
Cpp :: C++ cout iostream 
Cpp :: c++ cin 
Cpp :: c++ stl vector get iterator from index 
Cpp :: namespace file linking c++ 
Cpp :: Split a number and store it in vector 
Cpp :: c++ class methods 
Cpp :: how to insert in a queue c++ 
Cpp :: fractional knapsack problem 
Cpp :: c++ initialize size of 3d vector 
Cpp :: unordered_map in c++ 
Cpp :: book allocation problem in c++ 
Cpp :: c++ throw index out of bound 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =