siddhant3s 1,429 Practically a Posting Shark

_stricmp is not available in all platform ( not on Linux )

siddhant3s 1,429 Practically a Posting Shark

>>Then you can use stricmp()
stricmp is not a standard function.

siddhant3s 1,429 Practically a Posting Shark

You may use third party library like libcURL(http://curl.haxx.se/libcurl/) which works well in fetching such webpage as strings.

If you problem is solved, please mark the thread as solved so that others don't waste time on this problem.

siddhant3s 1,429 Practically a Posting Shark

Yes, I tried that, it works fine.
Here is the code that I used:

#include <iostream>
using namespace std;
int main()
{
    int month,day,year,date;
    cout<<"Enter a date:";
    cin>>date;
    month = date / 1000000;
    day = (date % 1000000)/10000;
    year = date % 10000;

    cout<<"Date Entered:"<<date<<endl
    <<"Month:"<<month<<endl
    <<"Day:"<<day<<endl
    <<"Year:"<<year<<endl;
}

And this was the interaction at the terminal:

siddhant3s@x10n:~$ ./test
Enter a date:03081991
Date Entered:3081991
Month:3
Day:8
Year:1991
siddhant3s@x10n:~$
siddhant3s 1,429 Practically a Posting Shark

I don't understand your problem, the code works fine:

#include <iostream>
int main()
{
    using namespace std;
    int month,day,year,date;
    cin>>date;
    month = date / 1000000;
    day = (date % 1000000)/10000;
    year = date % 10000;

    cout<<month<<" "<<day<<" "<<year;
}
siddhant3s 1,429 Practically a Posting Shark

Request to delete this post

siddhant3s 1,429 Practically a Posting Shark

>The OP is clearly at a low level
Actually, I was sharing it with you. But then I realised `Oh no, whom do I think I am sharing it with?'
Never mind :)

siddhant3s 1,429 Practically a Posting Shark

Agreed with iamthwee.
There is an intrinsic problem in using a single int as data type for Date.
You can sure go along and use int but then whenever you be needing to extract the individual elements from it (the month, day and year) you must always check if the integer is 8-digit long or 7 digit long and then do your parsing.
Note that throughout C, there is no distinction between 0123 and 123. It is same.

siddhant3s 1,429 Practically a Posting Shark

Technically, space is also a character. Perhaps what you are talking about is the alphanumeric characters.
Now this is simple, just loop through your string and check if each character is alpha-numeric (hint, use isalnum() in <cctype>) , if yes, increase the counter.

siddhant3s 1,429 Practically a Posting Shark

The above post (#10) contains a fairly `nice' code. Though it, too is looping unnecessarily throughout the whole array. Stepping out of the loop once we know that the current character is null '\0' will be better: (modifying the above code)

#include <iostream>
#include <iomanip>  // <- because of setw()
using namespace std;

int main()
{
  // The size of the array, note: 'const'
  const int array_size = 30;

  int count = 0;

  // !! Attention !!
  // Initialize the whole array to '\0'
  char str[array_size] = "";

  cout << "enter a word: " << endl;

  // Safely get the input, no fear of buffer overflow
  cin >> setw(array_size) >> str;

  for (int x = 0; x < array_size; x++)
    if(str[x])
    {
      count++;
    }
    else break;


  cout << count;

  return 0;
}

This code can however be shortened considerably which would eliminate the need of many redundant variables. Here is a possibility:

#include <iostream>
#include <iomanip>  // <- because of setw()
using namespace std;
int main()
{
  // The size of the array, note: 'const'
  const int array_size = 30;
  char str[array_size];

  cout << "enter a word: " << endl;
  // Safely get the input, no fear of buffer overflow
  cin >> setw(array_size) >> str;

  int count;
  for (count = 0; str[count]; count++) ;

  cout << count;
}
kvprajapati commented: Helpful! +9
siddhant3s 1,429 Practically a Posting Shark

To the OP:
When we (at least I) ask you to show up a code and we will try to fix it we say it in a spirit that a car mechanic would say ``Sir, I can't build a car for you, but if you have a defective car, I can repair it". What you are doing is giving him a toy car and saying ``here is my car, please fix it so that I can sit in it".
In and all, it means that you won't be getting any pre-made code over here: learn the language, write the code (doesn't matter if it is not working), and then ask for help incorporating the details of all the experiments you did with it when trying to figuring out why it didn't work.
Use, code tags. void main is bad.

