tux4life 2,072 Postaholic

Image Compression,Audio Compression .You couldn't even find this.....?

In a strange way, people here can't use Google, but they can find the forums :P (That's a relief!)

tux4life 2,072 Postaholic

when we do free the memory space reserved by malloc..does it free only the memory space not being used at that time or the whole memory space allocated by malloc???

http://www.cplusplus.com/reference/clibrary/cstdlib/free/

tux4life 2,072 Postaholic

dude, i can suggest a different approach for ur problem....
since 30 is divisible by 3 so start ur loop from 30 and subtract 3 each time and print until ur number becomes less than the input number...

this approach will print number between ur input and 30 but in decreasing order..

try it.

Why starting with 30 if you can easily start with 3 and each time add 3 ?
Then the output will be in a more logical order, right?

tux4life 2,072 Postaholic

Step 1: Get an integer from the user (store it in a variable)
Step 2: Check whether the integer is higher or lower than zero:

IF number < zero:
    Print out the multiples ([B]Step 3[/B])

Step 3 (Get all multiples of 3 for all numbers less or equal to 30):

  • Create a variable (here I'll call it tmp) and initialize it to the first multiple of three.
  • You create a while loop and let it run as long as tmp is less or equal to 30.
  • Each time you print the value of tmp.
  • At the end of the loop you each time add three to tmp.
tux4life 2,072 Postaholic

what api would you all recommend to learn? i dont really want to use visual c++ .net since i want it to be independent from the .net library. is there an api that is easily bundled with the exe such as when you install many windows programs it just works with out any additional libraries.

Microsoft Visual C++ 2008 (Professional and Express Edition) both allow you to compile your code as a native Windows executable, so the application compiled as a native Windows executable will be able to run without .NET Framework.

From Wikipedia:

Visual C++ 2008 Express can build both native and managed applications.

( Source: http://en.wikipedia.org/wiki/Microsoft_Visual_Studio_Express )

tux4life 2,072 Postaholic

And an addition to csurfer's post:
Get rid of that void main(), it's evil!
Change void main() to int main().

>How to convert structure to class in C++?
Theoretically in C++ a structure is a class.

Edit:: BTW, is your code working? As I cannot compile this crap on my compiler.

tux4life 2,072 Postaholic

> For example, i want to know how does wxWidgets create there windows, buttons, etc.
It's open source, go read the code.

Well, I guess if he reads the documentation or some of the tutorials listed here, it will be already more than enough for him :P

tux4life 2,072 Postaholic

I can confirm AD's code works correctly.
(Also tested it on a couple of Word files, playing a bit with the formatting)
The file is loading correctly after copying.

To the OP:
Ensure that you're copying a file which isn't corrupted, before copying you should check whether the file you want to copy loads correctly in MS Word, otherwise you've already missed the boat before the copying process starts.

tux4life 2,072 Postaholic

hello.

i want to get a free web space where i can upload my own created designs.

i went in with some of the sites but they provide their own templet.


so, please help me by giving me some names of such sites.

thanks !!

Ever tried finding a free hosting provider here ?
For PHP and MySQL, I always use this one :)

tux4life 2,072 Postaholic

thanks for the links :)

Is'nt that tutorial site learning API coding in C & not C++? I am not familiar with C.

I know, it says that it's for C, but it will work with C++ as well.

tux4life 2,072 Postaholic

There is an alternative al-though it might be faster if you clear the buffer.

#include<iostream>
#include<sstream>
#include<vector>
using namespace std;

int main()
{
 int i;
 vector <string> scoresheet;
 //stringstream q; //Donot Declare the stringstream here.
 string z;
 scoresheet.push_back("QWEr");
 scoresheet.push_back("Q");
 scoresheet.push_back("QEr");
 scoresheet.push_back("wQqE");
 for(i=0;i<scoresheet.size();i++)
 {
 stringstream q;//Declare stringstream inside loop.
  q<<scoresheet[i];               
  z=q.str();
  cout<<z<<endl;
 }
 cin.get();
}

What has been said in the second post of this thread?
BTW, he only wants the first word, not the whole string, so z=q.str(); is incorrect (but it seems working if you're dealing with strings which only contain one word).

tux4life 2,072 Postaholic

Is there any other way?
i mean to clear the buffer??

This is not for clearing the buffer, but for resetting the error flags: q.clear(); (You can read more about it here)
As you mentioned in your first post: The first value is printing over and over.
This is because the stringstream goes into an eof state.

tux4life 2,072 Postaholic

Wouldn't

stringstream q;
q << scoreSheet;
string x=q.str();

Do the trick?

Nope, because you just get back the whole string, not the first word.
And I think you meant: q << scoreSheet[B][i][/B]; .

tux4life 2,072 Postaholic

Look, I found another thread of you, about exactly the same:
http://www.daniweb.com/forums/thread201674.html

tux4life 2,072 Postaholic

Does your program directly crash after executing it?
If no, what input are you entering?

Also please answer the following:
What compiler are you using?
What OS are you on?

BTW, what are you going to do when the user enters a negative value at the start of your program?
(see: while (n--) )
You'll create a never-ending loop then.

tux4life 2,072 Postaholic

Declare the stringstream inside the for-loop's body:

for(i = 0; i < scoreSheet.size(); i++)
{
  [B]stringstream q;[/B]
  q << scoreSheet[i];
  q >> z;
  cout << z << endl;
}

:)

