tux4life 2,072 Postaholic

Will that work on all platforms

I know that it works on Windows, Linux and Unix :P

for example
WIndows.

C:> dir >>output.txt

Would put all the output of the Dir command In Output.txt

Yes, but remember that >> is different from > : >> appends to the file (if it exists, otherwise it's just creating the file and writing the output of the dir command to it)

For further info on this: http://commandwindows.com/command1.htm

tux4life 2,072 Postaholic

Customer *CUST = new Customer; will cause a memory leak also :)

tux4life 2,072 Postaholic

int count = 1; , you should declare it outside your while-loop :)

tux4life 2,072 Postaholic

I would store the words in a plain text file and read them into a vector :)

tux4life 2,072 Postaholic

Or is there some other way around, like freeing up the memory in va()?

In the code you post this will lead to undefined behavior, as you're returning a pointer which points to deallocated memory, that's very dangerous :)

tux4life 2,072 Postaholic

Hello.
I'm currently having a memory leak problem in my program (C++ with MFC, VS2005) and I don't know WHY it happens, as the code seems to be OK to me. Anyway, here's the code that causes memory leaks:

char* va( char* FormatStr, ... )
{
	va_list ArgPtr;
	char *FinalString;
	int length;

	va_start( ArgPtr, FormatStr );

	length = _vscprintf( FormatStr, ArgPtr )+1;
	FinalString = new char[length];
	vsprintf_s( FinalString, length, FormatStr, ArgPtr );

	va_end( ArgPtr );
	return FinalString;
}

void CMFC1Dlg::OnBnClickedButton1()
{
	if( m_POpened )
	{
		CString cmd;
		m_EditCmd.GetWindowTextA( cmd );
		m_Port.WriteToPort( va( "%s%c", cmd, 13 ) );
	}
}

I have searched the forums already, but found no solution to this... These are the leaks (assuming cmd = "AT" ):

The thread 'Win32 Thread' (0x9a4) has exited with code 100 (0x64).
The thread 'Win32 Thread' (0x6b0) has exited with code 0 (0x0).
Detected memory leaks!
Dumping objects ->
e:\dawg\prg\vs2005\c++\mfc1\mfc1\mfc1\mfc1.cpp(102) : {140} normal block at 0x003B9ED0, 4 bytes long.
 Data: <AT  > 41 54 0D 00 
e:\dawg\prg\vs2005\c++\mfc1\mfc1\mfc1\mfc1.cpp(102) : {112} normal block at 0x003B9BB8, 4 bytes long.
 Data: <AT  > 41 54 0D 00 
e:\dawg\prg\vs2005\c++\mfc1\mfc1\mfc1\mfc1.cpp(102) : {110} normal block at 0x003B3828, 4 bytes long.
 Data: <AT  > 41 54 0D 00 
Object dump complete.
The program '[2636] MFC1.exe: Native' has exited with code 0 (0x0).

Debugger points to FinalString = new char[length]; as the source of the leak. Could you please help me out?

Kind regards
Pat

Yes, every time the 'va' function executes it allocates new memory, and returns a pointer …

tux4life 2,072 Postaholic

I'd like to explain it to you but I don't know how the grading system works (when do you get A, B, ... ?)

BTW, The total marks displayed isn't correct I think, for example if I enter the following marks: 5 4 3 2 1 it's displaying 2.3 , is that how the grading system works, or wasn't it meant like that (I mean: Did you expect 15 as a total?)

tux4life 2,072 Postaholic

Actuall you don't have to change a thing to make it get input from a data file instead of the keyboard -- just direct stdin from data file, like this: c:>myprogram.exe <test.cpp <Return> When the above is typed from a command prompt window your program will get input from the file test.cpp instead of the keyboard.

But it won't strip the comments out of the file ...

tux4life 2,072 Postaholic

What does CTRL+C do?

CTRL+C just breaks/stops the execution of a console program :)

By the way: I had also expected your program was stripping comments from C/C++ source files, but you can easily adapt it to remove comments from files :)

Sky Diploma commented: Thanks :) +3
tux4life 2,072 Postaholic

Saving a few extra keystrokes isn't worth sacrificing code readability. If the OP finds it more understandable, it's his choice. Generally I only give advice on formatting if it's actually unreadable to other programmers, or is just very messy.

Sure that it's his choice, I didn't say that he must use this way, it was only an alternative way to do it :)

tux4life 2,072 Postaholic

Sorry, I've found a typo:

A little clarification: when you write if (delim[b]=fileWord[b]) then you're assigning the value at fileWord[b] to delim[b] , as a result the assignment operator always returns the assigned value what means that if the assigned value is NULL or 0 (it's the same :P) the if statement will treat the expression as false , in any other case the expression will be evaluated as true :)

