Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
}



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?
Posted
Comments
E.F. Nijboer 21-Dec-10 10:58am    
You can just check if nothing was entered and process that as null?

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900