tux4life 2,072 Postaholic

You can use the sin function from the C++ library ...
(Look here for a reference)

tux4life 2,072 Postaholic

Did you actually write this code ?

tux4life 2,072 Postaholic

Take a look at this tutorial ...

tux4life 2,072 Postaholic

Maybe you should also add an error handler ...
e.g: If this is your input:
a: 1
b: 2
c: 2

You'll get an error ...

Maybe it's useful to first check whether (b*b-4*a*c) > 0 ...

tux4life 2,072 Postaholic

Why are you passing a pointer to your array in the function void quadraticFormula(double a, double b, double c, double *totalResults) ??

You can also pass an array to a function as follows: void quadraticFormula(double a, double b, double c, double totalResults[]) as an array's name can be used as a constant pointer to that array ...

Edit:: With a constant pointer to that array I mean: a constant pointer to the first element of that array ...

tux4life 2,072 Postaholic

cin >> tside; ?

Yup that's right! :)

tux4life 2,072 Postaholic

Use 'cin' ...

Edit:: e.g: cin >> your_variable; Edit:: e.g: cin >> tside;

tux4life 2,072 Postaholic

Instead of exit(0); , consider returning a bool from your function for success or failure. Then the program can continue.

Yup, I forgot to mention that, but generally it isn't recommended to use the exit(0); function ...

I second the cin.get() thing. It probably doesn't really matter in this case, but it's something to consider.

I only gave him a better alternative to system("PAUSE"); ...

tux4life 2,072 Postaholic

> Can I use .NET with C++ ?
>> Yes, you can if you're working with Microsoft Visual C++ (2008) it should be easy to do so ...
>> Edit:: This link might be useful for you ...

tux4life 2,072 Postaholic

It's better to avoid using system("PAUSE"); (look here for more info)
You could use cin.get(); instead ...

tux4life 2,072 Postaholic

I guess it's more of a principle thing for me. All other things being equal, if one thing has benefits over the other (even if they are very small), you should choose that thing.

I just find when I stray from this, even on small things, I am more likely to stray from it with bigger things. That's why it's so important to me. But really not a big deal in this discussion.

Now if you said there are benefits to no space, I would be more likely to accept it as being ok.

If you don't type a space, your code will be 1 character shorter, less typing :P ...
But actually both don't have real benefits, it's only a matter of preference ...

tux4life 2,072 Postaholic

Agreed with all above :)

tux4life 2,072 Postaholic

I disagree.

Why it's just a useless discussion, and what's the main topic?
"a space" ...

BTW, What wrong choice could he make in this case ?
NO ONE !
Because the C++ preprocessor is just ignoring that space ...

tux4life 2,072 Postaholic

You should not put any spaces
between the arithmetic expression, it should
be written as 5+3 or -6+3, and so on

Yeah, agreed, but you're expecting the user is always inputting valid information, without ever making a typo, so it might be better to do some input validation and display an error in that case ...

tux4life 2,072 Postaholic

Of course it is but when given a choice people often make a bad one ;)

I'm not trying to make a big stand on this but I would say this is one of those things that just make sense to do and I thought I would encourage it.

In this case he can't make a bad choice ...

And here stops the discussion !

tux4life 2,072 Postaholic

It was a suggestion for readability. It's minor but the space should be there as far as I'm considered. But it's just my opinion.

Actually it doesn't make much sense as it's his choice to write it with or without a space :) ...

tux4life 2,072 Postaholic

Thanks a lot for that suggestion there, it condensed the code and made it much more manageable! All though if I could trouble you to explain exactly how that works and is able to be the equivalent of several if statements, I would be very appreciative and probably a much better programmer. :)

OK, I'm trying to explain this to you:

We start looking at the graphical representation of the instruction 'char board[9];'

 0  1  2     3  4  5     6  7  8
[ ][ ][ ] | [ ][ ][ ] | [ ][ ][ ]

