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

c++ arrays in structs

//add answer
Comment

PREVIOUS NEXT
Code Example
Cpp :: vector of strings initialization c++ 
Cpp :: matplotlib hide numbers on axis 
Cpp :: insert vector to end of vector c++ 
Cpp :: how to get size of char array in c++ 
Cpp :: c++ string contains 
Cpp :: how to make a list in c++ 
Cpp :: c++ get full line of input 
Cpp :: max element in array c++ stl 
Cpp :: vector fin element c++ 
Cpp :: cpp initialize multidimensional vector 
Cpp :: segmented sieve of Eratosthenes in cpp 
Cpp :: string to decimal c++ strtol 
Cpp :: c++ programming language 
Cpp :: convert integer to string c++ 
Cpp :: vector size for loop 
Cpp :: sort a 2d vector c++ stl 
Cpp :: Quicksort taking random pivot 
Cpp :: c++ splitstring example 
Cpp :: how to find the sum of a vector c++ 
Cpp :: cpp loop through object 
Cpp :: hello world in c++ 
Cpp :: built in function in c++ for binary to decimal 
Cpp :: change colour of output to terminal c++ 
Cpp :: pointer in return function c++ 
Cpp :: str remove char c++ 
Cpp :: how to empty string c++ 
Cpp :: priority queue in c++ 
Cpp :: struct c++ 
Cpp :: after login redirect to dashboard in nuxt 
Cpp :: c++ finding gcd 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =