课程和使用* [英] classes and using *
问题描述
/ *
为什么不是char * city;输入字符串时工作,
但char city [256];是吗?
我必须将城市名称及其临时数存储在数组中,
然后输出所有信息。我如何在这个
计划中使用数组? * /
#include< iostream.h>
#include< iomanip.h>
#include< conio.h>
class Cities {
private:
char city [256];
int temp;
public:
void prclass();
Cities();
pause();
};
void Cities :: prclass()
{
cout<<'' \\\
'' <<城市<<" "<< temp;
}
城市::城市()
{
int t = 0;
char answer;
do {
cout<<" \ n\ n" ;;
cout<<"请输入城市名称#"<< t + 1<<":" ;; cin>> city;
cout<<" \ nnase输入"<< city<<"'的temp:" ;; cin>> temp;
t ++;
cout<<" \ n另一个城市?(Y / N)" ;;
cin>> answer;
} while(回答==''y''||回答==''Y'');
cout<<" \ n \ n" ;;
}
void pause(){
cout<<" \ n\ n" ;;
cout< <"按> ENTER<继续..." ;;
cout<<" \ n\ n" ;;
cin.get();
} //关闭暂停
int main()
{
clrscr();
Cities var;
var.prclass();
pause();
返回0;
}
------------------------------------ --------------
删除* batSPAM *给我发电子邮件
----------- ---------------------------------------
/*
why doesnt char* city; work when entering a string,
but char city[256]; does?
I have to store city names and their temps in an array,
and then output all info. how do i utilize arrays in this
program? */
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class Cities {
private:
char city[256];
int temp;
public:
void prclass();
Cities();
pause();
};
void Cities:: prclass()
{
cout<<''\n''<<city<<" "<<temp;
}
Cities:: Cities()
{
int t=0;
char answer;
do{
cout<<"\n\n";
cout<<"please enter name of city#"<<t+1<<": ";cin>>city;
cout<<"\nplease enter "<<city<<"''s temp: ";cin>>temp;
t++;
cout<<"\nAnother city?(Y/N)";
cin>>answer;
}while(answer ==''y'' || answer ==''Y'');
cout<<"\n\n";
}
void pause() {
cout<<"\n\n";
cout<<"Press >ENTER< to continue...";
cout<<"\n\n";
cin.get();
}//close of pause
int main()
{
clrscr();
Cities var;
var.prclass();
pause();
return 0;
}
--------------------------------------------------
remove *batSPAM* to e-mail me
--------------------------------------------------
推荐答案
Developwebsites写道:
Developwebsites wrote:
/ *
为什么不是char * city;输入字符串时工作,
但char city [256];是吗?
我必须将城市名称及其临时数存储在一个数组中,然后输出所有信息。我如何在这个程序中使用数组? * /
#include< iostream.h>
#include< iostream>
#include< iomanip.h>
#include< iomanip>
#include< conio.h>
非标准包含文件。你真的需要吗?
班级城市{
私人:
char city [256];
int temp;
public:
void prclass();
Cities();
pause();
};
当使用字符串(即C风格的字符串)时,
需要一个所有字符的位置。声明:
char * city;
声明指针,但不为字符分配任何空间
。请参阅下面的常见问题解答。
int main()
{
clrscr();
clrscr()不是标准函数。你真的需要在执行一个简单的
程序之前清除屏幕吗?
Cities var;
var.prclass();
pause();
返回0;
}
/*
why doesnt char* city; work when entering a string,
but char city[256]; does?
I have to store city names and their temps in an array,
and then output all info. how do i utilize arrays in this
program? */
#include<iostream.h> #include <iostream>
#include<iomanip.h> #include <iomanip>
#include<conio.h> Non-standard include file. Do you _really_ need it?
class Cities {
private:
char city[256];
int temp;
public:
void prclass();
Cities();
pause();
};
When using character strings (i.e. C-style strings) you
need a place for all the characters. The declaration:
char * city;
declares a pointer but does not allocate any space
for characters. See the FAQ below.
int main()
{
clrscr(); The clrscr() is not a standard function. Do you really
need to clear the screen before you execute a simple
program?
Cities var;
var.prclass();
pause();
return 0;
}
我强烈建议您切换到使用
std :: string类而不是C风格的字符串。
FAQ解释了原因。
-
Thomas Matthews
C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt
C ++常见问题: http://www.parashift.com/c++-faq-lite
C常见问题: http://www.eskimo .com /~scs / c-faq / top.html
alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
其他网站:
http://www.josuttis.com - C ++ STL Library book
I highly recommend that you switch to using the
std::string class rather than the C-style strings.
The FAQ explains why.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
" Developwebsites" <德************* @ aol.combatSPAM>在留言新闻中写道:20 *************************** @ mb-m13.aol.com ...
"Developwebsites" <de*************@aol.combatSPAM> wrote in message news:20***************************@mb-m13.aol.com...
/ *
为什么没有char * city;输入字符串时工作,
但char city [256];呢?
因为char *不是字符串类型。它是char的poitner。
你将未初始化的指针传递给cin来填写。如果你想要
这样做,你必须分配存储空间
ie,city = new char [256];
但是,如果你想要一个字符串,你应该使用字符串类
而不是求助使用char *犯错误。
#include< iostream.h>
#include< iomanip.h>
你最好在这里使用标准包括:
#include< iostream>
#include< ; iomanip>
#include< string>
使用命名空间std;
class Cities {
private:
char city [256];
string city;
char answer;
string answer;
} while(answer ==''y''|| answer ==''Y'');
/*
why doesnt char* city; work when entering a string,
but char city[256]; does?
Because char* is NOT a string type. It is a poitner to char.
You pass an uninitialized pointer to cin to fill in. If you wanted
to do that, you must allocate storage
i.e., city = new char[256];
However, if you want a string, you should use the string class
rather than resorting to making mistakes using char*.
#include<iostream.h>
#include<iomanip.h>
You''re better advised to use the standard includes here:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Cities {
private:
char city[256]; string city;
char answer;
string answer;
}while(answer ==''y'' || answer ==''Y'');
如果您的用户输入了是,则只读取一个字符将会留下es。在缓冲区中
将被后续输入读取。
If you user had typed "yes", reading only one char would leave "es" in the buffer
that would get read by the subsequent inputs.
" Developwebsites" <德************* @ aol.combatSPAM>在消息中写道
新闻:20 *************************** @ mb-m13.aol.com。 ..
"Developwebsites" <de*************@aol.combatSPAM> wrote in message
news:20***************************@mb-m13.aol.com...
/ *
为什么不是char * city;输入字符串时工作,
但char city [256];是吗?
/*
why doesnt char* city; work when entering a string,
but char city[256]; does?
定义int时,将分配多少内存?当您定义
指向char(char *)的指针时,将分配多少内存?当你
定义一个256个字符的数组(char city [256])时,将分配多少内存
?
When you define an int, how much memory will be allocated? When you define
a pointer to a char (char*), how much memory will be allocated? When you
define an array of 256 characters (char city[256]), how much memory will be
allocated?
>
这篇关于课程和使用*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!