(in total we've 9 elements in the array (element 0-8))

 0           3           6
[ ][ ][ ] | [ ][ ][ ] | [ ][ ][ ]

 |           |           |
row 1       row 2       row 3
starts      starts      starts
here        here        here


If we know the row where we want to write in, we first have to calculate the actual place
of the row in the array before we start writing ...

So, after a bit trying you can find the place where the row starts (in the array)
by using the following formula:

row_place_in_array = (row-1)*3 ;
                             |
                  in each row there are
                     three columns

Now we only need to find a formula which is indicating the place in the array if we know
the row and the column ...

We already know the formula to find the row in the array, so if we start from here and
if we adjust …
tux4life 2,072 Postaholic

There's an extensive thread about flushing the input stream, you can find it here ...

hurbano commented: thanks for the help +1
tux4life 2,072 Postaholic

Include a space between #include and <iostream>

The space isn't really needed, the code will also compile without a space between #include and <iostream>

tux4life 2,072 Postaholic

Agreed with all above ...

To the OP: Please remember that every C++ keyword is written in lowercase, this was introduced to keep everything simple, so actually you don't have to hesitate whether you've to enter an uppercase letter or a lowercase one, it's just always in lowercase ...

tux4life 2,072 Postaholic

Did you try searching Google ??
Remember Google is your friend !

This is some simple info about linked lists ...
And what about this one ?
Or this one (from professor Narue, still not complete, but it covers the basics and it's very detailed) ?

tux4life 2,072 Postaholic

Your Example is cute and all (and a bonus, it works =]) but firstly its in C++ and while I like C++ I want to do it in C hence why its in the C forum ^_^. secondly this doesn't do what I want it to do.

Until you translate that to C we can't move any further with that code.

Oh, sorry for that I didn't notice I was posting in the C forum ...
In that case you'll have to take a look at the link I posted ...

tux4life 2,072 Postaholic

You can find a tutorial about C++ here ...

tux4life 2,072 Postaholic

Yeah, siddhant is absolutely right !!

But this link might be helpful too ...

tux4life 2,072 Postaholic

Yes I did notice that it returned a int type, I designed it to be like scanf which returns the number of variable scanned (at least thats what it did last time I checked, I can't seem to remember =[ ).

*Starts Testing tux4life's suggestion*
...
... ...
... ... ...
Ah, I see. Apparently your method fails because of the fact that its an array of characters, but that is a datatype I hadn't thought of before so I will experiment with it

It fails because you implemented it in the wrong way ...
Maybe the following thread might be useful for you: http://www.daniweb.com/forums/thread56000.html

I've also included an example:

#include<iostream>

using namespace std;

char * strRet();

int main()
{
	char * string;
	string = strRet();
	cout << string << endl;
	delete[] string;
	return 0;
}
char * strRet()
{
	char * sth = new char[15];
	strcpy(sth, "Hello World!");
	return sth;
}
tux4life 2,072 Postaholic

I think you should also do some form of syntax checking ...
Check what happens if you input 56 + 5 ? (with very much spaces between '56' and '+' ... And with very much spaces between '+' and '5') ...
You could make a function which reads out all the spaces (and characters between the operand and the operandus)
Or, you could just give an error ...

You have to decide it !

BTW, Nice program ! :)

tux4life 2,072 Postaholic

Did you already notice that your function getString() actually is returning a value of type int ?

To return a c-string your function has to return something of type const char * ...

tux4life 2,072 Postaholic

its just like -_-
i sorta get the syntax of code sorta
but like it all seems like gibberish i dont know what to put where i see everyones response and i can read them and im sure they make sense but to me it might as ell be in a different language and this stuff is so frustrating i was crying in the middle of class and had to leave cause i dont get this stuff and thats just more stuff i missed and just less i know how to do and i was lost enough before and i just wanna drop out but i need to pass this stupid class for my major for some ridiculous reason and just ahhh i wanna scream

i dont know what a dynamic variable is, how to make one, why youd wanna, i dont get what classes are, set functions none of this stuff..ugh sorry to rant

In that case you should read a tutorial/book about C++ first ...

tux4life 2,072 Postaholic

Thanks a lot for that suggestion there, it condensed the code and made it much more manageable! All though if I could trouble you to explain exactly how that works and is able to be the equivalent of several if statements, I would be very appreciative and probably a much better programmer. :)

It's just a formula I found by making a graphical presentation of the array which represents the board, and after searching a little time I came up with this ...

You can try it out and you'll see it actually works ! :)