tux4life 2,072 Postaholic

Want to know whether you're code is working or not? Just try compiling it :)

Sorry, typo: I meant your* code of course :)

tux4life 2,072 Postaholic

plz make USER class as m getting alot of difficulties in it...i will user ur code as a reference code and will write my own code after undertanding it

No, you write it, Daniweb isn't a homework completion service :P
Your question was a bit vague, don't ask questions like: 'could you write it for me?' the answer will always be: NO!
Just ask specific questions about what you don't understand and we'll be glad to help you out :)

tux4life 2,072 Postaholic

Try this library :)

tux4life 2,072 Postaholic

Did you try:

#include <cv.h>
#include <highgui.h>

?

And oh, by the way, have you already read this?

tux4life 2,072 Postaholic

That's a star? I doesn't look like any star I've ever seen.

It isn't a star but a 'star pattern' (or asterisk pattern)

tux4life 2,072 Postaholic

That's the most ridiculous I've ever heard: using a float to define a boolean LOL :P

  1. It's harder to accomplish (but it isn't impossible :))
  2. A float uses much more memory than a char (and you don't need all this memory)
tux4life 2,072 Postaholic

In your example you have to use strcpy() strcpy(filename, argv[1]);

Sorry, I forgot :'(
Edit:: Corrected it in my post :)

tux4life 2,072 Postaholic

Try something like this:

char filename[20];
if(argc>1)
{
    strcpy(filename, argv[1]);
}

Edit:: We have to check whether the number of arguments is greater than 1 because the program's first argument ( argv[0] ) is always containing the program's filename ...

tux4life 2,072 Postaholic

Declare a new variable in your code called d_count (delimiter count) for example ( int d_count=0; )
(Remember to do this outside your for-loop).
Change this piece of code (the changed part is marked in green):

if ((inchar == '|') || (inchar=='\n')) {
printf("%d: chars counted\n", [B]++d_count[/B]);
break;
}

What you were doing is just displaying the loop's index ( i ) which is wrong of course, because this index is incremented always when a character is read :)

tux4life 2,072 Postaholic

Could you please post using code tags?
And by the way: your code is still using the C file handling this isn't wrong but in C++ the C++ way for File I/O is preferred :)

i need help in modifing code to count the number of records the '\n' will be one record. i need program to determine how many records in file so i can loop until eof to get all records.

So you mean that you've one record per line in the file ?

iamthwee commented: *nods* +19
tux4life 2,072 Postaholic

How exactly would you display the read data?

(I assume the data is already read into the library structure)
To display the whole library you could make use of a for-statement like this:

for(int i=0; i<NUM_OF_BOOKS; i++)
{
     cout << "Author: " << library[i].author << endl;
     cout << "Title: " << library[i].title << endl;
     cout << "Publisher: " << library[i].publisher << endl;
     cout << "Price: " << library[i].price << endl << endl;
}

Hope this helps !

tux4life 2,072 Postaholic

it dint work same error

You're doing something wrong, with me this compiled perfectly :)

tux4life 2,072 Postaholic

No wonder you get an error: char str[]=""; , you actually have to create an array with more than 1 element, you can't store much else in it than just the null-terminator: \0 :P

Try char str[80]; instead of char str[]=""; Some little clarification: when you write char str[]=""; this actually does the same as char str[1]; , but only one element is not enough space to hold a whole string, it's just enough space for holding the null-terminator (which is obligatory for each c-string: if a c-string doesn't contain a null-terminator and you're performing operations on it your program will crash in the best case, or your whole operating system in the worst case, but today's operating systems normally offer memory protection so it's only your application which will crash) :( ...

tux4life 2,072 Postaholic

>thank you for helping.i've tried to do it, hope that i did right.
Want to know whether you're code is working or not? Just try compiling it :)

tux4life 2,072 Postaholic

Sorry that was my mistake, try the following: strcpy(str, fileOneVector[n].c_str());

tux4life 2,072 Postaholic

Post edited:: Try: strcpy(str, fileOneVector[n].c_str());

tux4life 2,072 Postaholic

I'm not repeating it anymore :angry:: Post using code tags !!

>how ever when i use the below code it works but not all comars get eliminated
That's just logical because you only check whether the character is a punctuation mark, and only in that case you remove the character from the string :)

tux4life 2,072 Postaholic

Could you please post using code tags? punct=str[B]o[/B]tok(str," .,:'!@#"); , it isn't 'strotok', but strtok :P

tux4life 2,072 Postaholic

could be be kindful to show me the sample code using strtok

There's an example provided here, just scroll the page a bit down and you'll see it :P

tux4life 2,072 Postaholic

You can't declare functions within other functions/statements:

switch (menu) {
   case 0:{
      
		   void displayHigh ( int max)
		   	{
				double max = 0.0;

			for ( int numbers[i] = 1; i < 100; i++ ) 
				if ( numbers[i] > max )
				
					cout << "The Maximum number is: " << max;
			break;
			}
	}
   case 1:{
		   void displayOdd ( int odd)
			{
				for (int i in createArray)
				{
					if (numbers[i]%1 == 0, i <= 100, i++)
					do {
						cout << "The odd numbers are: " << odd[i];// to list all odd numbers
						} while(numbers[i] < 0, numbers[i] <=100);
				}
		   }
			break;
		  }
		  
   case 2: {
      void count30to70
	  {	
		  for (int i in createArray)
		  {
			 if (numbers[i]=30, i <=70)
			 count = // dont know how to code it
      	  }
	  } 
	  break;
		   }
   case 3: {
	   void displayAverage (int numbers[0],int numbers[1], int numbers[2], int numbers[3],int numbers[4])
	   {
		   average = (numbers[0] + numbers[1] + numbers[2] + numbers[3] + numbers[4])/5;
		   cout << "The average is: " << average;
	   }      
      break;
		   } 
   default: {
      
	   cout << "Wrong input";
			}

}//end of void displayMenu

You don't have to wrap curly braces around the statements in a case, though it's allowed you could always forget typing a curly brace, but it's your choice :)

Also take a look at the following:

void createArray (int numbers[0], int numbers[1], int numbers[2], int numbers[3],int numbers[4]) 
{
		for(int i = 0; i < 4; i++){
		do {
		cout << "Enter 5 numbers from 1-100 : ";
		cin >> numbers[i];
		} while(numbers[i] …
tux4life 2,072 Postaholic

yes so i can say that if the char is equal to ispuntc then remove the character

Yes you can, but there's an easier way:

  • Declare a string (called 'result' for example) to store the 'stripped' string in.
  • Just loop through the string (containing the punctuation marks) and every time you evaluate whether the character is a punctuation mark or not, if the character is not a punctuation mark, you add it to your 'result' string, after the loop has finished you've a string which contains an 'exact' copy of the other one, but without the punctuation marks :)

To repeat what I've already said: the strtok function might be a lot easier for you, but it's your choice :)

tux4life 2,072 Postaholic

is it possible to remove the punction marks with "ispunct"

No, the ispunct function is just checking whether a character is a punctuation mark or not :)
(you can find more information on 'ispunct' here)

By the way: Why don't you just use the strtok function?

tux4life 2,072 Postaholic

I'm quoting what has been already said (cause you didn't read the thread carefully enough :():

In your main function you're declaring two pointers like this: int **matr1,**matr2; , there's nothing wrong with this line but you don't assign memory to them and then you're just passing matr1 to several functions like sumMat , subtraction , etc. without even have assigned memory to these pointers, that will definitely end in a crash :)

So what do you have to do first? Well, you've to assign some memory to both of them before you can let your functions perform operations on your matrices :)

tux4life 2,072 Postaholic
if (delim[b]=fileWord[b])

Firstly Assignment "=" is not equal to Equal to "==".

A little clarification: when you write if (delim[b]=fileWord[b]) then you're assigning the value at fileWord[b] to delim[b] , as a result the assignment operator always returns the assigned value what means that if the assigned value isn't NULL or 0 (it's the same :P) the if statement will treat the expression as false , in any other case the expression will be evaluated as true :)

Sky Diploma commented: Thanks for explaining it out +3
tux4life 2,072 Postaholic

Wouldnt int **matr1 do the trick as well?

Well, actually it's the same, I meant that post as an advice only, it's just a matter of preference :) ...

int **matr1;
matr1=new int* [sizeofrows];
[B]while[/B]
[B]matr1[1]=new int[sizeofcol];[/B]

In case you forgot (I do NOT say you forgot, but in case you did): an array's first element is the 0th :)
By the way: what is the 'while' doing there without any condition?

tux4life 2,072 Postaholic

What does mart1 point to?

It points to an unknown location in memory.

To the OP:
In your main function you're declaring two pointers like this: int **matr1,**matr2; , there's nothing wrong with this line but you don't assign memory to them and then you're just passing matr1 to several functions like sumMat , subtraction , etc. without even have assigned memory to these pointers, that will definitely end in a crash :)

tux4life 2,072 Postaholic

If you want to pass an array of pointers to a function you should use: int *matr1[] (instead of int **matr1 ) for example :)

tux4life 2,072 Postaholic

Just some advices (after a first quick look at your code, not a solution to your problem):

  • In your main-function I encounter this line: while(flag==true) it's just the long way to write: while(flag)
  • I also encounter the nasty system("pause"); line, there's actually no need to use this, you could just use cin.get(); instead (why not to use system("pause"); ?)...
