Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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;
    }
}
 
PREVIOUS NEXT
Tagged: #struct #array
ADD COMMENT
Topic
Name
9+4 =