Edit::
In some cases, you might want to add an additional check as well:

for(i = 0; i < scoreSheet.size(); i++)
{
  stringstream q;
  [B]if( !scoreSheet[i].empty() ) {[/B]
    q << scoreSheet[i];
    q >> z;
    cout << z << endl;
  [B]}[/B]
}
tux4life 2,072 Postaholic

To the OP:
How do you think variable n will ever get value 10 ?
You never explicitly put a value in it.

n will never become ten, so you can rip this out of your code, and it will work in exactly the same way.

if(n==10)
    cout<<count;

>Its really hard even for us to know its LOGIC.
The LOGIC is: there's no LOGIC :P

tux4life 2,072 Postaholic

Look at the link in adatapost's post???

Do you know how to use a computer? (I hope so)
Do you know what 'internet' is? (I hope so)
If you can answer yes to both of these questions, then try to answer the following question:
Do you know what a link is? (On this forum links are marked in blue, and when you move your mouse pointer over it, it will be underlined)
Just click on it, and your browser (this is the program on your computer that you use to display webpages, with you this is probably called: 'Internet Explorer') will display a new web page.
Then you start reading this web page.

>I solved a c++ solution but i don't understand the idea of the program,
so i need help to understand the steps and the use of this program.

So, you're saying you solved it, but you don't understand your own solution?
Strange :icon_rolleyes:

tux4life 2,072 Postaholic

sorry i don't now how to use it please reply,

Look at the link in adatapost's post :)

tux4life 2,072 Postaholic

Well VC++,DevC++ and CODE::BLOCKS are great.And in case you don't like them you can always go back to good'ol Linux for g++.

Well, actually Dev-C++ and Code::Blocks are both IDEs featuring the MinGW compiler, a port of the GNU Compiler Collection.
(e.g: g++, gcc)

tux4life 2,072 Postaholic

Chances of getting an answer is even greater if you give your request a meaningful title.

He couldn't help it, first he hijacked a thread, then a moderator was informed to split this into a new thread, and the thread's title was decided by a moderator.

tux4life 2,072 Postaholic

We can say nothing for sure if we can't see your code, but I can maybe use my physic powers to make a guess.

Couldn't it be that you try to access __cpu_features_init in a place where your program can't see it, or you've made a typo, or you just didn't declare it?

tux4life 2,072 Postaholic

Look, I found another post of you, about exactly the same:
http://www.daniweb.com/forums/thread201413.html

tux4life 2,072 Postaholic

He said he was running it on Dev C++ in his post a few up.

I think you're confusing XTRobot with the OP (sgw)

Hence my suggestion to use MinGW or a non-dead compiler to keep up to date.

Actually Dev-C++ uses MinGW as it's compiler but it's an older version, so I'd also recommend him to upgrade his compiler.
Normally you can configure Dev-C++ to work with the new MinGW compiler, so he can continue to use Dev-C++, but with the new MinGW compiler, this shouldn't be a problem.

tux4life 2,072 Postaholic

