Look at [URL="http://www.cplusplus.com/reference/iostream/stringstream/"]<stringstream>[/URL] in the stl. You know how you can pass any standard numeric type to cout and it knows how to print it? Well, stringstream knows how to convert any standard numeric type to a string. Furthermore, stringstreams allow iomanip formatting, so you can pad numbers and other such manipulations.

I hate to rain on your parade, but as long as you are using classes from the standard template library (string), you might as well use methods from the library as well:

include <string>
include <algorithm>

using namespace std;
int main(int argc, char *argv[]){
string alphabet = "abcdefghijklmnopqrstuvwxyz";
random_shuffle( alphabet.begin(), alphabet.end() );
cout << "alphabet after shuffling: " << alphabet << endl;
return 0;

As you can see, the shuffling can be done with a single line by invoking the random_shuffle algorithm from the <algorithm> library.

Ancient Dragon commented: You are right. +26

[QUOTE=sidra 100;1122965]plz guide me i m having some error at line 28[/QUOTE]

  1. [I]Explain your problem more clearly[/I]. Saying "plz guide me" is not very helpful for us, and it's ridiculously presumptuous that we will respond with thorough help when you can't type more than one sentence.
  2. [I] Post the error that you are getting[/I]. We aren't psychics that just [B]know[/B] what your compiler is saying.
  3. [I]Think about what you are trying to do.[/I] On line 28, you have a serious confusion that is pretty inexcusable. In particular, s is a char array member of this class. There is no ".t" member of a character array. You probably mean "t.s". If you actually thought about what you were typing, and had a better naming convention, you would probably have caught this as you wrote it.
  4. [I]Clean up your code before you post it[/I]. The indentations and extra blank lines are infuriating. I truly hope your code doesn't look like this in your IDE
  5. [I]Write better code.[/I] Your class is named [B]str[/B], but two of your functions (including the one on line 28) return a [B][I]string[/I][/B]. There is a [B]string[/B] type in the standard template library, and I doubt you want your char array wrapper class to return a 3rd type of string representation. If you are studying computer science in school, you have a very long way to go. You should learn to write careful code first. Even ridiculously simple classes like these should be built ...
Agni commented: I agree !! +3

For this task, you should really use [B]Regular Expressions[/B]. There aren't any regular expression parsers built into standard c++, so you will need to install a library. [URL="


I am a newbie to C++ (and programming in general)

Since you are new, and have a fresh, open mind, I would recommend that you start using the Standard Template Library now. This library is powerful, helpful, and relatively easy to learn, and it works in all [B]c++[/B] compilers. If you are using a [B]C[/B] compiler, you're out of luck.

Anyway, the [URL="

[COLOR="Red"]void operator= (monomial & p);[/COLOR]
monomial operator* ( monomial &p);
monomial operator/ (monomial &p);
monomial a1("a:b:c");
monomial a2("a:c");
monomial a8;

The problem, i believe, is that you are trying to pass a non const monomial reference to the assignment operator. This is fine if you are passing a reference to a variable like
monomial a1("a:b:c");
monomial a2("a:c");
monomial a8;
a8 = a1;
However, the value you get back from the operator / hasn't been assigned to a variable yet. You can't change the value of this monomial, because it doesn't have a permanent address yet. These values are always const. Consequently, you need to change you assignment operator to take a const monomial for an argument. In general, you should use const protection wherever it makes sense

[COLOR="Red"]monomial&[/COLOR] operator= ( [COLOR="Red"]const[/COLOR] monomial & p){
var = p.var;
return this;
The assignment operator should always return a reference to the monomial being assigned, so that assignments can be chained like [icode]a=b=c=d;[/icode] This is done by having the code return
this. The argument should always be const, because while assigning p to this, you should never change the values in p

monomial operator/ ( [COLOR="Red"]const[/COLOR] monomial &p) [COLOR="Red"]const[/COLOR];
This function creates a new monomial, sets its value, and then returns the new monomial. The values in p should never be changed, so we pass the argument as a const monomial reference. This will also allow the operator to operate on ...

[QUOTE=SpyrosMet;1120077]I need to make a program that takes one character as an input and instantly continues execution and the time limit for the user to enter that character is 2 or 3 seconds. please help me and if possible ppost an example. Thanks for your help.[/QUOTE]
Please read [URL="

[QUOTE=mmasny;1120083]Would you be so kind and help me solve this problem?
It doesn't compile. Compiler says that the line 23 causes problems and I don't know why. It says class OknoWlasciwe has no member called ojciec and how come?? I mean, OknoWlasciwe is a public subclass of NieDesktop, which has such a member, so I thought it had it too...[/QUOTE]

Tthe ojciec member of NieDesktop is [B]private[/B]. This means that only an instance of NieDesktop can access it. If you want this variable to be visible to derived classes, you must declare it to be [B]protected[/B]. If you want any member of a class to be visible to only itself, use [b]private[/b]. If you want the member to be visible to itself and it's descendants, use [b]protected[/b]. If you want it to be visible to anyone, use [b]public[/b].

Good luck!

You have some obvious problems that need correcting:

Problem 1:
template <class T>
T min(vector<T> a)
Don't use the name "min". There is already min and max functions in the standard template library, and you shouldn't override these. Use something like vectorMin(), or vMin()

Problem 2:
T min = a[0];
This is no good. The function is named min, so this variable's name needs to change. Perhaps use minimum?

Problem 3:
vector<T>::iterator it;

for( it = a.begin(); it!=a.end(); ++it)

For the vector container class, iterators are not necessary for looping over the contents. They are important for the list containers (and others) because the members of a std::list are not stored in adjacent memory. For vectors, however, the data is stored just like an array so this:
for( int i=0; i<(int)a.size(); i++ ){
doSomething( a[i] );
is just as fast and much easier to code / read. Using an iterator is not wrong, but it is uneccesary extra work.

Problem 4:
min =
The index to your maximum value is getting changed every time the if statement executs. So, your maximum will shift, and weird behavior will appear. You should only keep track of the index of the maximum value if you really need it.

I recommend you re-write this in a simpler form such as:

template <class T>
T vMin( const vector<T>& v ){
T minimum = v[0];
for( int i=1; i<(int)v.size(); i++ ...

[QUOTE=philipbas;1118641]i don know about class n pointers,can u explain it with essy examples please[/QUOTE]

You should learn about google (perhaps you could google it? AAAHHH recursion! ). There are so many simple c++ tutorials and examples [b]THAT ALREADY EXISTS AND ARE 1 GOOGLE SEARCH AWAY[/b]. Please try to solve your own problem before you ask for help.

[QUOTE=firstPerson;1118929]clock() returns the time in mili seconds, [/quote]
Not so. This returns the number of clock ticks since the last call to clock(). What this number means varies by hardware and os.
CLOCKS_PER_SEC is the conversion
factor from the value returned by clock() into seconds.
That is so. so:
double secs = clock() / (double)CLOCKS_PER_SEC;
will give you the time in seconds since the last call to clock.

i figured it out too.
im just confused by the innermost while since its my first time seeing a while without contents.[/QUOTE]
This is basically what you call busy waiting. The loop will essentially do nothing but check the loop condition. Once the loop condition is false, the program can move on. In general, busy waiting is not a desirable way to wait for a condition to resolve. The problem is that, for long intervals, this loop will waste a lot of processor cycles to no purpose. If you are concerned about correctness, you would need to calculate how much time remains until the condition resolves and then sleep for that interval. Sleep tells the OS that the processor can be used for other computations until a certain time interval has passed. Unfortunately, sleep() and usleep() are not c++ standards, so you would have to choose a platform dependant method of non-busy waiting. For simple experimental cases, however, busy waiting will be fine. Just be sure that you don't give it too long of a waiting interval, because you ...

[QUOTE=rusydi_10;1116487]Write a program for multiplication and division of two numbers for different base number.

Programme structure: The program should contain

  1. Should have an infinite loop which is the program is always running.
  2. An option for a user to select base number of the first number. (For example: base 2, base 8, base 10, base 16)
  3. Then, again list the options of base number for the second number.
  4. Then, do the multiplication and division of those two numbers.
  5. Ask the user to choose the base number of the output.
  6. Reminder: You have to check either the input from user is in the range of the base number. (For example: if the user enters number 4 in base 2. So, it is invalid since it is not in the base of 2)[/QUOTE]

This is obviously homework. If you don't even try the problems, how are you going to learn anything? When you cheat on homeowrk in classes, you're only cheating yourself out of knowledge.

Salem commented: Indeed, makes you wonder why some people ever bother to sign up for courses. +19

[QUOTE=dardar4;1114439]yes, i know i should use cvFitline();
but as i stated i'm not sure what this function does.
what does it returns? and how do i work with it?[/QUOTE]

The link he provided describes everything you need. Also, this is a c++ forum, not an OpenCV forum. OpenCV has a very active yahoo group if you need help. However, most of any questions you have about OpenCV functionality can be answered by their extensive online documentation: [url]http://opencv.willowgarage.com/wiki/[/url]. I use OpenCV extensively, and I visit that site at least once a day.

Here is a working example with a few notes at the bottom:


include <iostream>

using namespace std;

enum ops{ ADD, SUB, MLT };

template <class T>
class Matrix{
T* mat;
int rows;
int cols;
int sz;

void initMat( int rows, int cols ){
    if( rows <= 0 || cols <= 0 ){
        mat = NULL;
    this->rows = rows;
    this->cols = cols;
    sz = rows * cols;
    mat = new T[sz];

mat = NULL;

Matrix( int rows, int cols ){
    initMat( rows, cols );

Matrix( int rows, int cols, T initVal ){
    initMat( rows, cols );
    for( int i=0; i<sz; i++ )
        mat[i] = initVal;

Matrix( const Matrix& other ){
    initMat( other.rows, other.cols );
    memcpy( mat, other.mat, sz * sizeof(T) );

    delete [] mat;

T& operator()( int row, int col ) const{
    return mat[ cols * row + col ];

Matrix operator+( const Matrix& other ) const{
    return arithmeticOperator( other, ADD );

Matrix operator-( const Matrix& other ) const{
    return arithmeticOperator( other, SUB );

Matrix operator*( const Matrix& other ) const{
    return arithmeticOperator( other, MLT );

Matrix arithmeticOperator( const Matrix& other, ops op ) const{
    if( mat == NULL || other.mat == NULL || other.rows != rows || other.cols != cols )
        return Matrix();
    Matrix neo( rows, cols );
    switch( op ){
        case ADD: for( int i=0; i<sz; i++ ) neo.mat[i] =  mat[i] + other.mat[i]; break;
        case SUB: for( int i=0; i<sz; i++ ) ...
vidit_X commented: Thanks. +2

y=f/2; [COLOR="Red"]//This will tell you nothing about the evenness of f[/COLOR]
if ( y= 0) [COLOR="Red"]//This will always resolve to false because the assignment operator essentially returns the rvalue, which is always 0 in this case. Perhaps you meant y==0 ?[/COLOR]
cout<<" odd";

This is a good way to avoid the costly mod operator (if this was your intention) while determining if a number is even:
[code=c++] bool isOdd = result & 1;[/code]

This works because all even values have a 0 in the least significant bit, while odds have a 1 in the LSB. The bitwise operator will essentially test the LSB, and return 1 if the LSB is 1, and 0 otherwise. You can see this for yourself with this code

include <iostream>

using namespace std;
int main(int argc, char *argv[]){
for( int i=0; i<14; i++ )
cout << i << " is " << ( i & 1 ? "odd\n" : "even\n" );
return 0;

This could be easily applied to this problem by using this line
[code=c++]cout << "your result is " << ( result & 1 ? "odd\n" : "even\n" );[/code]

I know the solution to the random reordering has already been handled, but here is a solution that I find more mathematically satisfying.

  1. Assign each die a random x, y coordinate
    a. The coordinates should be real numbers (i.e. doubles )
    b. If you prefer integers, then the range of possible values for each dimension should be significantly larger than the corresponding grid dimension and a multiple of the grid dimension.
    For a 6x16 grid, use x=rand()%(66), y=rand()%(1616)
  2. Sort the dice by their y coordinate in place
  3. Partition the list into h equally sized groups where h = height of dice grid
    For a 6x16 grid, obviously partition into 16 groups of 6 coordinate pairs
  4. Sort the dice in each partition in place
  5. The list should represent a random distribution representing random re-ordering over a fixed size grid
    So, for the die in group 4 at slot 2, it should go to Grid location row 4, column2

It may not be more efficient (haven't done the analysis), and it might be more troublesome to implement, but it is an interesting solution, no?

You can also deduce the indices of a 2D array from a 1D iterator using modulus math:

/ Performs both integer division and modulo with improved efficiency /
void divmod( int dividend, int divisor, int &quotient, int &remainder ){
quotient = dividend / divisor;
remainder = dividend - divisor * quotient;

int find2D(int A[][], int x, int w, int h, int &i, int&j ){
for( int idx=0; idx<n; idx++ ){
divmod( idx, w, i, j );
if( A[i][j] == x )
return 1;
return -1;

If the function returns 1, a match was found at A[i][j]