siddhant3s commented: You should perhaps add that system() remark on your signature. +6
tux4life 2,072 Postaholic

Why are you just ignoring all the questions we ask you?
ArkM asked: What's series #1, #2 ... ?

tux4life 2,072 Postaholic

I saw that my previous post was just a bit of an overkill :P
(I assume that your Big Integers are stored in a character array): use cin>>[I][U]yourchararray[/U][/I]; >then I guess i have to create an array with those same digits to an int. lol how?
I don't understand this, do you mean that you have to store each digit of the character string into an integer array (where each element is a digit)? ==> Result is memory hog :P

Edit:: I see that NathanOliver had already recommended it to you, so I'm very sorry for this double recommendation :'(

tux4life 2,072 Postaholic

I've forgotten that in c++ the not operator is implemented with "~"

Just a little bit extra info: In C++ you've actually two NOT-operators (this does also apply to the AND and OR operators): you've the bitwise NOT-operator ~ and you also have the logical NOT-operator ! :) ...

You use the logical NOT-operator if you expect an expression to be true or false , the bitwise NOT is just wrapping all the bits of it's operand (1 becomes 0 and 0 becomes 1)
The NOT operators in C++ are both unary operators, which means that they both work with one operand.

ddanbe commented: nice clarification in this discussion! +4
tux4life 2,072 Postaholic

the i have the numbers but i want the iterations....

:?:

tux4life 2,072 Postaholic

Hi siddhantes, there's nothing wrong with your way of thinking, but doesn't your code have to be:

#include<iostream>
#include<iomanip>
int main()
{
double d=3.1457545;
std::cin>>d;
//do your calculation with d
//with great acuraccy !!
const std::streamsize orig=std::cout.precision();//save the orignal precision
std::cout<<std::setprecision(3)<<d<<std::endl;//set precision to 3 significant digits
std::cout<<std::setprecision([B]orig[/B]);//regain orig[U][B]i[/B][/U]nal precision
}
tux4life 2,072 Postaholic

That's the worst main function I've ever seen: void main() (reason)

Please post using code tags, and by the way: I don't see any question, I'm not a magician, I cannot look into your mind to see what your problem is, so please just tell it :P

tux4life 2,072 Postaholic

Divide the problem into parts:

  1. Declare an array (of type char )where you want to put all those digits in
  2. Get the integer from the user (get it via cin and store it in an integer variable)
  3. Use the itoa function to convert the integer to a c-string (= character array) (Take a look at this for some more information about itoa)

Hope this helps !

tux4life 2,072 Postaholic

Why don't you just use the principle of reading and writing structures from/to a file?

To write to the file:
Let's say we want to write the following structure to a file:

struct book {
    char author[30];
    char title[20];
    char publisher[20];
    int price;
};
const int NUM_OF_BOOKS = 30;
ofstream outfile;
book library[NUM_OF_BOOKS];
outfile.open("[I][U]yourfilename[/U][/I]", ios::binary);
if(!outfile.is_open())
{
    cerr << "ERROR: Couldn't write file." << endl;
    exit(1);
}
// ... we assume that there are already some values in 'library'
outfile.write(reinterpret_cast<char* >(library),NUM_OF_BOOKS*sizeof(book));
outfile.close();

To read from the file:
Reading the structures back from the file is analog:

const int NUM_OF_BOOKS = 30;
ifstream infile;
book library[NUM_OF_BOOKS];
infile.open("[I][U]yourfilename[/U][/I]", ios::binary);
if(!infile.is_open())
{
    cerr << "ERROR: Couldn't read file." << endl;
    exit(1);
}
infile.read(reinterpret_cast<char* >(library),NUM_OF_BOOKS*sizeof(book));
infile.close();

Hope this helps ! :)

tux4life 2,072 Postaholic

ok i have this... but i have overloaded... error with pow...

ceil(pi1 * pow( 10,digits ) - 0.5) /pow(10,digits);

If you're having an error, can you please mention it also, so we can take a look at it?

tux4life 2,072 Postaholic

With the math.h (or cmath someone correct me on this) header you can use the floor and ceil functions if I remember correctly to round down and round up respectively.

In C++ cmath is the preferred way, use math.h only if your compiler doesn't support the new header file style (e.g.: cmath ), but the use of math.h is not wrong :)

tux4life 2,072 Postaholic
double pi1=0;

for (int long n=1; n<=10000; n++) 
{//star for loop
	 pi1 += (4*pow(-1.0, n+1))/(2*n - 1); 
}//end loop

before loop for i want the pi1 round=number by the user...
for example user give 3 the pi1 =3.140

I actually wanted to see an example with concrete numbers, and as already mentioned: the floor and ceil functions from the C++ math library perform rounding on numbers :(
(and oh, if you post a snippet of code, could you please post it using code tags then ?)