I seriously believe this is not the right way to code. Why can't you use arrays and make the code shorter? Or is it that you are not taught about arrays till now?
Mark as solved if you are done with it.
I seriously believe this is not the right way to code. Why can't you use arrays and make the code shorter? Or is it that you are not taught about arrays till now?
Mark as solved if you are done with it.
In your code you are not calling isLeapYear()
//if (!isLeapYear) -- wrong
if (!isLeapYear(year)) //right way to call the function!!
Why I preferred unsigned is that there is not such thing like a negative date, month or year.
oh come on!!!
I should not have spoon fed :(
1. There is no harm in using int variables. change unsigned to int.
2. += can be replaced with ordinal_day = ordinal_day + month;
3. Last but not least, you must have a bool function for leap year check. I didn't take the pain in writing one for you. So I put a comment inside if-condition. You have to call a bool function, inside the if-condition. The argument to that function should be the year and it returns true or false.
Why can't you do something like this.
unsigned int mm; //holds the month
unsigned int dd; //holds the day
unsigned int yy; //holds the year
unsigned int ordinal_day = 0; //holds your ordinal day number
//define an array which holds the number of days in each month
unsigned int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (/* yy is leap year*/)
month[1] = 29;
for (int i = 0; i < (mm-1) ; i++)
ordinal_day += month[i];
ordinal_day += day;
Dude, you are giving us a 500 line code and asking us to find the bug. Why can't you debug a bit on your own and post only that code portion which is the reason for your "lock up"?
The program can be greatly simplified
But the reason why he did this way is to output the count down. In fact, count up. Starts from 0:00 to 1:30
at line 29 you are setting seconds back to 0. So it's an infinite loop.
Use a different variable in for loop condition.
try this :)
while(std::cin >> numberinput){
//std::cin >> numberinput;
LinkedList.InsertFront(numberinput);
}
You are passing the pointer by value. And you are swapping the copy of the actual pointers in swap() function. When it comes out from the function, the pointers' copies go out of scope and hence no swap happens.
One solution is to pass the pointer by reference.
Other solution is to swap the values rather than swap the pointers (in swap() function) like shown below.
void swap(int *x,int *y)
{
static int temp;
temp=*x;
*x=*y;
*y=temp;
}
How about this??? :cool:
unsigned int grade_index;
unsigned int score = 35; //this is your score. use scanf to get it.
grade_index = score/55 + score/65 + score/75 + score/85;
char grade = 69;
grade = grade - grade_index;
printf("%c", grade);
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.
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 :)
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?
I think a closing bracket is missing
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.
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/
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.
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?
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
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.
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. :)
first of all if you intend to use a[] as a string, it should end with '\0' character.
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
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);
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;
}