When running this code, variable w contains an unknown value, this will nearly always cause an array override (unless you've much much luck).
You've to (explicitly) assign w a value as well, before using it anywhere in your program.

for ( i=3; i<[B]sqrt(100)[/B]; i++ ) {

           if ( [B]x[w][/B] != 0) { [B]// This will cause a segmentation fault[/B]

            for ( w=i; w<100; w++ ) {

                   if ( x[w] % i == 0 ) {

                          x[w] = 0;

                       }
                }
            }
        }

BTW, If I remember right, sqrt(100) equals 10, so why not just writing 10 instead?

Edit:: For anyone who thinks the code might not be compiling under gcc: I can confirm it compiles.

tux4life 2,072 Postaholic

thanks guys..
now another question is how will i test the hello world server/client program.
can i use only 1 pc someone said i should use 2? since its a networking program is that correct?

You could use what's called: 'a virtual pc'
In that case it's like you have two pc's (but in fact you only have one real pc, and on the real pc you run a kind of emulator software, this is software which emulates the hardware of a pc. In that emulated computer, you can install another operating system, connect it to a network...), but you'll have to make sure that your current pc has enough RAM to run the Virtual Machine on of course.

tux4life 2,072 Postaholic

What you can also try is just recompiling the source of the file which is behaving incorrectly.
(I compiled this source successfully using MinGW).

What compiler are you using ?

Could you just attach the whole file (the source of the file which doesn't work correctly) to this thread?
(and if possible: the executable file as well)
So I can check it by running it on my computer.

tux4life 2,072 Postaholic

Look: http://www.daniweb.com/forums/post906316.html
Edit:: This double-thread is deleted by an Admin.

tux4life 2,072 Postaholic

i need help please...i ask, to make a program that accepts a positive integers and displays all odd number between 1 to the integer entered (inclusive) accept the integer in main() and use a recursive function to identify and display the odd numbers.

tnx..salie

Don't resurrect old threads to just ask your question, you've already your own thread here:
http://www.daniweb.com/forums/post906282.html.

tux4life 2,072 Postaholic

I'll give you a small start:

(assume number is an integer variable)

A number is odd when the modulus is not equal to 0.
For example:

if( number % 2 )
{
   // The number is odd
}
else
{
  // The number is even
}

BTW, What are you going to do when the user enters a negative number?

if( number < 0 )
{
    // Negative number entered
    // Put your code to handle negative numbers here
}

A quick resume:

- Accept positive integers:

Declare an array, and use cin within a for-loop to fill it up with user entered numbers (also add a check to see whether the number is positive or not, before adding it to the array (see my example))

- Use a recursive function to identify and display the odd numbers:

From your main() function, you call the recursive function, inside the body of that function, you check whether the element is an even or an odd number (see my example), when the number is an odd number, you display it on the screen, you keep calling that function from itself until you've processed each element in the array.

tux4life 2,072 Postaholic

Hey AD, in your post (#3) you forgot an ending bracket on this line: while( fin.read( iobuffer, sizeof(iobuffer) ) :P

tux4life 2,072 Postaholic

>WARNING: NO NEW LINE AT THE END OF FILE
>What should I do?

Move your cursor to the end of the latest line of your file and press ENTER.
Then try recompiling your source, the warning should be away.

Nick Evan commented: Your wisdom is endless, all bow to the mighty tux ;) +20
Salem commented: Free food for the gimme question :) +36
tux4life 2,072 Postaholic

I once tried to do something like this with ONLY C++. Tried to take all the contents of a document and remake it with the same thing. The problem is though that there are some characters that may not show up (may not follow ascii char set) and their may be text that is not being retrieved. Make sure your getting all the text, so use a pointer:

#include <iostream>
#include <fstream>

using namespace std;

char* main(char* file)
{
    char* contents;
    char* buffer;
    int numOfChars = 0;
    char ch;
    ifstream fin(file);
    if(fin)
    {
        while(fin.get(ch))
        {
            buffer = new char[numOfChars+2];
            for(int i = 0; i < numOfChars; i++)
                buffer[i] = contents[i];
            buffer[numOfChars] = ch;
            buffer[numOfChars+1] = '\0';
            delete contents;
            contents = new char[++numOfChars+1];
            for(int i = 0; i < numOfchars; i++)
                contents[i] = buffer[i];
            contents[numOfChars] = '\0';
            delete buffer;
        }
        fin.close();
    }
    else
        return "Error";
    return contents;
}

Hey, why has no-one told him that it's [B]int main()[/B] and not char* main() or void main() or ... ??

tux4life 2,072 Postaholic

To generate an executable you should 'Build' it, compiling is not enough.
If you've compiled it, you still have to 'link' it to an executable file.
The 'Build' menu command will do these things both: it first compiles your source, and thereafter it links your compiled object file to an executable file.

tux4life 2,072 Postaholic

return (void*) y; What do you do here?
You create a pointer and put the value of y in it, this means that you let the pointer point to memory address 10 (not to the value 10 !!) in your example, you don't return a pointer to the value.
Further in your program you think that you've a pointer to that value, but in fact, the value is just stored as a memory address in your pointer, which explains why your program gives the expected output.
For example if you'd put value 0 in variable y, your program would have displayed 'error', which directly explains why your code is wrong.
This can be very dangerous, because you think the pointer points to the memory location which holds the value, but after dereferencing it, you can easily get a segmentation fault.
(And this is only about reading the data, can you imagine what would happen if you change the data like this: *x = 35 ? No? I can: you put the value 35 on memory address 10, this is certainly not what you want)

Note:: My previous post only applies if you'd have written the return statement like this: return &y; (and in that case your compiler should probably warn you as well)

tux4life 2,072 Postaholic

It's not recommended to just program it like that because you return a pointer to a local (automatic) variable, and when the function returns, the variable will be 'destroyed' (this means: it doesn't exist anymore, the memory assigned to it will be marked: "allowed to use"), but you still have a pointer to that freed memory, it will be never guaranteed that the freed memory will still contain the value of variable x (10 in this case).
If a local variable goes out-of-scope, then the memory it was occupying will be freed, meaning that the memory may be used to store other things in it, so the value 10 may be overwritten during the program's execution.

