Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Allocating Memory Problem

Here are some projects of actual programs you can practice making.

Allocating Memory Problem

Postby Jack Sparrow on Tue Sep 15, 2009 5:37 am

Hi guys, how are you?

Would anyone please write the following program:
Code: Select all
Design a structure called car that holds the following information about an automobile:
its make, as a string in a character array or in a string object, and the year it was built,
as an integer. Write a program that asks the user how many cars to catalog. The program
should then use new to create a dynamic array of that many car structures. Next, it
should prompt the user to input the make (which might consist of more than one word)
and year information for each structure. Note that this requires some care because it
alternates reading strings with numeric data (see Chapter 4). Finally, it should display
the contents of each structure. A sample run should look something like the following:
How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser


Here's what I've been desperately and unsuccessfully trying to do:
Code: Select all
#include "stdafx.h"
#include <iostream>
#include <string>



struct cars
{
   string maker;
   int year;
};


using namespace std;

int main()
{
   cout <<"How many cars do you wish to catalog; ";
   int user;
   cin >> user;
   cars member[user];
   int counter = 0;
   for(counter; counter < user; counter++)
   {
      system("cls");
      cout << "\nMaker: ";
      getline(cin, member[counter].maker);
      cout <<"\nYear: ";
   }
   counter = 0;

   for(counter; counter < user ; counter++)
   {
      cout << "Here's your catalog: \n";
      cout<< member[counter].year <<" "<< member[counter].maker <<endl;
   }

   system("pause");
   return 0;
}


Obviously I haven't included the new thing at all, so would anyone please make this work and hopefully explain how creating run-time storage and putting data there works:)

Regards,
Jack
Jack Sparrow
 
Posts: 4
Joined: Mon Sep 07, 2009 12:53 pm

Re: Allocating Memory Problem

Postby C++ on Tue Sep 15, 2009 5:46 pm

Well all right, since you took the effort of trying yourself :)

Code: Select all
// car.h

#ifndef CAR_H
#define CAR_H

#include <iostream>
#include <string>
#include <limits>


struct Car
{
private:
   std::string make;
   int year;
   static int cars;

public:
   Car();
   friend std::ostream & operator<<(std::ostream &, const Car &);
   static int getInput(const char *);
};

#endif


Code: Select all
// car.cpp

#include "car.h"

using std::cout;
using std::cin;
using std::ostream;
using std::streamsize;
using std::numeric_limits;


int Car::cars = 0;

Car::Car()
{
   ++cars;
   cout << "Car #" << cars << ":\nPlease enter the make: ";
   getline(cin, make);
   year = getInput("Please enter the year made: ");
}

ostream & operator<<(ostream &o, const Car &car)
{
   o << car.year << ' ' << car.make << '\n';
   return o;
}

int Car::getInput(const char *msg)
{
   int input;

   while (cout << msg && !(cin >> input))
   {
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
   }
   cin.ignore(numeric_limits<streamsize>::max(), '\n');
   return input;
}


Code: Select all
// main.cpp

#include "car.h"

using std::cout;
using std::cin;
using std::nothrow;


int main()
{
   size_t cars = Car::getInput("How many cars do you wish to catalog? ");
   Car *pcars = new (nothrow) Car[cars];

   if (pcars)
   {
      cout << "Here is your collection:\n";
      for (size_t i = 0; i < cars; ++i)
         cout << pcars[i];
      delete[] pcars;
   }
   else
      cout << "Memory allocation error!\n";
   cin.get();
}
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: Allocating Memory Problem

Postby Jack Sparrow on Thu Sep 17, 2009 6:43 am

Thanks a lot for this man, but seems pretty old school to me
Jack Sparrow
 
Posts: 4
Joined: Mon Sep 07, 2009 12:53 pm

Re: Allocating Memory Problem

Postby C++ on Thu Sep 17, 2009 8:55 am

Jack Sparrow wrote:Thanks a lot for this man, but seems pretty old school to me


I did exactly what was asked in the program requirements.
If it seems so old school to you, then I wonder why you couldn't solve this one yourself, or improve my 'old school' solution? Laziness? :roll:
Well thanks for letting me know that you won't be needing my help again. Good luck with your state of the art programming techniques!
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: Allocating Memory Problem

Postby noobgrammer on Thu Sep 17, 2009 12:44 pm

I thought structures couldn't be private
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: Allocating Memory Problem

Postby Jack Sparrow on Fri Sep 18, 2009 3:55 am

C++ wrote:
Jack Sparrow wrote:Thanks a lot for this man, but seems pretty old school to me


I did exactly what was asked in the program requirements.
If it seems so old school to you, then I wonder why you couldn't solve this one yourself, or improve my 'old school' solution? Laziness? :roll:
Well thanks for letting me know that you won't be needing my help again. Good luck with your state of the art programming techniques!

Don't misunderstand me here. I'm very grateful for your help, absolutely. It's just that the code 'style' you've used is kinda different from the tutorials I've watched and the books I've been reading.

Anyways, I've kinda managed to do a program that can do pretty much the same thing, just that I avoided using memory allocation, instead I used file input/output to store the data. Not that neat but it works after all:)

Anyway, thanks mate:)
Jack Sparrow
 
Posts: 4
Joined: Mon Sep 07, 2009 12:53 pm


Return to Projects

Who is online

Users browsing this forum: No registered users and 0 guests