为什么我会得到垃圾输出? [英] why do i get garbage output?

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

问题描述

使用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
--------------------------------------------------

推荐答案

Developwebsites写道:
Developwebsites wrote:
使用Dev C ++ 4
用户可以输入城市和临时名称,
但是当它输出城市名称时,我会得到垃圾,但是温度很好。

struct Cities {
char city [];


使用

字符串城市;


而不是

int temp;
};
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[];
use
string city;

instead
int temp;
};



问题是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.




Developwebsites <德************* @ aol.combatSPAM>在消息新闻中写道:20 *************************** @ mb-m06.aol.com ...

"Developwebsites" <de*************@aol.combatSPAM> wrote in message news:20***************************@mb-m06.aol.com...
struct Cities {
char city [];
int temp;
};


这甚至不应该编译。阵列城市需要一定的规模。坦率地说,

我怀疑你是C ++的新手。不要把char阵列打死。使用字符串

class。


cin>> info [t] .temp;


你不测试这些输出是否已经结束。

void main(){


main must return int。

void input_names(Cities [],int);


你不需要在这里重复声明。

城市信息[numb_cities];
struct Cities {
char city[];
int temp;
};
This shouldn''t even compile. The array city needs a definite size. Frankly,
I suspect you''re new to C++. Don''t beat char arrays to death. Use the string
class.

cin>>info[t].temp;
You don''t test if these finputs ail.

void main() {
main must return int.
void input_names(Cities[], int);
You don''t need to repeat the declaration here.
Cities info[numb_cities];




这也不合法。 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.


>>
void main(){
void main() {



必须返回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
--------------------------------------------------


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