@Banfa
That essentially the Sieve of Eratosthenes


@Narue>A common optimization for checking the primality of x is only testing from 2 to sqrt(x)
I just went through one of the article which says that rather than checking if i<=sqrt(x) , it would be wise to check if i*i<=x. A small trick I thought worth sharing.

siddhant3s 1,429 Practically a Posting Shark

int is a very poor choice for a data representation of Date. The right thing would certainly be to accept the user input as string -- as is and then parse it according to your needs.
I would even go as to creating a struct for holding three short int values.
Then I can always make a function parse_date(std::string date) which can parse the entered string and return me the struct.

siddhant3s 1,429 Practically a Posting Shark

Toward the superiority of vector vs array or vice versa:
when you are not bound by any performance issue (like optimization for speed or size), common sense would dictate that vector could be a more suitable choice. There are many reasons to choose vectors. Listed over here http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1

siddhant3s 1,429 Practically a Posting Shark

Any character in single quote has type of char.
Double quotes are used for string. They have type const char* that is pointer to char (whole values can't be changed).
So, "a" is actually an array of two elements : 'a' and '\0'

siddhant3s 1,429 Practically a Posting Shark

Oh, I never saw that. Great,
But still, your loop won't work because of the said reasons.

siddhant3s 1,429 Practically a Posting Shark

Oh, I never saw that. Great,
But still, your loop won't work because of the said reasons.

siddhant3s 1,429 Practically a Posting Shark

I can't see where are you comparing a pointer and an int.
In the loop condition, msg is a char and i is an integer. Pretty legal.
msg is having value 0. So I guess the loop would never execute.

PS: If your compiler is generating any error, please post it along.

siddhant3s 1,429 Practically a Posting Shark

When a program doesn't work as expected, *think* how it is working currently.
You have declared an array of 30 chars.
You are running a loop for each of those char. If it is not zero (which it would if it was the post-last character of the string, the null-character), you are incrementing the count. Pretty well till here, but what should happen if the current character, is actually a null-character? The loop should terminate. But in your case it is not doing so.
Now you see the problem?

siddhant3s 1,429 Practically a Posting Shark

>it's working but the code is very complex to me
Do not know about the complex part, but the code is wrong, do not follow it.

caut_baia, you were wrong in two ways: first, you tried to give the OP a direct answer which can spoil his fun of `getting' towards the answer. It's not the answer which is important but it's the path to the answer which is.

Secondly, you failed to allocate any memory allocation to str, it is pointing to some random memory location and because you are cin-ing to that location, you are invoking undefined behaviour. Please allocate enough memory to str before using it.

I think the no one understood the point that Ancient_Dragon wanted to make about the 50 character limit. He wanted the OP to consider the std::strings. They will make the program flexible to accept any length long input string.

Ancient Dragon commented: Excellent comments +28
siddhant3s 1,429 Practically a Posting Shark

The easiest way, which is in fact the more elegant way to do things would be to declare a struct or class of a card. Create a vector of such 52 cards and use the standard library's random_shuffle() [http://www.cppreference.com/wiki/stl/algorithm/random_shuffle] to shuffle it.

siddhant3s 1,429 Practically a Posting Shark

>I need a function in c++ to calculat the age of a user, by asking the user to
>enter his birthday.
I think you're confusing this forum with the one who give away code.
Read the forum announcement. We help only those who show efforts.

What have you tried so far?

siddhant3s 1,429 Practically a Posting Shark
siddhant3s 1,429 Practically a Posting Shark

I suggest you to change the return type of the menu() to int. If the user wants to continue return 1 and he doesn't wants to continue(that is, when he pressed 5), return 0

Then inside your main(), you can simply do like this:

int main(){
while( menu() );
}

Note that <iostream.h>is depracated header file, use <iostream> instead.

Also, post your code next time in code tags

Edit: the above post by Grn Xtrm is also another way to tackle this.

siddhant3s 1,429 Practically a Posting Shark

>for ex if an array has 10 items then n-1 would give u the size of that
>array, since index of arrays stars from 0.

I think it should be that if an array has n items then n-1 would give u the highest possible subscript of that array, since in c,c++,python (and other popular languages) index of arrays stars from 0.
When studying about algorithms, array doesn't necessarily 'starts' with a zero. Many author use 1-based array, namely the ``Introduction to Algorithm" by CLRS

To OP:
Please re-frame your question such that it could be understood.

siddhant3s 1,429 Practically a Posting Shark

The if-else is redundant;
A number which is smaller than 10, when undergoes modulus operator with divisor as 10 will yeild that number itself.
7%10=7
1%10=1 and so on...

Your reverse function is not designed well:
You are trying to print the reversed value of the number in the function itself, hence the function should not return anything.
If at all you want to return the reversed of the number(without printing it), you will have to think of doing something more!
One easiest way would be like this:

Algorithm R (to reverse a integer N and store it in s):
R1(initialisation): Let s=0, i=(number of digit in original number N) -1.
R2(check): Is N>0? If No, GOTO R6. If yes, proceed to R3
R3(extract last): Extract the last digit of N and place it in d.
R4(Add): Add d*10^i to s
R5(Loop): N=N/10; i=i-1
R6(end): STOP, s is the reversed integer.
siddhant3s 1,429 Practically a Posting Shark

Well, I remember I ran into a problem when I needed to install MinGW on a machine with no Internet Connection.
So here is what I did: I downloaded the Codeblocks setup from the website: the one which is of nearly 20MB ( that comes along with MinGW). I installed codeblocks (thus installing MinGW)

siddhant3s 1,429 Practically a Posting Shark

How would I set up the "For" loop in this case??

For (count = 1; number != seed; count++ ) ??

Did you tried it?
Hint: Yes, you are on the right path. Just go on... ;)

siddhant3s 1,429 Practically a Posting Shark

>how to use isalpha to chech for a character
Check the reference.
Next time, if you intent to ask a C-related question, check out the C Forums of this website.

By the way, a quick example of isalpha is:

#include<stdio.h>
int main()
{
  char c;
  printf("Enter a char");
  scanf("%c",&c);
  if(isalpha(c)) printf("The char you entered was an alphabet");
  else printf("The char you entered was not an alphabet");
}
siddhant3s 1,429 Practically a Posting Shark

Learning from examples is a great way to understand things; only if you understand the example completely before trying to fit your crude analogies.

And when you are not able to construct a basic loop, a quick look-up on to your text book wont do harm.

I would suggest you to use a for loop instead as it better suits your need ( ignore this advice if you are doing this problem as a homework given from a teacher with instruction as ``you HAVE TO you a while loop")

Here, a link about for-loop.

siddhant3s 1,429 Practically a Posting Shark

If at all you want to throw string literals, remember that they are const char* not simply char*. So refactor your code as:

void Xhandler(int test)
{
    try
    {
        if (test) throw test;
        else throw "Value is zero";
    }
    catch (int i)
    {
        cout << "Caught One! Ex. #: " << i << '\n';
    }
    catch (const char *str)
    {
        cout << "Caught a string: ";
        cout << str << '\n';
    }
}

But then, before you find throwing string literals very comfortable, read this article http://www.informit.com/articles/article.aspx?p=30642&seqNum=5

siddhant3s 1,429 Practically a Posting Shark

>How do I give out Rep on this site?
What is 'Rep'.
If you mean reputation, can just click on the "Add to <username> Reputation" on the post for which you want to repute the user. Choose "I Approve" and click "Add to Reputation"

siddhant3s 1,429 Practically a Posting Shark

Uh good. But I think you realised your limitation.
For sorting integers, there are many algorithm available.
But before that, you need to build a common program to try those. This program includes code to input N numbers into an array. Then it sorts the array using one of the algorithm and finally prints the result( that is the sorted array itself).
So go and try each of the algorithm and post back if you find any difficulties.

Tip: start out with insertion sort.

siddhant3s 1,429 Practically a Posting Shark

stringstreams are often useful when you want to parse a sting input, specially when you need to do something with words.
So make a stringstream and use the >> operator to fetch out one word at a time. Check if that has a .length() equal to 3. If yes, increase the counter.

siddhant3s 1,429 Practically a Posting Shark
rev1 = 0;
{
num3 *= 10;
num3 += rev1%10;
rev1 /= 10;
}

Looks like you are trying to loop some statements. But the syntax of the loop is not correct. Read http://www.cplusplus.com/doc/tutorial/control/ (Loops) to find out the correct format.

siddhant3s 1,429 Practically a Posting Shark

Use a std::map.
Create a map<std::string, int>.
Scan through the file and insert each of the word to the map with initial value 1. If the word already existed, increment its value.
Study the insertion example given on http://www.cplusplus.com/reference/stl/map/insert/

siddhant3s 1,429 Practically a Posting Shark

>Its c++, is there anything cute about it?
Yes. I find std::strings , std::vectors very cute. I have a less of headache when I use these (provided I am not concerned so much about the program speed).

siddhant3s 1,429 Practically a Posting Shark

Consider the parenthesised version of the OP's code:

double ExprTree::evaluate(TNode* p) const
{		//this is the first if
        if(	p->data == "+" ||
			p->data == "-" ||
			p->data == "*" ||
			p->data ==  "/"		)
                {
		            double op1 = evaluate(p->left);
		            double op2 = evaluate(p->right);
		            
					if(p->data == "+")			//if number 1.1
		                    return op1 + op2;
		            if(p->data == "-")			//if number 1.2
		                    return op1 - op2;
		            if(p->data == "*")			//if number 1.3
		                    return op1 * op2;
		            if(p->data == "/")			//if number 1.4
		                    return op1 / op2;
                }
        else
				{
					if(isalpha(p->data[0]))		// if number 2
		                return getVariable(p->data);
        			else
                		return atof((p->data).c_str());

        		}
}

Thinking logically, we all know that this function will return a value [1].
But the compiler cannot determine that. This is because the compiler is unable to realise that when the condition of the first if is true then at least one of the four conditions of if statements(if number 1.1 to 1.4) will definitely be true. The compiler cannot logically relate the original condition and the distributed condition. So, according to the compiler, there can arise situations when all of the internal if's (if number 1.1 to 1.4) be false at the same time. Hence, (according to the compiler) there may arise situations when the function returns no value, thus the warning.

[1]: If it is not immediately clear that this function will definitely return something, you should perhaps check the structure of the if lader thus formed. If the if_1 is true, at least one of …

siddhant3s 1,429 Practically a Posting Shark

>but also i guess "cin" might be intelligent enough to know whether the input is valid or
>not
cin is intelligent enough that it won't at least calmly accept a invalid input. But when the input is not valid, say you entered a character when a integer was expected, cin gets 'mad' and does quite arbitrary things. For sure you could check the fail bit flags to validate your input but then..... its up to you.
The bottom line is: cin, by default do not handle invalid input so cutely. You definitely need to put an effort to make it behave properly.

siddhant3s 1,429 Practically a Posting Shark

>>nope.can you help me solve my problem??please...

>>hey there FirstPerson.I m new with c++ so i think i m zero with it.can you help me
>>with my problem??

>>As you know firstPerson i m new with c++.I m think i m a litle bit blur what just you
>>tech me..Sorry.Could you solve my c++ question entirely??with example also??

All these statements of yours shows that the only thing you are interested is in getting the pre-cooked solution and run with your feet off.
Showing yourself a big looser ( ``I don't know C++ thus help me" ) doesn't help you. It would have been so much better if you would have read your textbook and at least given a try to the problem.
Read>> http://catb.org/~esr/faqs/smart-questions.html#id382403

siddhant3s 1,429 Practically a Posting Shark

>U dont need to validate them like that.
Actually he needs to. If the user enters any non-numeric character, his program can hang, halt, break or whatever. The worst part is, that the programmer won't be able to display a error message.


>Link
failbit, badbit, goodbit and stuff only makes my code difficult to read and understand.

If at all I am curious about what my user is inputting, I always input it in form of string and just parse it my own way.
I thus get a lot more control over the input I received.

siddhant3s 1,429 Practically a Posting Shark

Get some sand and clay. Try to first model desks and benches. Then model teacher and students. And your class is ready.

siddhant3s 1,429 Practically a Posting Shark

This is called explicit template instantiation.
Read http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12

siddhant3s 1,429 Practically a Posting Shark

>>little ugly
Is it?
This is the correct way to the thing what you want.
You create a class template when you want to separate the implementation of the whole class according to the template parameter. But here, this is not what you want to do.
In fact, you could templatize the class along with a different function template:

#include <iostream>

using namespace std;
template<typename C>
class A
{
public:
	A() { }
	template<typename T>
	void Print(T a) { cout<<a<<endl; }
};
int main()
{  
	A<int> t;
	t.Print(3.1415f); //implicitly
	t.Print<unsigned int>(100); //explicitly

	return 0;

}

Here we are using two templates: one for the whole class (with C as the parameter) and other for the particular function only.

StuXYZ commented: class+function templates together are too often overlooked. +6
siddhant3s 1,429 Practically a Posting Shark

>can u plz explain that when is the order of precedense will be right to left?
Are you deaf?
Look at the post #2 and try to accept that what ever niek has said is actually true.
Your program is suffering from undefined behaviour. All that means to you is that 'anything' can happen when you run the program.
There is a FAQ on Bjarne Stroustrup's FAQs(http://www.research.att.com/~bs/bs_faq2.html#evaluation-order) and also on Marshal Cline's FAQs(http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15)

Try to read and understand them.
In simple term:
1. Sequence point is a point of time while executing your code at which all the variables have defined values.
2. Any operation done which alter the value of a variable more than once in between only a pair of consecutive sequence point leads to undefined behaviour.

No doubt, there are teachers who put forward these kinds of question trying to show off how tricky their questions are . Actually, all they are doing is teaching their student to depend on studying implementation specific behaviour.

Regarding, the use of void main, conio.h and clrscr(), read http://siddhant3s.elementfx.com/

kvprajapati commented: Great explanation. +17
siddhant3s 1,429 Practically a Posting Shark

>In your function, you allocate new memory for the array
>that is passed, which already has been allocated memory.

I suppose he has commented that line.
Hence technically, there are no memory leaks in the first program.

OP>Why is that anyone know?
That is because the .size() member function of the std::vector returns a size_t and not a int. size_t is usually a unsigned int defined by your implementation. Hence it is actually safe to do for(size_t i = 0; i< vec.size(); ++i)

By the way, in your second program, rather than first creating a vector and then resizing, it would have been better if you could have constructed a vector of the given size before hand.
Anyways, it wont matter here for the problem in hand.

Edit: I replied a bit slow.

siddhant3s 1,429 Practically a Posting Shark

I think one should always try to look at the date of the last post before replying to a thread no one is interested to read.
It is more than an year old.

siddhant3s 1,429 Practically a Posting Shark

>Task:

>Write a program to grade a test and print the student’s score.
The task was given to you. You will be given grades for that.
You are redirecting the task to us, we won't get anything out of it. But that was perfectly fine if you could get any help out of it. If we will cook your food, how will you manage to live ones we are gone?

siddhant3s 1,429 Practically a Posting Shark

Its a project-euler.net problem No. 67 Right?
>My program for this program is not working
>can you please help.
Perhaps not. Seriously, I don't have enough time to spend and debug your code but I do can help you by guiding you to a correct algorithm:
Go the the second last row, for each of the number in that row, replace it with the maximum of the sum with either of the two neighbor:

21 26 32 [U]15[/U] 14 84 68 12 
11 21 32 35 01 28 32 99 12

say for 15, the path with the largest sum would be the one containing the 35. So you replace 15 by 15+35.

Do this for each of the element in the second last row and delete the last row completely.

After this, you will get a triangle with n-1 rows. Do the above again. That is, again replace each element of the second last row (third last for the previous triangle) with the maximum sum possible for the last row(the second last row of the previous triangle).

Ultimately you will get the largest sum possible.

siddhant3s 1,429 Practically a Posting Shark

>Got exam tomorrow
So you've been screwed.
Now did you remember when your instructor told you to read a text book?

>Anyone know that c program got wat type of error?
Yes.

tux4life commented: I've exams tomorrow, can you help me? :P +21
siddhant3s 1,429 Practically a Posting Shark

>1. Passing into a function and then into a sub-function.
Did you asked Google? Anyways, I did it for you: http://www-ee.eng.hawaii.edu/~dyun/ee160/Book/chap6/section2.1.2.html

>2. Passing 2d arrays, this had just mucked me up.
http://www.physicsforums.com/showthread.php?t=270319 and http://cboard.cprogramming.com/c-programming/97898-passing-2-dimensional-array-function.html

>3. Making comparisons between pointers and ints
Sorry I did not got this properly to make any remarks. :)