Karkaroff 19 Light Poster

You could try the scanf function...

int h,m;
scanf("%d:%d",&h,&m);

You can enter the time as xx:xx

Karkaroff 19 Light Poster

Thanks Duoas!

And everyone who tried to help!

Karkaroff 19 Light Poster

I have changed that gets() function then itself(See post #6, reply to Salem). I just wanted to know if gets() gets into fstream's way ;)

I have added the binary mode too into open() .
I didn't get you(Duoas) the first time you told that.I thougt you were asking me to open a file before trying to read and write from it!!!


I was not able to use that statement Duoas asked me to.
compiler couldnot recognize std.
I tried using the statement using namespace std; and got error : declaration syntax error.
The IDE does not recognize using as a keyword either.
Though I fixed the problem by first taking the value of sizeof into an integer variable and then passing this variable inside seekg().(How it worked I dont know! Maybe it helped in type conversion).
Opening file in binary mode was particularly useful to read the objects from file
(or is it unserializing the objects from the file?)
The size of all objects were not the same in text mode.(Why? I don't know)

In short, the problem is solved. And I would be glad if someone would help understand how it got solved!!! :-/
Please try to answer the questions I've asked in this post when you explain what has really happened!!
@Duoas
I'm sorry I got a bit angry at you!

Karkaroff 19 Light Poster

One more thing the ios::fail flag is set after the write function call. But ios::bad is not set.

Karkaroff 19 Light Poster

@Duoas
"Stock.txt" exists and has 1 object in it.
I wrote it by hiding the read line mentioned in the question and my last post.
How many times should I say that?:@
That's the reason I'm asking this question in this thread. :S
Else I would have assumed that I have messed up with the syntax or logic somewhere!!!

@ Salem
The gets() function has nothing to do with reading and writing into a file I guess. Anyway, I have changed it to cin, if its a bad style to use C functions. [I'm curious to know if there is indeed something awful with that function!]

This is driving me nuts. I'm stuck because I cannot go ahead with my program without writing this into the file.

Karkaroff 19 Light Poster

Sorry i forgot to mention I am opening the file in the main() function stfile.open("stock.txt",ios::in|ios::out); And those tellp() functions I used just to see if the file is being actually written into the file.

What baffles me is that the object is being written into the file when the statement that reads an object from file (line 26 in the snippet given in question) is removed ( making it a comment).

Karkaroff 19 Light Poster

hey! are you sure you are not lying about your name???:icon_eek:
Its a good idea to use a screen name instead of your real name.
But I don't think its nice to tell people that the screen name is your real name!!!

Anyway, welcome abroad sis!!!:)

Karkaroff 19 Light Poster

In the following code, the object is not being written into file in line 30 stfile.write((char*)this,sizeof(item)); When line 26 : stfile.read((char*)&ob,sizeof(item)); is hidden the object is written into the file.

Please help!!!

fstream stfile;
class item
{
	int code;
	char name[25];
	int price;
	int qty;
public:
	int getcode()
	{
		return code;
	}

	void GetNew()
	{
//	  cout<<"\nENTER THE CODE";      //Unhide this when 
//	  cin>>code;                   //entering first iteminto the file   
	  cout<<"\n ENTER THE ITEM NAME";
	  gets(name);
	  cout<<"\n ENTER THE PRICE";
	  cin>>price;
	  cout<<"\n ENTER STOCK";
	  cin>>qty;
	  item ob;
	  stfile.seekg(-1*sizeof(item),ios::end);
	  stfile.read((char*)&ob,sizeof(item));
	  code=ob.getcode();code++;           //hide these three
	  stfile.seekp(0,ios::end);           //lines when entering
	  cout<<stfile.tellp()<<endl;        //first iten into the file
	  stfile.write((char*)this,sizeof(item));
	  cout<<stfile.tellp();
	  return ;
	}
	  void show()
	  {	cout<<"\ncode " <<code
		    <<"\nname "
		    <<name
		    <<"\n price"
		    <<price
		    <<"\n quantity"
		    <<qty;
	  }






};
void ShowItem(int c)
{
	stfile.seekg(0,ios::beg);
	stfile.clear();
	while(!stfile.eof())

	{item ob;
	stfile.read((char*)&ob,sizeof(item));
	if(stfile.eof())
	{perror("search"); break;}
	if(ob.getcode()==c)
	{ ob.show();break;	 }
	}
}
iamthwee commented: So many f.u.c.k ups -2
Ancient Dragon commented: Equalizer +21
Karkaroff 19 Light Poster

Thanks for taking pain to explain!!!

Karkaroff 19 Light Poster

flushing works.

could you please explain how and why it works?

Karkaroff 19 Light Poster

i posted this problem in C++ forum and people told me its impossible.
I'm using a very old version of C++ and most of its features are very similar to C.
So i thought maybe you people will understand what the problem is....


In the following code, getch() is executed before the file is displayed.

void ReadFile()
{	char fname[13],ch; 
	cout<<"Enter filename : ";
	cin>>fname;
	strcat(fname,".mth");
	temp.open(fname,ios::in|ios::nocreate);
	if(!temp)
	{	cout<<"File does not exist!!!";
		getch();		return;
	}
	temp.get(ch);
	while(!temp.eof())
	{	cout.put(ch);
		temp.get(ch);
	}	temp.close();
	getch();
}

you can find the thread i've posted in C++ forum at
http://www.daniweb.com/forums/thread103115.html

i've posted there the exe file i got after compiling the program containing this function and how to get this error un that program.

Karkaroff 19 Light Poster

The file is opened and read!
only the program doesnot wait after displaying the file, it goes on with displaying the menu.

Karkaroff 19 Light Poster

i've attached my exe file after zipping it!
if you are so much sure i'm lying, u can try executing it.:ooh:
First choose 1 in menu, type anything you like, press ctrl+G after that and save the file when prompted.
then choose 2, the menu is displayed along with the file, without waiting for getch().

Karkaroff 19 Light Poster

does it getch() after the file is displayed???

Karkaroff 19 Light Poster

in the following code, getch() is executed before the file is displayed.!
whats wrong? how can i correct this?

void ReadFile()
{	char fname[13],ch;

	cout<<"Enter filename : ";
	cin>>fname;
	strcat(fname,".mth");
	temp.open(fname,ios::in|ios::nocreate);
	if(!temp)
	{	cout<<"File does not exist!!!";
		getch();
		return;
	}
	temp.get(ch);
	while(!temp.eof())
	{	cout.put(ch);
		temp.get(ch);
	}
	temp.close();
	getch();
}
Karkaroff 19 Light Poster

i've made the final draft of the program its the same as the one posted in post #15
only i've changed the return type of main to int and added the statement
return 0;
at the end.

i feel that it has improved since i've started this thread.

Thanks, everyone who has helped!


And Happy New Year to all.

Karkaroff 19 Light Poster

ok!
now what about the comments?
is it enough?should i add more? delete some? modify some?

Karkaroff 19 Light Poster

That Programming tips were quiet useful!
But i couldnot understand certain terms in it.
Could someone explain to me what it means?
1. "telephone test"
2. off-by-one error

Karkaroff 19 Light Poster

its a joke right?

Karkaroff 19 Light Poster

oh! i too found the file too big to handle.
but we are not supposed to split the program code.
you see, its not in our syllabus!

Karkaroff 19 Light Poster

oh! sorry!
i didn't see your last post!
So you mean to say,
int main()
is the standard code???

Karkaroff 19 Light Poster

i'm using an old version of C++.
its just like C. Only difference - you can have classes in this.

And about the non-standart stuff .
i didn't find anything to that effect in Salem's post.(if you are reffering to post no. 14)

Karkaroff 19 Light Poster

sorry!
i didnot get you

Karkaroff 19 Light Poster

I've made the changes you people asked me to make ( I hope!:) ). If its not enough please tell.


I have also made some changes to the program.
In addition to commenting please tell me if there is something that i can improve upon in the program, without greatly changing the structure of the program.

Please find attached the program "file1.cpp".

Karkaroff 19 Light Poster

also please tell me which of the comments I should remove - atleast some of them, if there are many(so that I may know what sort of comments is to be retained and what is to be removed!).

Karkaroff 19 Light Poster

@ dubeyprateek
could you please explain what is to be written in "parameters" in your comment on functions.

Should I comment using the format
Method Name::
Parameters::
Return::
Purpose::
all the functions in my program.

Karkaroff 19 Light Poster

@Narue
could you please illustrate the difference between the "why" comment and the "what" comment!

Karkaroff 19 Light Poster

Please help me in self documenting the program below.
All you have to do is, take a quick glance at the following code to see if you can understand what each part is doing. If you have any difficulty in understanding(at a quick glance that is!), please tell me, so that I can add a comment.

Also tell me if I need to change the order in which I defined the functions and classes to enhance readability!


I've already posted this question once at
http://www.daniweb.com/forums/thread102405.html

I was asked to make a lot of changes so I closed the thread, made the changes and is reposting the question.

/*                Expression Evaluation Notebook
	Program to read text from keyboard and write it into a file, after
evaluating all simple mathematical expressions in the text.* /


#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<ctype.h>


/**********************Definitions of Data Structures Used*********************/


union element		//used to store parsed elements
	{ char sym;	//for symbol
	  int ival;	//for integer
	  float fval;	//for float
	};

struct info     //information part of stack/queue
	{	element el;     //element
		char flag;	//flag to show type of element
	};

class Queue
	{	struct node
		{       info inf;
			node *next;
		}*front,*rear;
	 public:
		Queue()
		{front=rear=NULL;}
		void push(info n);
		info pop();
		info peek()
		{return front->inf;}
	};

class Stack
	{	struct node
		{       info inf;
			node *next;
		}*top;
	 public:
	       Stack()
	       {top=NULL;}
	       void push(info);
	       info pop();
	       info peek()
	       {return top->inf;}
	};

/******************************End of definitions******************************/



//Defining Global objects and variables
fstream temp;
Queue …
Karkaroff 19 Light Poster

u can make it faster by incrementing counter by 2 when divisibilty by 2 fails.

u can further accelerate, by avoiding checking multiples of numbers already checked.(sieve of Eratosthenes)

Read sth about it at
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
also,
http://en.wikipedia.org/wiki/Primality_test
will help.


and...
personally, i feel checking till n/2 will be more efficient than sqrt(n). because sqrt(n) may take more steps.

u can also update the upper limit during each iteration
as
for (i=3;i<=n/i;i++)

Karkaroff 19 Light Poster

ok! i'll close this thread and repost the program after i've documented it properly!!!

Karkaroff 19 Light Poster

Please help me in self documenting the program below.
All u have to do is, take a quick glance at the following code to see if u can understand wat each part is doing. If u have any difficulty in understanding(at a quick glance that is!), pls tell me so that i can add a comment.

Also tell me if i need to change the order in which i defined the functions and classes to enhance readability!

/**********************Definitions of Data Structures Used*********************/


union element
	{ char sym;
	  int ival;
	  float fval;
	};

struct info
	{	element el;
		char flag;
	};

class Queue
	{	struct node
		{       info inf;
			node *next;
		}*front,*rear;
	 public:
		Queue()
		{front=rear=NULL;}
		void push(info n);
		info pop();
		info peek()
		{return front->inf;}
	};

class Stack
	{	struct node
		{       info inf;
			node *next;
		}*top;
	 public:
	       Stack()
	       {top=NULL;}
	       void push(info);
	       info pop();
	       info peek()
	       {return top->inf;}
	};

/******************************End of definitions******************************/



//Defining Global objects and variables
fstream temp;
Queue expr;
int exp_flag;


/***************************Function declarations******************************/
char symb( char);
void display(Queue);
int insert();
void post_convert();
int evaluate();
int expression();


/****************************End of declarations******************************/


/*******************************Main Function**********************************/
void main()
{	clrscr();
	temp.open("temp.mth",ios::out|ios::in);
	char ch;
	cin.get(ch);
	while(ch!=7)
	{	exp_flag=0;
		if(ch=='=')
		{	cin.get(ch);
			if(ch=='=')
			  exp_flag=expression();
			else
			  temp.put('=');
			if(exp_flag)
			  cout<<"Error : Expression syntax error!!";
		}
		else
		temp.put(ch);
		cin.get(ch);
	}

	getch();
}

/********************************End of Main***********************************/


/************************Other Function definitions****************************/
int expression()	//converts infix expression to postfix and evaluates
{       exp_flag=0;
	int fail=insert();
	cout<<"return frm insert()";

	if(fail)return -1;

	post_convert();
	cout<<"\nreturn from post_convert()";
	fail=evaluate();
	cout<<"\nreturn from evaluate()"<<fail;
	if(fail)
	{	char …
Karkaroff 19 Light Poster

Sorry! The prblem was not with the change in input.
I messed up with the way -ve numbers r handled and the way infix is converted to postfix.
in file.cpp
line 250:'>' should be replaced by '>='
line169:the statement MinusFlag=0; need to be added.

anyway! Thanx for trying to help me!!!

Especially Ancient Dragon and Walt P!!!

Karkaroff 19 Light Poster

weird, division works!!!
==34/2;
gives the output 17
but nothing else does!

Karkaroff 19 Light Poster

but when i use >> operator i cannot read wat comes after a blank space!
i want to read everythin in an expression, ignoring the white spaces!
like
== 12-8;
and ==12 -
8 ;
should evaluate to same result.

Karkaroff 19 Light Poster

sorry! i forgot to mention!
i've added some cout statements in between the "file.cpp" file to site the error!
i found that that in line 345 in function evaluate(), the if statement is being executed.
it might mean there's some error in

  1. inserting into queue,
  2. converting into postfix (inserting into stack)
  3. postfix evaluation
  4. or, more likely, handling exceptions in inserting![insert() function]

I couldnot pin point the error till now!

Also, in "file.cpp"
to stop enterin into file, u got to press ctrl+g.

Karkaroff 19 Light Poster

but getline reads whole line. i cannot ignore spaces with it.
with cin.get i can ignore spaces...

Karkaroff 19 Light Poster

PROGRAM CODE OF "expeval.cpp"

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<ctype.h>


/**********************Definitions of Data Structures Used*********************/


union element
	{ char sym;
	  int ival;
	  float fval;
	};

struct info
	{	element el;
		char flag;
	};

class Queue
	{	struct node
		{       info inf;
			node *next;
		}*front,*rear;
	 public:
		Queue()
		{front=rear=NULL;}
		void push(info n);
		info pop();
		info peek()
		{return front->inf;}
	}expr;

class Stack
	{	struct node
		{       info inf;
			node *next;
		}*top;
	 public:
	       Stack()
	       {top=NULL;}
	       void push(info);
	       info pop();
	       info peek()
	       {return top->inf;}
	};

/******************************End of definitions******************************/


/***************************Function declarations******************************/
char symb( char);
void display(Queue);
int insert();
void post_convert();
int evaluate();
void expression();


/****************************End of declarations******************************/


/*******************************Main Function**********************************/
void main()
{	clrscr();
	expression();
	getch();
}

/********************************End of Main***********************************/


/************************Other Function definitions****************************/
void expression()
{	int fail=insert();

	if(fail==0)
	post_convert();

	fail=evaluate();
	if(fail)cout<<"Error in eval ";
	getch();
}


int insert()
{

	int ctr=0,sign=1;
	float factor;
	char in[81];
	info inf={ 0,0};	//INPUT is by default integer


	cin.getline(in,81);
	for(int i=0;in[i]!='\0';i++)
	{	if(in[i]==' ')continue;
		if(symb(in[i]))
		{
			if(in[i]=='-'&&(symb(in[i-1])||i==0)&&in[i-1]!=')')
							//If operator is unary
			{	sign=-1;		//minus, change sign of
				inf.flag=0;		//factor, set flag as
				continue;		//integer, and continue
			}
			else if(symb(in[i])&&symb(in[i+1]))
			  if(in[i+1]!='('&&in[i+1]!='-'&&in[i+1]!='\0'&&in[i]!=')')
			  {	cout<<"Error!! too many operators!!!";
				return -1;
			  }
			else
			  sign=1;
			if(in[i]=='(')
			{	ctr++;
				if(inf.flag!=2&&i!=0)	//If there is no symbol
				{	inf.el.sym='*';	//before '(', then
					inf.flag=2;	//multiply by default
					expr.push(inf);
				}
			}
			else if(in[i]==')')
			  ctr--;

			inf.flag=2;		//INPUT is a symbol(operator)
			inf.el.sym=in[i];
			expr.push(inf);

		}
		else if(isdigit(in[i]))
		{       if(symb(in[i-1])||i==0)
			{	inf.flag=0;		//reset flag
				inf.el.fval=0.0;	//reset el
				inf.el.ival=(int)(in[i]-'0')*sign;
			}
			else if(inf.flag)
			{	factor/=10;	//decrement the place value
						//of last digit
				inf.el.fval+=(in[i]-'0')*factor*sign;
			}
			else
			  inf.el.ival=10*inf.el.ival+(int)(in[i]-'0')*sign;

			if(symb(in[i+1])||in[i+1]=='\0') …
Karkaroff 19 Light Poster

i made a program to evaluate an expression, "expeval.cpp" [find attached].
i then tried to go further.....
i made the program to read characters from the keyboard and write it into a file["file.cpp",find attached]. if an expression is encountered, it is evaluated and the result is displayed after the line and also written into the file.
When the program encounters '==' the expression evaluation is initiated and when ';' is encountered, expression is terminated.
This, was working till i changed the input style. From gets to cin.get .

Then everything went wrong!
pls help me find wat went wrong!!!
:icon_confused:

Please..........i've got to submit the project within one week!!!:S

Karkaroff 19 Light Poster

Newton-raphson iterative formula for f(x)=0; is
x2=x1-f(x1)/f'(x1);
u have to find the root of (x^3)-a=0;
so the formula is x2=x1-((x1^3)-a)/(3x1^2);
orx2=(2x1+a/x1^2)/3;
In other words u got ur formula wrong.

Now let us do the 100 prob with this formula-
assume x=1

after Pass 1 x= 34
Pass2 x=22.695501
Pass3 x=15.195048
Pass4 x=10.274402
Pass5 x=7.165367
Pass6 x=5.426147
Pass7 x=4.749559
Pass8 x=4.644025
Pass9 x=4.64159
Pass10 x=4.641589

Hope that clears ur prob.

Karkaroff 19 Light Poster

Hi, I'm George from India. I'm doing my last year in school. I love programming. Here for some real programming experience. To learn from all u Big B's here.