C++动态数组输出乱码的问题

问题描述

C++动态数组输出乱码的问题

为何输出成员brand是乱码“屯”?
#include
#include
using namespace std;
int main()
{
struct CandyBar
{
char brand[20];
float weight;
int calorie;
};

CandyBar *test=new CandyBar[3];
test[0].brand =="The First";
test[0].weight=5;
test[0].calorie=136;
test[1].brand =="The Second";
test[1].weight=10;
test[1].calorie=350;
test[2].brand=="The Third";
test[2].weight=15;
test[2].calorie=333;
cout<<"The Brand: "<<test[0].brand;
cout<<", "<<test[1].brand;
cout<<", "<<test[2].brand<<endl;
cout<<endl;
cout<<"The Weight: "<<test[0].weight;
cout<<", "<<test[1].weight;
cout<<", "<<test[2].weight<<endl;
cout<<endl;
cout<<"The Calorie: "<<test[0].calorie;
cout<<", "<<test[1].calorie;
cout<<", "<<test[2].calorie;
cin.get();
cin.get();
return 0;
}

解决方案

struct CandyBar
{
string brand;
float weight;
int calorie;
};

CandyBar *test=new CandyBar[3];
test[0].brand ="The First";
test[0].weight=5;
test[0].calorie=136;
test[1].brand ="The Second";
test[1].weight=10;
test[1].calorie=350;
test[2].brand="The Third";
test[2].weight=15;
test[2].calorie=333;

解决方案二:

解决方案三:

#include
#include
using namespace std;
int main()
{
struct CandyBar
{
char *brand;
float weight;
int calorie;
};

CandyBar *test=new CandyBar[3];
test[0].brand =="The First";
test[0].weight=5;
test[0].calorie=136;
test[1].brand =="The Second";
test[1].weight=10;
test[1].calorie=350;
test[2].brand=="The Third";
test[2].weight=15;
test[2].calorie=333;
cout<<"The Brand: "<<test[0].brand;
cout<<", "<<test[1].brand;
cout<<", "<<test[2].brand<<endl;
cout<<endl;
cout<<"The Weight: "<<test[0].weight;
cout<<", "<<test[1].weight;
cout<<", "<<test[2].weight<<endl;
cout<<endl;
cout<<"The Calorie: "<<test[0].calorie;
cout<<", "<<test[1].calorie;
cout<<", "<<test[2].calorie;
cin.get();
cin.get();
return 0;

}

解决方案四:

P(x|c)=frac{P(c|x)cdot P(x)}{P(x)}


解决方案五:

用memcpy给char数组赋值

解决方案六:

不懂耶,不过有时候字符串后面不加/0就可能出现乱码。

时间: 2024-09-03 19:22:06

C++动态数组输出乱码的问题的相关文章

c语言-动态数组赋值,实现矩阵m,n的乘积,为什么p矩阵的输出总是0元素呢

问题描述 动态数组赋值,实现矩阵m,n的乘积,为什么p矩阵的输出总是0元素呢 #include"stdio.h" #include"stdlib.h" //动态分配的函数需要包含该头文件 //void Matrix(double ,double **,double **,int ,int ,int ); int main() { int i,j,k,mWidth,mHeight,nWidth,nHeight; double **m,n,**p; //定义指向指针的指

请教一输出动态数组方法

问题描述 现有一个dataset中的datatable如下表所示:User_idUser_nameUser_age1张三202王五263李四204张三205李四216李四207张三208王五209张三20现要将这个datatable传入一方法,然后根据user_name列排序后输出一数组,如输入上表则应输出testArray[2]testArray[0]={1,4,7,9}//张三的User_idtestArray[1]={2,8}//王五的User_idtestArray[2]={3,5,6}

NSArry 为静态数组,不能往里面添加元素 NSMutableArray 为动态数组,是NSArray 的子类

NSArry 为静态数组,不能往里面添加元素 NSMutableArray 为动态数组,是NSArray 的子类 定义一个数组 NSarray *city = [[NSArray arrayWithObjects:@"beijing",@"shanghai",@"heibei",nil]; 必须以nil结束.   方法: - (undesigned) count  //元素个数 - (id) objectAtIndex: (indesigned)

c++-一道C++读取文件,动态数组和自定义指令的题目,有什么最优解答?

问题描述 一道C++读取文件,动态数组和自定义指令的题目,有什么最优解答? 读取123.txt,里面含有以下几种指令: ADD_F ADD_B REMOVE_F REMOVE_B RESULT 开始要求创建一个空数组 例如 ADD_F 2 3 5:在数组开头增加2 3 5 ADD_B 3 5 6:在数组结尾增加3 5 6 REMOVE_F 3:删掉数组头3个数字 REMOVE_B 2:删掉数组后2个数字 RESULT:输出当前数组 例子 ADD_F 1 2 3 ADD_B 5 6 7 RESUL

malloc-动态一维数组输出问题

问题描述 动态一维数组输出问题 新手一枚,求前辈指教! /* malloc动态内存分配练习 从键盘输入成绩并打印在屏幕上,人数未知 */ #include<stdio.h> #include<stdlib.h> int main() { int num=0; printf("请输入学生人数:n"); scanf("%d",&num); int *mark; mark = (int *)malloc(num * sizeof(int))

《C语言及程序设计》实践项目——动态数组

返回:贺老师课程教学链接 [项目1-学生人数没个准]输入学生成绩,输出高于平均成绩的学生序号和成绩.其中学生人数不定,可能10个,可能1000.在录入成绩之前,学生人数由键盘输入.由于要先求出平均成绩,然后才能确定输出哪些学生的信息,所以需要一个数组先将学生信息保存下来.考虑学生人数不定,用动态数组是个更适合的方案下面的程序结构供参考: #include<stdio.h> #include_________ //m?????.h int main() { int number; //学生人数

C++动态数组类的封装实例_C 语言

C++中的动态数组(Dynamic Array)是指动态分配的.可以根据需求动态增长占用内存的数组.为了实现一个动态数组类的封装,我们需要考虑几个问题:new/delete的使用.内存分配策略.类的四大函数(构造函数.拷贝构造函数.拷贝赋值运算符.析构函数).运算符的重载.涉及到的知识点很多,对此本文只做简单的介绍. 一.内存分配策略 当用new为一个动态数组申请一块内存时,数组中的元素是连续存储的,例如 vector和string.当向一个动态数组添加元素时,如果没有空间容纳新元素,不可能简单

动态数组的实现案例

Java动态数组是一种可以任意伸缩数组长度的对象,在Java中比较常用的是List.下面介绍一下List作为Java动态数组的用法. 我们可以首先编写两个类List.java  和一个测试类Test1.java.将主类和测试类分开写,更有利于扩展性,这是一个非常好的编程思想.下面来看一下我们如何来实现List类.注释已经写得很清楚了,如果有不懂的地方欢迎留言. //定义一个容器类 public class List{ final int INIT_LENGTH=10; int[] array=n

JavaScript中的索引数组、关联数组和静态数组、动态数组讲解_javascript技巧

数组分类: 1.从数组的下标分为索引数组.关联数组 复制代码 代码如下: /* 索引数组,即通常情况下所说的数组 */ var ary1 = [1,3,5,8]; //按索引去取数组元素,从0开始(当然某些语言实现从1开始) //索引实际上就是序数,一个整型数字 alert(ary1[0]); alert(ary1[1]); alert(ary1[2]); alert(ary1[3]);   /* 关联数组,指以非序数类型为下标来存取的数组  python中称为字典 */ var ary2 =