硬件在C ++中输入空值? [英] hw to enter a null value in c++?

查看:297
本文介绍了硬件在C ++中输入空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hai der ...

我正在编写一个c ++程序以接受一个讨厌的源程序作为输入(简而言之,我想接受一组字符串行)并显示它.

这是代码...

hai der...

i am writing a c++ program to accept an assebly source program as input(simply sayong i want to accept a set of string line) and display it.

here is the code...

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<alloc.h>
class source
{
	char label[10];
	char mnic[15];
	char op[10];
public:
	char* getdata()
	{
	cin>>label>>mnic>>op;
	return strdup(mnic);
	}
	char* display()
	{
	cout<<"\n"<<label<<"\t"<<mnic<<"\t"<<op;
	return strdup(mnic);
	}
};
class symtab
{
};
int main()
{
clrscr();
source obj1[15];
char *sptr;
int i=0;
do
{
sptr=obj1[i].getdata();
i++;
}while((strcmp(sptr,"end"))!=0);
i=0;
do
{
sptr=obj1[i].display();
i++;
}while((strcmp(sptr,"end"))!=0);
getch();
free(sptr);
return 0;
}




我们为每个对象输入三个字符串(即label,mnic,op).但是问题是如果我不想为label输入一个字符串,代码将无济于事.c提示会等到我们输入所有三个字符串.它可能不会两个输入一个值.这就是我可以输入一个空值的方法吗?


例如:

如果我输入

名称始于2000
g lda b
f结束g

它显示的输出与上面给出的相同.

我想输入

名称始于2000
lda b
f结束g




We enter three strings for each object (namely label,mnic,op).But the problem is that the code cant help if i did not want to enter a string for label.the c prompt waits until we enter all the three strings.how its possible not two enter a value.that is how i can enter a null value?


eg:

if i enter

name start 2000
g lda b
f end g

it shows output as the same given above.

i want to enter

name start 2000
lda b
f end g

how i can put the first enter as null?

推荐答案

这是完全读取每一行,然后将该行解析为其组成元素的问题.与其将三个字段作为单独的输入数据读取,不如阅读完整的文本行.然后将行拆分为单独的字段,并确定每个字段代表哪个项目.因此,在上面的示例中,如果仅输入两个字段,则说明存在操作码和操作数,但没有标签.您完整的程序将需要了解所有可能的组合,并针对每种组合进行迎合.
This is a matter of reading each line completely and then parsing that line into its constituent elements. Instead of reading the three fields as separate pieces of input data you should read a complete line of text. Then split the line into separate fields and decide which item each field represents. So in your example above if you only get two fields input then you know there is opcode and operand but no label. Your complete program will need to be aware of all possible combinations and cater for each one.


这篇关于硬件在C ++中输入空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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