为什么我会得到垃圾输出

为什么我会得到垃圾输出

本文介绍了为什么我会得到垃圾输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Dev C ++ 4

用户可以输入城市和临时名称,

但是当它输出城市名称时我会得到垃圾但是

temps很好。

struct Cities {

char city [];

int temp;

} ;

void input_names(Cities info [],int numb_cities){


for(int t = 0; t< numb_cities; t ++){

cout<<" \ n\ n" ;;

cout<<"请输入城市名称#"<< t + 1<< ;":" ;; cin>> info [t] .city;

cout<<" \ nplease输入城市的临时温度"

< ;< info [t] .city<<":" ;;

cin>> info [t] .temp;

}


cout<<" \ n\\\
" ;;

for(int t = 0; t< numb_cities; t ++){

cout<< info [t] .city<<" "<< info [t] .temp<<" \ n" ;;

}


} //关闭input_names;


void main(){

int n,

numb_cities = 10; //城市数量


void input_names(Cities [],int);


城市信息[numb_cities];

input_names(info,numb_cities);

}


------------------- -------------------------------

删除* batSPAM *给我发电子邮件

-------------------------------------------- ------

using Dev C++4
user can enter names of cities and temps,
but when it output city names i get garbage but
temps are fine.
struct Cities {
char city[];
int temp;
};
void input_names(Cities info[], int numb_cities) {

for(int t=0;t<numb_cities;t++) {
cout<<"\n\n";
cout<<"please enter name of city#"<<t+1<<": ";cin>>info[t].city;
cout<<"\nplease enter temp of city "
<<info[t].city<<": ";
cin>>info[t].temp;
}

cout<<"\n\n";
for(int t=0;t<numb_cities;t++) {
cout<<info[t].city<<" "<<info[t].temp<<"\n";
}

}//close input_names;

void main() {
int n,
numb_cities=10; //number of cities

void input_names(Cities[], int);

Cities info[numb_cities];
input_names(info, numb_cities);
}

--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------

推荐答案



问题是cin>> char []不分配存储空间,你需要

才能做到这一点。 std :: string完成分配存储的所有魔力和

清理它。


The problem is that cin >> char [] does not allocate storage, you need
to do that. std::string does all the magic of allocating storage and
cleaning it up.





这也不合法。 C ++中的数组声明需要大小不变

表达式(我打赌你使用的是一个简单的G ++)。


我建议你得到一个体面的C ++文本。我认为Koenig& Moo''s Accellerated C ++

对你非常有帮助。



This is also not legal. Array declarations in C++ need sizes that are constant
expressions (I bet you''re using an anceint G++).

I''d recommend you get a decent C++ text. I think Koenig & Moo''s Accellerated C++
will be very helpful to you.




必须返回int。



main must return int.




为什么会这样?

返回到哪里?

程序编译得很好。


------------ --------------------------------------

删除* batSPAM *给我发电子邮件

------------------------------------- -------------



why is that?
return to where?
the program compiles fine as it is.

--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------


这篇关于为什么我会得到垃圾输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 23:43