LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-28-2003, 01:17 AM   #1
OlRoy
Member
 
Registered: Dec 2002
Posts: 304

Rep: Reputation: 86
Whats wrong with this?


I'm doing some examples in my C++ book and can't figure out how to create a array of a structure using new when there are different variable types in the structure. I'm getting a "Expression syntax" message (among others )in Borland C++ 6 Pro

#include <clx.h>
#include <iostream>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused

struct CandyBar
{
char brand[20];
double weight;
short int calories;
};
int main(int argc, char* argv[])
{


CandyBar * ptr = new CandyBar [3];
ptr[0] = {"Mocah Munch", 2.3, 350};
ptr[1] = {"Snickers", 3.1, 431};
ptr[2] = {Hershey", 1.9, 257};
std::cout << "Candy Bar Statistics\n\n";
//Mocah Munch
std::cout << "Brand: " << ptr[0].brand << "\n";
std::cout << "Weight: " << ptr[0].weight << "\n";
std::cout << "Calories: " << ptr[0].calories << "\n\n";
//Snickers
std::cout << "Brand: " << ptr[1].brand << "\n";
std::cout << "Weight: " << ptr[1].weight << "\n";
std::cout << "Calories: " << ptr[1].calories << "\n\n";
//Hershey
std::cout << "Brand: " << ptr[2].brand << "\n";
std::cout << "Weight: " << ptr[2].weight << "\n";
std::cout << "Calories: " << ptr[2].calories << "\n\n";
std::cin.get();
std::cin.get();
return 0;
}
 
Old 05-28-2003, 03:53 AM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
here's one way:

#include<iostream>

using namespace std;

struct CandyBar
{
char* brand;
double weight;
short int calories;
};

int main(int argc, char* argv[])
{

CandyBar* ptr= new CandyBar [3];


ptr[0].brand="Mocah Munch";
ptr[0].weight=2.3;
ptr[0].calories=350;

ptr[1].brand="Snickers";
ptr[1].weight=3.1;
ptr[1].calories=431;

ptr[2].brand="Hershey";
ptr[2].weight=1.9;
ptr[2].calories=257;

cout << "Candy Bar Statistics\n\n";

for(int i=0; i<3; i++)
{
cout << "Brand: " << ptr[i].brand << "\n";
cout << "Weight: " << ptr[i].weight << "\n";
cout << "Calories: " << ptr[i].calories << "\n\n";
}


return 0;
}
 
Old 05-28-2003, 01:21 PM   #3
OlRoy
Member
 
Registered: Dec 2002
Posts: 304

Original Poster
Rep: Reputation: 86
That works, thank you
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
whats wrong with my fsck? Dillius Mandriva 5 02-19-2005 04:18 PM
Whats wrong with this script??? Sammy2ooo Linux - Networking 2 08-24-2004 06:20 AM
whats wrong with my sound? HNjerry Linux - Software 0 03-02-2004 11:12 AM
Whats wrong with my XF86Config here: Scruff Linux - General 4 09-17-2003 01:06 AM
whats wrong w/ my slack? Flawless Slackware 8 03-01-2003 03:21 PM


All times are GMT -5. The time now is 04:12 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration