subith86 17 Junior Poster
subith86 17 Junior Poster

That didn't look like a circular linked list. Anyway, I made a few corrections and it worked fine.

void add()
{
    int n;
    cout << "What is n? ";
    cin >> n;
    if (n > 0)
    {
        Head = new(node);
        Head -> Item = n;
        Last = Head;
        for (int i = n-1; i >= 1; i--)
        {
            Temp = new(node);
            Temp -> Item = i;
            Last -> Next = Temp;
            Last = Temp;
        }
        Last -> Next = Head;
    }
    else;
}
void print_circle(ptrType Head)
{
    if(Head!=NULL)
    {
        Last = Head;
        while (Last->Next != Head)
        {
            cout<<Last->Item;
            Last = Last->Next;
        }
        cout<<Last->Item;
    }
}

But I would suggest you to try working it out yourself.

subith86 17 Junior Poster
bool dayType::equalDay(const dayType& otherday) const
{
    return (this->day == otherday.day);
}

"this" is the object with which you are calling equalDay().
You are comparing this's day and otherdays's day and returning the result.

subith86 17 Junior Poster

You are calling a non-const function by a const object. Hence the error. Make the functions as const and you'll get rid of this error.

int statistic::length() const
{
    return amount;
}

There seems to be another solution to this. I haven't tried it and I seriously believe it is NOT a reliable solution. Use const_cast on your object before calling those non-const functions.

subith86 17 Junior Poster

It seems the assertion failure is coming from my for statement in line 81. The purpose of this for loop is to print the "input" string backwards, and assign those values the the array of "convert" array. How do you think I can modify this so that I won't have an assertion error? The error it said is "Expression: string subscript out of range."

Yes, might be. As I told you, I didn't go through the entire code. But at first glance I spotted out the integer overflow error with digit10 and digit9.
Go ahead with correcting the error pointed out by WaltP. It may solve the assertion problem. But at some point the integer overflow problem will trouble you. :)

subith86 17 Junior Poster

I didn't try to understand what you are doing in your code. But, I see one mistake.
Variables like digit10 which is of int type cannot hold such a big value.
If int is of 4 bytes, it can go only till 2147483647 but digit10 is assigned with 68719476736.

subith86 17 Junior Poster

Error 1 error C2106: '=' : left operand must be l-value (error on line in bold)

Did you mean to put the value you get after summing sum and count into ex. If so, you should do like this.

ex = sum + count;

Warning 2 warning C4244: '=' : conversion from 'double' to 'int', possible loss of data

count is declared as an int, which means it can hold values like 100, 101, 102... but not 101.43 or 3.14. But here you are trying to assign 25.67 to count. This is not an error, but eventually your count will ignore 0.67 and store only 25. So, if the 0.67 is important in your case you should use data types like double or float for count

subith86 17 Junior Poster

Oh, no!!!
No while loop to check for validity. But since you are a beginner, I understand you'll take some time to get a hang of things.

Validations are like checking whether a condition is met or not. And for a condition, the right candidate is if(condition).

Something like this

if((mm > 0) && (mm <= 12) && (dd > 0) && (dd <= 31) && (yyyy >= 0))
{
    //valid
}
else
{
    //invalid
}

But there is a problem here. If a user enter Feb-31, it gets validated. So the validation thing is a bit complex for a 2-week guy. You have to use a very complicated if-condition to achieve the purpose. Or, I would go with arrays. Storing the number of days in each month in an array and validating it. But I don't think so that you are taught arrays by this time. So, leave the validation part as of now.

Now coming to your existing code, i feel the else part is not needed. The three possibilities between two integers are '<', '>' and '==' all of which are handled in separate blocks. Whatever combination of integers you give, it will never reach the "else" part.

And glad to see that you are using int main(void) as the prototype of your main function, unlike other beginners (including me, when I was a beginner) who use void main() :) Also mark the thread as solved, when done :)

subith86 17 Junior Poster

the reason why no body is helping is that your code is not intended at all, which makes it difficult for us to read. Please format it with proper intendation and the re-post. Also remove all the blank lines in between.

subith86 17 Junior Poster

Completely untrue. Write a test program and see.

Thanks WaltP. I didn't know that. I posted it with the knowledge that int i = 09; gives an error.
So, I believe scanf will be doing some formatting to 09. Am I right?

subith86 17 Junior Poster

I think a closing bracket is missing

subith86 17 Junior Poster

I don't see any validity checks done for your dates. He can enter invalid dates like 33/33/2012.
Once the dates are validated, the job is easy.

day1 = yyyy1 * 10000 + mm1 * 100 + dd1
day2 = yyyy2 * 10000 + mm2 * 100 + dd2

Check which is greater out of day1 and day2. So simple, isn't it :P

But there is another potential error in your program. Your variables mm1, dd1 are declared as integers and in your console out put you ask the user to enter in mm/dd/yyyy format. So for September, the user will enter "09". But since mm1 is declared as int, "09" will be treated as octet number and you'll get run time errors, since 8 & 9 are not allowed in octet system. So better use strings.

subith86 17 Junior Poster

I find lot of bugs in your code.

1. In the main function you are calling createAndCount() without any arguments, but where as from the signature of the function, it is seen that it accepts an integer.

2. In count() you are using a variable 'n' in the for loop. But it is not declared anywhere. Or do you have any global variable which you haven't posted along with your code?

3. The call to count() from createAndCount() is wrong

//sum = int count(int intArray[i]); ---wrong---
//It should be
sum = count(intArray[i]);

4. Provided the 3rd suggestion is exactly what you want, you have another bug. The prototype of count(). When you pass an integer, count() cannot have an integer pointer as a parameter.

5. But if the 3rd suggestion is wrong, you have another problem. At first glance, what I see is that you are doing some array operations inside count() which means that you are supposed to pass an array to it from createAndCount() which is like this.

sum = count(intArray);
subith86 17 Junior Poster

I write a small program to measure the time

Why don't you put the solution in a loop. Loop it some 100000... times, so that your program will give you some number. Do it separately for both solutions.

subith86 17 Junior Poster

check this link, this should help you
http://linux.die.net/man/3/strtol

For C++ programmers, there is another solution
http://www.cplusplus.com/articles/numb_to_text/

subith86 17 Junior Poster

once it gets back to process_and_display_information, the data is gone.

Yes, it will be gone, because the scope of myStrings is only within tokenize_urls(). myStrings is created in the stack and once the control goes out of tokenize_urls(), the memory will be freed. That is why you lose your data at process_and_display_information().

To solve this, you have to allocate memory for myStrings using new. By this way, the data of myStrings will be stored in heap. Even if tokenize_urls() finishes it's job, myStrings will still be there in heap and can be accessed by process_and_display_information(). Remember to delete the memory in process_and_display_information() once you are done using myStrings.

Another way to solve this is to declare myStrings in process_and_display_information() and pass it by reference to tokenize_urls(). Let tokenize_urls() be a void function and let it modify myStrings. So when you get back to process_and_display_information() you will have the modified myStrings with you.

I prefer the second method because it doesn't involve any memory allocation and delallocation to be taken care by the programmer.

freedomflyer commented: Very, very clear and well thought out explanation. Even bolded terms and such to make it more clear. +0
subith86 17 Junior Poster

1. Line 49 : It should be

temp1->ptr=temp;

2. Line 67 and 69 : You repeat the same statement. Delete both the lines and bring one of them after the if-condition.

if(temp1->ptr==NULL)
{return 0;}
temp1=temp1->ptr;

Now it's up to you to think and find what error you made and how these two corrections rectified it.

subith86 17 Junior Poster

you are using getch(), clrscr() all of which are part of <conio.h> which you haven't included. It is also suggested not to use <conio.h> as it is not standard and not supported in many compilers.

You are returning 0 in line 272, but your main function's return type is void. Avoid it.

As said in the previous post, avoid using goto statements too.

subith86 17 Junior Poster
b2 = &b1[0];
(b2+i)->iPageNo

And no need of typedef

subith86 17 Junior Poster

cool :) if so, please mark the thread as solved.
The XOR swapping you have done is less efficient than the swap using temp variable. See this link http://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_use_in_practice
And regarding the compiler question you have asked, I don't know much about it :(

subith86 17 Junior Poster

I don't know why this fails for you. Can you try the swap using a temp variable, instead of XOR.

temp = *q;
*q = *p;
*p = temp;
subith86 17 Junior Poster

What do you actually intend to do? When I ran your code, for an input "abcde" I got an output "edcba". What is the problem you're finding with swap()?

Just one piece of advice. Avoid writing codes like those in line 19 and 22. "++" operator may give different results with different compilers in case of post and pre increment.

subith86 17 Junior Poster

where do i set the parameter to read in 1 char for each index?

Can you be a bit more clear? I believe I gave the solution to what you have asked "Trying to store inputted string as individual chars to individual index's of Array"
What is blocking you?

subith86 17 Junior Poster

possibley im trying to read in 2-15chars in a string(ex: "2ta3" or "4528tj" and store them in seperare index's of an array

In my previous post, my second suggestion exactly gives the solution.

subith86 17 Junior Poster

If it's only for read only purpose, you can use your original string itself as an array of characters terminated with '\0'.

string myString = "hello world";
    const char *myArray;
    myArray = myString.c_str();

Otherwise create a copy and do read/write

string myString = "hello world";
    const char *myArray;
    myArray = myString.c_str();

    char *copy = new char[strlen(myArray)];
    strcpy(copy, myArray);
    //use your copy
    delete copy;
subith86 17 Junior Poster

You can simply enter your input separated by spaces. It need not be \n character always.
For cin>>i>>j; you can actually enter two numbers separated by space.

Or, you want a space to be inserted when you press enter, and wait for the next input?

subith86 17 Junior Poster

great !!! please mark the thread as "solved" :)

subith86 17 Junior Poster

If I dont use 'delete' in those 2 functions, doesn't that cause a memory leak?

Yes it causes. I have posted a second time to your question. See my fourth point in that post. It tells you how to solve.

And what did you mean by I am trying to use memory allocated to the linked list after deleting it?

First look at your addNode() function. You're passing "pointer" to add(). In add(), you make "newItem" point to the memory which is pointed by "pointer" in addNode(). Then you delete "newItem" which means, you deleted "pointer"'s memory. Now the execution in add() is over. It came back to addNode(). Here you are trying to delete "pointer" which was already deleted.

Hope this explanation is good enough for you to understand the problem in your code?? :S

subith86 17 Junior Poster

I will add some other comments on the code.

1. In the constructor, why do you have to allocate memory for head? head is just a pointer to the first block. It doesn't contain any data. Initially head should point to "nothing" since you haven't created any block yet.

Family()
        {
                size = 0;
                head = NULL;
        }

2. In the function add(), you are passing a node pointer for which memory is already allocated at addNode() function. Inside the add() function you are again allocating memory.

void add(node* addMe)
        {
                addMe->next = head;
                head = addMe;
        }

3. In the function printMe() you allocate memory for print. As I see from the code this is supposed to hold only the address and print the values in the block where it is pointing to. So, just a simple pointer is enough.

void printMe(int num)
        {

                node *print;
                print=head;

                for(int i=0;i<num;i++)
                {
                        cout<<print->name<<endl;
                        cout<<print->age<<endl;
                        cout<<print->jobTitle<<endl;
                        cout<<print->salary<<endl;
                        print=print->next;
                        cout<<endl;

                }
        }

4. Make use of the destructor to free the allocated memory for the linked list.

subith86 17 Junior Poster

I thought everytime I allocated memory using 'new', I am suppose to free the memory using 'delete'.

You are right, but you try to use the memory allocated to the linked list after deleting it. This causes the problem. So do a delete only after you are finished playing with the memory you reserved. :)

subith86 17 Junior Poster

check line 35. Syntax error.
Also, I doubt int cardNumber is not initialized anywhere.

subith86 17 Junior Poster

To access element at a[5][4][9][20], use

printf("%d", *(*(*(*(a+5)+4)+9)+20));
subith86 17 Junior Poster

first of all if you intend to use a[] as a string, it should end with '\0' character.

subith86 17 Junior Poster
subith86 17 Junior Poster

the scanf at the end is to keep the program on the screen after it executes and displays the output..

Use getch() function defined in conio.h thereby you can avoid using scanf() to hold the output screen.

And before you proceed, you need to verify the sides belong to a valid triangle. If you remember from old school mathematics, the 3 given sides are the sides of a triangle only when sum of any two sides is greater than the 3rd side. 1, 2, 5 can never be the lengths of sides of a triangle.

Moreover there is no need for the last "else if". A simple "else" is enough.

The error that you made in your code, is clearly shown in the warning from the previous post.

subith86 17 Junior Poster

I don't understand why it happens to you. But here I copy paste my code and output. I use gcc to compile. Not sure whether the compiler is causing the difference in my case. But here it goes.

janus ~/tools $ cat power.c
#include <stdio.h>
#include <math.h>

int main()
{
    double i = pow(2,1000);
    printf("%f\n",i);
    return 0;
}
janus ~/tools $ gcc power.c
janus ~/tools $ ./a.out
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376.000000
janus ~/tools $

In order to verify it's only a printing problem in your case, do an inverse power on the value that you get and see if you get exactly 2 as the answer. I have showed it in my previous post.

Moreover, see this link. Maybe it'll help you in case of printing issues.
http://byuh.doncolton.com/courses/tut/printf.pdf

subith86 17 Junior Poster

means ? can u give me link so that i can learn from there ? i have searched too much. i didn't get any relevant link. please help!!

Try these links
http://programminggeeks.com/recursive-permutation-in-c/
http://www.codeguru.com/cpp/cpp/algorithms/article.php/c5123/

They do it using recursive algorithm.

subith86 17 Junior Poster

this is the c forum use printf() instead of cout

oops, i forgot that !!!

@himanshu : if you want to print the full number then try printing like this

printf("%f",i);
subith86 17 Junior Poster

the function pow() returns double. So you can use double to hold the value.

using namespace std;

int main()
{
    double i;
    i = pow(2,1000);
    cout<<i<<endl;                 //prints 1.07151e+301

    i = i*4;
    cout<<i<<endl;                 //prints 4.28603e+301

    i = pow(i,((double)1/1002));
    cout<<i<<endl;                 //prints 2

    return 0;
}
subith86 17 Junior Poster

assume the string is abcde

first group "abc" and "de" separately
"abc" is the fixed part and "de" is the variable part.
keeping "abc" fixed, there are two possibilities with which you can arrange "de"

Iteration 1
abc|de --- (1)
abc|ed --- (2)

In the next iteration, take out the right most character from the fixed part "abc" and it has to be put in the variable part. In this case the character is "c". Take the case (1). "c" can be placed in the middle of "de" and at the end of "de".

Iteration 2
ab|dce --- (3)
ab|dec --- (4)

Taking case (2)

ab|ecd --- (5)
ab|edc --- (6)

Similarly, continue this process till the end where you reach "a". When you reach "a", stop the grouping and release all the reserved memory. Shift all the characters left by 1 place. New string becomes "bcdea". Continue the above process till you reach "b". Now shift left again and the new string is "cdeab". Repeat till the string "eabcd".

One drawback of this solution is you have to store the strings in each iteration. In iter1 you have to store 2 string. In iter2, you have to store 4 strings, and so on.
Another drawback is, when you have duplicate characters like in string "abccde" this logic will output duplicate results too. I will think of a better solution and get back. This …

