Код: Выделить всё
struct CandyBar
{
string name;
float weight;
int calories;
};
CandyBar *snack =new CandyBar [3];
Если массив объявлен статически, всё нормально.
Модератор: Модераторы разделов
Код: Выделить всё
struct CandyBar
{
string name;
float weight;
int calories;
};
CandyBar *snack =new CandyBar [3];
Код: Выделить всё
struct CandyBar {
char *name;
float weight;
int calories;
};
void init_candy_bar (struct CandyBar *t, char *name_, float weight_, int calories_) {
t->name = name_;
t->weight = weight_;
t->calories = calories_;
}
void print_candy_bar (struct CandyBar *t) {
printf("%s\n", t->name);
printf("%f\n", t->weight);
printf("%d\n", t->calories);
}
int main () {
struct CandyBar snack[3];
init_candy_bar(&snack[0], "marmalad", 5.0, 10);
init_candy_bar(&snack[1], "chokolate", 7.0, 100);
init_candy_bar(&snack[2], "ice-cream", 10.0, 1000);
print_candy_bar(&snack[0]);
print_candy_bar(&snack[1]);
print_candy_bar(&snack[2]);
return 0;
}
Код: Выделить всё
#include <iostream>
#include <string>
struct CandyBar {
std::string name;
float weight;
int calories;
};
void init_candy_bar (struct CandyBar *t, char *name_, float weight_, int calories_) {
t->name = name_;
t->weight = weight_;
t->calories = calories_;
}
void print_candy_bar (struct CandyBar *t) {
std::cout << t->name << std::endl;
std::cout << t->weight << std::endl;
std::cout << t->calories << std::endl;
}
int main () {
struct CandyBar *snack;
try {
snack = new struct CandyBar[3];
} catch (std::bad_alloc xa) {
std::cout << "memory error." << std::endl;
return 1;
}
init_candy_bar(&snack[0], "marmalad", 5.0, 10);
init_candy_bar(&snack[1], "chokolate", 7.0, 100);
init_candy_bar(&snack[2], "ice-cream", 10.0, 1000);
print_candy_bar(&snack[0]);
print_candy_bar(&snack[1]);
print_candy_bar(&snack[2]);
delete [] snack;
return 0;
}
Код: Выделить всё
#include <iostream>
#include <string>
struct CandyBar
{
std::string name;
float weight;
int calories;
};
int main()
{
CandyBar *snack = new CandyBar[3];
snack[0].name="Beer";
snack[1].name="Tee";
snack[2].name="Aqua";
std::cout<<snack[0].name<<std::endl;
std::cout<<snack[1].name<<std::endl;
std::cout<<snack[2].name<<std::endl;
delete []snack;
return 0;
}
Код: Выделить всё
#include <iostream>
#include <string>
struct CandyBar
{
std::string name;
float weight;
int calories;
CandyBar()
{
std::cout<<"Input name\n";
std::cin>>name;
}
};
int main()
{
CandyBar *snack = new CandyBar[3];
std::cout<<snack[0].name<<std::endl;
std::cout<<snack[1].name<<std::endl;
std::cout<<snack[2].name<<std::endl;
delete []snack;
return 0;
}
Код: Выделить всё
#include <iostream>
#include <string>
struct CandyBar {
std::string name;
float weight;
int calories;
};
CandyBar *snack = new CandyBar[3];
void main () {
snack[1].name = "1";
snack[2].name = "2";
snack[3].name = "3";
std::cout << snack[1].name << snack[2].name << snack[3].name << std::endl;
delete []snack;
}
Код: Выделить всё
#include <iostream>
#include <cstring>
using namespace std;
struct CandyBar
{
string name;
float weight;
int calories;
};
void init_struct(CandyBar *n, string name_, float weight_, int calories_)
{
n->name = name_;
n->weight = weight_;
n->calories = calories_;
}
void print_struct(CandyBar *n)
{
cout << "Name: " << n->name << endl;
cout << "Weight: " << n->weight << endl;
cout << "Calories: " << n->calories << endl << endl;
}
Crazy писал(а): ↑03.10.2009 14:31Код: Выделить всё
#include <iostream> #include <string> struct CandyBar { std::string name; float weight; int calories; }; int main() { CandyBar *snack = new CandyBar[3]; snack[0].name="Beer";//[b][color="#00FF00"][size="6"]Подскажите а можно ли в строку name поместить имя с пробелом(допустим The biggest Beer)?[[/size][/color]/b] snack[1].name="Tee"; snack[2].name="Aqua"; std::cout<<snack[0].name<<std::endl; std::cout<<snack[1].name<<std::endl; std::cout<<snack[2].name<<std::endl; delete []snack; return 0; }