tux4life 2,072 Postaholic

I was just trying to stem any confusion. I think we can all drop this topic, doesn't look like the starter is interested.

Agreed, he has only one post in this thread ...

tux4life 2,072 Postaholic

Please post using code tags !

tux4life 2,072 Postaholic

A C++ compiler which follows the ANSI/ISO-standard will never compile this code ...

tux4life 2,072 Postaholic

Dear rahul,

I thought you were saying that compiling the following is possible:

#include <iostream.h>
using namespace std;
...

So, I must have misinterpreted your post, sorry for that !

tux4life 2,072 Postaholic

In your function phase1 you've to change everything in the while loop to:

c = in.get();
/* We use an if-statement to ignore the newlines */
if(c != '\n')
{
        c = (int)c;
	array[c] = array[c]+1;
}

It should work now !

tux4life 2,072 Postaholic

I guess the unwanted number '1' is the number of newlines your program has counted so far ...

tux4life 2,072 Postaholic

I actually don't understand your question ...

tux4life 2,072 Postaholic

This function have to change order of chars: first becomes last, second - last but one etc. Then, I have to write some simple program with this function.

Actually that's the same as: Write a function which uses recursion to reverse a c-string ...

tux4life 2,072 Postaholic

I too can see nothing wrong in your code (syntactically) except the thing what vmanes did already mention ...

tux4life 2,072 Postaholic

since the person itself has written the code using namepace . i assume that she is working on gcc compiler ,not the old tatted turbo c . thus there isnt any thing wrong with the code i have mentioned .

Wrong, look here ...

tux4life 2,072 Postaholic

Sounds like a class will be useful to implement it ...

tux4life 2,072 Postaholic

Awfully! You compare unitialized variable c...

Oh, that was a dumb mistake !

Change line 3 in the code I posted to int c = a; ...

tux4life 2,072 Postaholic

Actually you can replace lines 37 - 63 with: board[((row-1)*3)+(col-1)] = *player; ...
Or with board[((row-1)*3)+(col-1)] = player; if you're using a simple char-variable ...

tux4life 2,072 Postaholic

Where are you having problems with?
With defining the class and it's data members (read this) ? or with writing a program to solve the problem described above (look at lerner's post) ?

tux4life 2,072 Postaholic

Yeah, it's true what niek_e said ...

You could make use of a function like this one:

int get_range(int a, int b)
{
	int c;
	do
	{
		if(c > a && c < b)
			return c;
		else
			cout << "Please enter a number between " << a << " and " << b << ": " << endl;
	} while (cin >> c);
}
tux4life 2,072 Postaholic

If you have to use pointers I would rather suggest you to use C-strings (character based/array of characters) instead of C++-strings as a C-string actually is an array and an array's name can be used as a constant pointer it will be easier to implement it this way ...

tux4life 2,072 Postaholic

>>> You can use it like the sin function

I said that because the function prototypes of the sin and asin function are the same ...

tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

This code will calculate the last digit of a multiplication of two numbers:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int str2int(string s);
string int2str(int i);

int main()
{
	int n1, n2, digit;
	string s1_dig, s2_dig, s3_dig;
	
	n1 = 25;
	n2 = 6;
	
	s1_dig = int2str(n1);
	s1_dig = s1_dig[s1_dig.length()-1];
	
	s2_dig = int2str(n2);
	s2_dig = s2_dig[s2_dig.length()-1];
	
	s3_dig = int2str(str2int(s1_dig)*str2int(s2_dig));
	s3_dig = s3_dig[s3_dig.length()-1];
	
	cout << s3_dig << endl;
	
	return 0;
}

int str2int(string s)
{
	stringstream ss;
	int i;
	ss << s;
	ss >> i;
	return i;
}

string int2str(int i)
{
	stringstream ss;
	string s;
	ss << i;
	ss >> s;
	return s;
}

NOTE: It might be useful to put the code in the main into an apart function (e.g: f_lastdigit(int n1, int n2) )

tux4life 2,072 Postaholic

He mentions needing to use a for loop, not recursion. At least that is how I took it.

Taking user input is easily done using a Scanner.

Yup, that's right !

tux4life 2,072 Postaholic

Could you please post your code ?