subith86 17 Junior Poster

Thank you Moschops. Just out of curiosity I am asking, how is it more efficient? Your answer might be useful for others too.

subith86 17 Junior Poster

thanks for all your replies.
I did some more googling. And i got the solution

char *temp = "poiuytrewq";
std::cout<<*temp<<std::endl;  //should print p
function(&temp);
std::cout<<*temp<<std::endl;  //should print i

And the function prototype will be

void function(char **temp);
subith86 17 Junior Poster

Hi all,
Sorry if this is a stupid question.

Assume I have a code like this.

char *temp = "poiuytrewq";
std::cout<<*temp<<std::endl;  //should print p
function(temp);
std::cout<<*temp<<std::endl;  //should print i

Basically my function() in the above snippet increments the pointer twice so that temp point to 'i' now. I need to know the prototype of function() which can accept a pointer to a character array and change where it points to (something like pass by reference).

subith86 17 Junior Poster

Thank you all,
@firstPerson : your suggestion looks sensible. will think of doing it.
I'll get back in case of any problems

subith86 17 Junior Poster

Hi all,

Here is a short summary of my piece of code.

double availableUnits       = calculateAvailableUnits(some arguments);
int    minimumRequiredUnits = getMinimumRequiredUnits(some arguments);

std::cout<<"minimumRequiredUnits = "<<minimumRequiredUnits<<std::endl; //prints 30
std::cout<<"availableUnits = "<<availableUnits<<std::endl; //prints 30

if (availableUnits < minimumRequiredUnits)
{
    //throw exception;
}
//rest of the code

Even though both variables have the value 30 in it, the if-condition gets satisfied and exception is thrown, which is wrong. I studied the problem a little more and found that the double variable availableUnits infact doesn't contain 30, instead it contains 29.9999. Is there any way I can get rid of this problem? Also, I cannot use ceil() as this would affect the functionality.

calculateAvailableUnits() function does some calculation which involves fractions, so the result 29.9999 instead of 30.

subith86 17 Junior Poster

ooops... what was I doing? thanks for spotting it out... it worked.
I did so many trials, I wonder how all of them had 8 in it :P
anyway thanks

subith86 17 Junior Poster

Hi all,

I'm trying to get an octal number from console and store it in an int variable using scanf. Here is my code.

int n;
	scanf("%i", &n);

	printf("\nEntered number in dec = %d", n);
	printf("\nEntered number in hex = %x", n);
	printf("\nEntered number in oct = %o", n);

At console I enter 0238284, then all the printfs outputs 0.
But the same works for hex, ie, if i enter 0xFA31D, then it outputs the decimal, hex and octal values correctly.

Can somebody tell me why it doesn't work in the case of octal. I'm using GCC compiler on 64-bit machine.

Thanks in advance,
Subith

subith86 17 Junior Poster

ok thanks, one more doubt regarding the same.
The ++ operator is overloaded as shown in the above post. I tried this. And it worked

int j = obj++;

But later i thought of overloading + operator instead of ++ to do the same job. Now the below line gave compiler error.

int j = obj2+;

But this worked after I added another integer after +

int j = obj2+100;

I verified 100 is not going into the overloaded operator function. The function was simply returning a int which could have been assigned to j. It didn't happen. But when another constant is there on the right side it worked. Why is it so?

subith86 17 Junior Poster

Here the value of int x is always zero. I don't know if there is any case where it is non-zero. If so pls reply.

int OperOverld::operator--(int x)
{
    cout<<x<<endl; //always zero
    //some code goes here
    //...
    //...
    //...
    //atlast return some int
}
subith86 17 Junior Poster

You can use

Collections.sort(yourArrayList);

But there is some work you need to do. You have to implement Comparable interface in your class. There will be a method called compareTo (Object o) which you need to override. Here you are free to code how you should sort (using first name or last name or title), such that it should return an integer. Your sort will depend on whether you return a -ve, +ve or zero integer.

Any other clarification required, just post.