That's just the spirit of C: It's not because it seems working that it's actually working.

How to overcome this?
Use dynamic memory allocation.

tux4life 2,072 Postaholic

>It does work, but whats with this warnings ?
According to what I think, your program is bigger than that what you've posted, so could you please post the whole code so we can take a look at it?
The example in your post should compile fine without any warnings.

tux4life 2,072 Postaholic

A simple example demonstrating a string passed by reference:

void func(string &s)
{
    s += " something.";
}

This is just a useless function which adds " something." at the end of the string passed to the function, but it demonstrates the use of references.

A call to this function would look like:

string str = "This is";
func(str);

After running the function, str will contain: This is something. .

tux4life 2,072 Postaholic

If I read that correctly, if I use something like that, words[number] would be each value?

Everything between the delimiters is stored in a vector.
For example when you have this string: "first,second,third" and you set the comma as a delimiter, then you'll get a vector with these elements:

first
second
third
tux4life 2,072 Postaholic

If the OP had first used Google, then there even was no need to start this thread, because all his questions are already answered on Wikipedia:
http://en.wikipedia.org/wiki/Segmentation_fault

However, I want to congratulate the OP for wasting the time of everyone who replied in this thread.

tux4life 2,072 Postaholic

Hello All
Can anyone provide me a sample code, executing which generates a segmentation falut. need to understand how it works.

Thanks.

If you really want it:

for(int *p ;;) *p = 5;

Good luck with it :P

Edit::
But I really don't see how you'd understand it better, in programming, you just have to avoid this.
The only thing you really have to understand about this is that you never write some data to random memory.

tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

>Wait, what?? If you hover your mouse over the red box with text you see the code. That sure is weird!

That's because he has used TEX-tags to post his code, here's the code he was actually trying to post:

#include <stdio.h> 

int main ()
{
  float dollar, peso, dollar2, peso2, dtotal, ptotal;
  int choice;
  
  printf("Enter 1 if you want to convert dollars to peso");
  printf("\nEnter 2 if you want to convert peso to dollars\n");
  printf("Enter your choice: ");
  
  scanf("%d", &choice);
  getchar();
  
  if (choice == 1)
  {
    printf("Enter the amount of dollar(s): ");
    scanf("%.2f", &dollar);
    getchar();
    
    printf("%.2f dollar(s) is equivalent to %.2f peso(s)", dollar, dollar * ( 1 / 48 ));
  }

  getchar();
  
  if (choice != 1) 
  {
    printf("Enter the amount of peso(s): ");
    scanf("f", &peso2);
    
    dtotal = peso2 * 48;
    printf("%f peso(s) is equivalent to %f dollar(s)", peso2, dtotal);
  }
  getchar();
  return 0;
}

(I intended the code a bit as well :P)

tux4life 2,072 Postaholic

i ve actually read all the stuff out there, but really cant understand how to do that, if you send me an example i might get it

Examples are actually provided in the Demo's folder of the GNU MP distribution :)

tux4life 2,072 Postaholic

First create an integer type (of the GNU MP library), and then have a look at this:
http://web.mit.edu/gnu/doc/html/gmp_4.html#SEC11

tux4life 2,072 Postaholic

if you have to make a fact. of x

for(i=x;i>=1;i--)
{
fact=fact*i; // fact = 1
}
cout<<fact;

put this logic in your program..
it should be work..

Check your profile, how many times do you want to hear saying us: "Use code tags!!" ??

Information about code-tags is spread all over this website :
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled "Read Me: Read This Before Posting"
5) any place CODE tags were used
6) Even on the background of the box you actually typed your message in

:(

tux4life 2,072 Postaholic

Consider searching this site.

Eureka!
I've found one, Dave:
http://www.daniweb.com/code/snippet258.html

:)

Dave Sinkula commented: Oh! Was that there? What a coincidence. :icon_razz: +21
iamthwee commented: lol +21
tux4life 2,072 Postaholic

Well, actually there are strict (language) rules for splitting up a word in syllables, so I suggest you to look them up, and implement these rules in your program.
Once you've implemented them, you can start counting the syllables in a word, sentence, text.

tux4life 2,072 Postaholic

i have created a C string of my variables and guessing i need some king of loop to check each character after it gets it?

Could you please post the code you have?

tux4life 2,072 Postaholic

I'm wondering whether the OP is still interested in new posts in his thread.