mrnutty 761 Senior Poster
recordsList.sort(compareForSort);

Try this :

recordsList.sort(&Standings::compareForSort);
mrnutty 761 Senior Poster

++
Me too. Not only is it unnecessary, the code just reads a whole lot cleaner without them.

if (isComplete) {...

Yea, sometimes, but IMO not all the times. It depends on the variable
name. When giving a variable name, if giving it isBlah doesn't
necessarily make sense, for example, suppose you have a state flag
the in that case I would rather do this :

if(state == false)

than

if(!state)

I am not sure which one would be faster, but I guess it depends on
many things, but I assume it would be the same in a decent
compiler.

mrnutty 761 Senior Poster

You cannot use std::sort with list (if that is what you intended to do)

list has its own sort. Here is an example : http://www.cplusplus.com/reference/stl/list/sort/

mrnutty 761 Senior Poster

I am a little doubtful about your code. Why does CarNode inherit from
Car and it also composes it ?

Note that in c++ objects is by default passed by value and Not passed by
reference. You may wan't to read about pointers and reference in c++.

mrnutty 761 Senior Poster

Your copy ctor is hiding the member variable. And even worse you have
a memory leak.

mrnutty 761 Senior Poster

Then you should take the input as a string. Then if the size of the string is
greater than 1 then display an error message, else compare it with a or b.
Doing this way you would have to use if/else. This isn't that bad if you
don't have many cases.

mrnutty 761 Senior Poster

Let me try to explain further of the algorithm below :

An outline of the above algorithm would look something like:

// positive integer I is given
P = 1 //determines when a even factor of I is found
while (I > 1) //I is you input number
{
P = P + 1// p is now 2
if I mod P != 0 go back to previous statement //if I mod p == 1 then increment P until a factor is found

// now P is the smallest prime factor of I //now you found a factor for I
display P //show the factor 
mult = 1 
exactly divide I by P as often as is possible, //keep diving I by the even factor until you get a decimal division
assigning to I the result of each exact division, //now you need to find another factor of that number, so start repeating from the start

and incrementing mult with each exact division
display mult, end line
}
mrnutty 761 Senior Poster

You need to use the mod operator.

example :

2 % 2 = 0
4 % 2 = 0
12 % 2 = 0
114 % 2 = 0

as you can see even numbers mod 2 will equal 0. So use this as your
test case in your for loop. and if true then print that number else do nothing.

mrnutty 761 Senior Poster

Like super-sonic said, you need to pass your variables.

This code :

double distance()

{

double center1, center2, point1, point2;

double distance1, distance2, distance3;

 

if (point1 >= center1 && point2 >= center2 )

distance1 = (point1 - center1);

distance2 = (point2 - center2);

distance3 = pow((distance1 + distance2), (1/2));

cout << "Distance Between Points: " << distance3;

else

cout << "Points Will Produce an Unreal Number";

cout <<endl;

 

return distance3;

}

You DECLARE point1, point 2, center 1, center2 inside your function, so that
variable will be used, instead of the one you use in main. What you should
do is make your function take in parameters like super-sonic showed, then you can call it in main like so :

cout << "Distance between points: " << distance(point1,point2mcenter1,center2);
mrnutty 761 Senior Poster

>>
I'm trying to eliminate the highest and lowest number out of 5 inputs then get the average of the remaining 3 inputs...
In some instances, it gives me incorrect average..
<<

First find the highest/lowest integer.

When getting input, make a variable that checks for high and low numbers.

you can do something like ths :

int num1, num2, num3, num4, num5;
int max(0),min(0);
cin >> num1;
max = min = num1;
//then get other inputs while comparing the value

Then post back when you get that done.

mrnutty 761 Senior Poster

>> result = ((x % 10) + ((x / 10) % 10) + ((x / 100) % 10) + ((x / 1000) % 10) + ((x / 10000) % 10));

You know your formula won't handle number above 99999, try
doing this with for loops or recursions.

mrnutty 761 Senior Poster

Yea, to show that initializing variable should become a habit for it
will save bugs from appearing when working on bigger projects.

mrnutty 761 Senior Poster

>>last 15 years
Wow, I have just programming for about 2 yrs on/off so you have a lot more knowledge than me. Nevertheless I would still like to debate even though if I may be wrong, if that's OK with you.

>>Well, if that is the case ...<snipped>
<<

I see what you mean, let me rephrase what I meant to say, my words
weren't being clear. A reason why a person might use "!=" versus
"<" would rely mainly in its context. For example, != is more general
when iterating through a container, as not all iterator define the <
operator. For example, vector and map would work with != but not <
operator in C++. But I do not mean that != is more general than <
every time nor should it be used in cases like you have shown. To
answer the question for javaAddict, i used != because in a for loop it
might be more general at times, and just thought I would switch it up
a bit.

>>
Also, c++ calls generic types template types.
<<
Yea, but I was talking about generic in a sense of overall not just meta programming, sorry I should have been more clear, again.

mrnutty 761 Senior Poster

Use the suggestion above, although you might want to google sorting for knowledge experience.

You can do this if you want to use std::sort;

#
int main()
{
int x[5];
prompt(x);
printreverse(x);
std::sort(x,x+5);
system("pause");
return 0;
}
mrnutty 761 Senior Poster

>>(By the way, what does "if (! cin)" mean exactly? I copied yours but not sure the exact function of it. Can you put the "!" in front of any statment?)
<<

First realize that "cin" is a istream object, you might not understand what the means until you get to learn about class. Anyways, istream overloads the ! opertor so you could use it in conjunction with istream object.

So, when you do something like this :

int x = 0;
cin >> x;

Since x is a integer, and you are using cin to get a integer for an input,
then that means that cin is expecting a integer to be inputted. By if an
integer has not been inputted, cin sets off flags. These flags are just
bits. When a bit that handles fail in input is set, then that bit is set to
true and when everything is fine, that bit is false. So that means you
can use cin to check if certain bits has failed or not, and if so then whatever that bit represents( maybe failed input of end of file or
hardware failure), is set to true to indicate that something has gone
wrong. You can skip checking the bits and go straight to checking the istream object, cin using the ! operator. The ! operator when using like
this

if ( ! cin ) { ... }

is …

mrnutty 761 Senior Poster

One last question. I built it and compiled it. When i try to run the exe on another computer it doesnt want to. What gives?

what do you mean "it doesn't want to"? Does it give some errors?

mrnutty 761 Senior Poster

>>
"what guarantee do you have that you are actually comparing values in c++?"
<<

In c++ the "!=" operator when used with container from std does not compare address like the "==" in java ( at times), it only compares
the values. The standard C++ library was implemented with generic in
mind.

mrnutty 761 Senior Poster

Thanks, this is new to me, too. However, this is still not what I mean to want the program do (sorry for seeming stubborn, maybe there's no way anyway, but just in case...). To put it simply:

What I mean is to have a program which will use an integer x, whose value is entered as input (e.g. "What is your age?"), but when the user accidentally enters any non-integer (such as character "Q", float "8.72"), he will get a message on the screen "Wrong input entered", rather than having the screen just flash and then go dark. The last program suggested by you does solve part of the problem (entering character will result in an error message), but entering a float type ("53.97") will not produce the error message but instead have the screen flash and then go dark.

I wonder if there is a way to be able to deal with invalid input being not only character but also float and any other non-integer type. (If there is, it's probably complicated, seeing the one you suggested).

Then make another test case for floating point,

float temp = 0;
int val = 0;
cin >> temp;
//check for non number character error

int val = int( temp ) ; //get the whole value

if( val < temp ) cout << "bad\n"; //val should equal temp

So do the error check when temp is not a numeric value, then do
another check to see if the number entered …

mrnutty 761 Senior Poster

I have a question:
Why didn't you use this at the for loop:

for(int i = 1; i < 5; i++)

late reply but its just preference at times.

A thing to note ( at least in c++) is that != is more generic than
'<' because '!=' works with containers like vectors, string map
while '<' is not compatible with all of them. I imagine that
java is similar. It also depends on context btw.

mrnutty 761 Senior Poster

If you need only whole value integers. Then use a typecast on float and use that whole value.

float f = 12.33;
int i = int( f ); //i = 12

Then work with 12

mrnutty 761 Senior Poster

Oh right, its your conditional expression thats giving you problems :

if (mybmi < 18.5) {
}
else if (mybmi >= 18.5 && mybmi <25) {
}
else if (mybmi >= 25 && mybmi < 30){ 
}
else if (mybmi >= 30){ 
}

Basically this part of the code for example :

else if (mybmi >= 25 && mybmi < 30)

reads as : " if my body mass index is greater than 25 but less than 30 then i am over weight" for this situation.

The "&&" is called "and" operator. Google its truth table and its further uses.

mrnutty 761 Senior Poster

id has nothing to do with A[0].a. id is a variable that i pass to the function assign_struct. if id = 3 then A[0].a might be a different value, and if id=4 then A[0].a is another value and so on..

Then just do this :

void assign_struct(A test[], int id)
{
   switch(id)
   {
      case 0 : A[0].a = 12;
      case 1 : A[0].a = 2;
      case 2 : A[0].a = 1
      //so on
    }

  ///then rest of the code :
    A[0].b = new int[A[0].a];
    ...

or if you know the values before compile time

void assign_struct(A test[], int id)
{
      int A[] = {12,1,2,3,7,3,8 }; //If you know its values during compile time
  
  ///then rest of the code :
    A[0].b = new int[A[id].a]; //use id as index with its value coordinated
    ...
mrnutty 761 Senior Poster

>>Thank you so much. Now I have another problem. The only 2 messages it wants to print: the underweight and normal one.
Is it possible to do a range for these?

What do you mean? You only want the underweight and normal to be
a option?

mrnutty 761 Senior Poster

You mean the input is something like this ?

<enter fraction > 123/452

And its read as a string.

If so then separate the string into 2 components the left side before the '/' character and the right side, after the '/' character. Then use the
atoifunction .

mrnutty 761 Senior Poster

But why did this work?

a==1? cout<<"1": cout<<"0";

because thats still returning a value;

cout<<"A", mean A goes into the stream and then cout is returned .

with your :

a == 1 ? return 1 : return 0;

does not return a value for that expression so it does not work
mrnutty 761 Senior Poster

Your problem is that your are eventually doing :

cout<<cout;

which is print the object's address.

For example :

if (mybmi < 18.5) {
cout << "\nYour BMI is:" << mybmi << cout << "\tYour BMI says you are Underwight." << endl;
}

Break it up to this :

if (mybmi < 18.5) {
cout << "\nYour BMI is:" << mybmi <<endl;
cout << "\tYour BMI says you are Underwight." << endl;
}

Do this for others as well

mrnutty 761 Senior Poster

What type of converter ?

mrnutty 761 Senior Poster

First use code tags when posting code so we can read it better(its the button on the tool list that says "(code)" ).

here is your code with code tags

#include <iostream>
using namespace std;

int main(void)

{
int myweight;
int myheight;
int mynum;
mynum = 703;
int mybmi;
int squared;


cout << "Please enter your weight in pounds." << endl << endl;
cin >> myweight;

cout << "\nPlease enter your height in inches." <<endl << endl;
cin >> myheight;
cin.ignore();

squared = (myheight * myheight);
mybmi = ((myweight * mynum)/squared);


if (mybmi < 18.5) {
cout << "\nYour BMI is:" << mybmi << cout << "\tYour BMI says you are Underwight." << endl;
}
else if (mybmi >= 18.5) {
cout << "\nYour BMI is:" << mybmi << cout << "\tYour BMI says you are Normal." << endl;
}
else if (mybmi >= 25){ 
cout << "\nYour BMI is:" << mybmi << cout << "\tYour BMI says you are Overwight." << endl;
}
else if (mybmi >= 30){ 
cout << "\nYour BMI is:" << mybmi << cout << "\tYour BMI says you are Obese." << endl;
}


cin.get();


system("pause");

return 0;

}
mrnutty 761 Senior Poster

The code is on this thread: http://www.daniweb.com/forums/thread235535.html

I have two problems with the code.
first problem: I can't get the values to transfer thru the functions properly.
second problem: I can't go to the correct area of code that I want to go to.

Should I abort functions on this all together? ...they seem to create more of a problem than solve...

I don't know what you mean. Specify more. Don't assume that I
know exactly what you are talking about.

mrnutty 761 Senior Poster

Don't use goto. Period.

How about you show some code and explain what you are trying to do

mrnutty 761 Senior Poster

First tell me how id has something to do with A[0].a

I mean you have

if (id == 1)	
 { A[0].a = 2; ... }
else if(id == 2) { A[0].a = 1; ... }

How does id correlates to A[0].a?
What would A[0].a be if id is 3,4,5,6,7,8,9,10 ?

mrnutty 761 Senior Poster

Maybe this will help :

int x = 0;
cin >> x; //get input, expecting an numeric value
if(! cin ) //check if input wasn't a numeric value
{
   cin.clear(); //clear the stream
     while(cin.get() != '\n')  //discard all bad inputs up until the line end
          ;    

   cout<<"Bad\n"; //tell user the input is bad
}
else cout<<"good\n";
mrnutty 761 Senior Poster

Just because you are unable to understand the scientific process and the joy of knowledge for knowledge's sake does not make it 'bullshit'.

Have you ever seen a starving kid in places like Africa? It changes your
whole outlook in life. I bet you take even tap water for granted. You
have no idea how it feels like to be "poor". And when I say poor I mean
living way below the standard living condition for human. Sure
knowledge betters one, but it has been proven that by human nature
that in order to be a moral person, we need to help others, below us,
out. That is unless you are an immoral person, you should help them.

I am sorry that you do not have the creativity or imagination to understand that not everyone thinks the way you do or are driven by goals different from yours.

Thats too bad. This world is a piece of sh*t. You have no idea. You are just another person that feels like you know what your talking about, just because you think you are smart.

T

he scientific process contains within itself the processes necessary to change and adapt to new realities. When new data comes in and it disagrees with current theories, then then the theories are modified to include the new data (this is not done easily nor lightly and there are hard fought 'intellectual' battles over the meaning and consequences of …

mrnutty 761 Senior Poster

>> the problem is i dont know how to use the ifstream to read and use the code Morse that must be kept in .text file

Then google it or something?

Here is one from here

mrnutty 761 Senior Poster

Sorry for not replying earlier. Thank you sfuo and firstPerson. And it worked to put it like

return ( a == 1 ? true : false )

Thank you very much firstPerson! But I'm still kinda wondering why it didn't work the way I put it in the first place.

Lets look at this code again :

a == 1 ? return 1; return 0;

In between ? : operator meaning in :

? <here> : <here> ;

could only be a value, so you cannot have return statements
or anything else;

thus you need to wrap everything with the return statement
so it would work :

return ( a == 1 ? 1  : 0 );

So the inside part gets executed, and evaluates to either 1 or 0
then all you have left is either return 1 or return 0.

mrnutty 761 Senior Poster

>>If I have the class name in a string varibale, how can I instantiate an object of that class?

Your question is not very clear, but my guess would be that you meant
something like :

class Foo
{
   public Foo() { }
};

int main()
{
    string className = "Foo";
    Foo * foo; //not instantiated but waiting in a sense to be created.

    if( className == "Foo")  //a string variable has the class name FO
       foo = new Foo(); //so create a new class of that object

   //after foo is done being used with
  delete foo;
}
mrnutty 761 Senior Poster

just do this :

string input;
	string s = "$$$";

	ifstream iFile("ReadMe.txt");

	while(getline(iFile,input))
	{
		if(input == s) break;
		else cout<<input<<endl;
	}

Haven't tried it but that logic should work.

mrnutty 761 Senior Poster

>>like if I have 0000.9876 will be stored as 9876

If this is exact then, you are making it too complicated. Just have a variable that starts where the string has the '.' character then increment
it by one and then copy it.

string S = "000.123";
int stringIndx = 0;
//move index until the '.' character is found
while(S[stringIndx] != '.')
       stringIndx++;

//bypass the '.' character
stringIndx++;

//now copy from the string starting at stringIndx to its size
mrnutty 761 Senior Poster

>> How will i recognize the need of using the copy constructor ?

Usually when you have a class that uses dynamic memory then you need
to create your own copy constructor so when copying a value, each
variable has its own memory location for its member variables.

For example :

//say myString is a class that is similar to a string class which 
//uses new to dynamically allocated a char array
myString str("hello");
myString str2(str); //what happens here ?

In the second line str2 invokes the copy constructor.

Now imagine 2 cases :

case 1 : There is no copy constructor defined.
case 2 : You have created a proper copy constructor

Now for case 1 : what would happen is that str2 and str1 will both
hold a pointer to the same object that str1 uses inside its class.
So when str1 goes out of scope, str2 points to a dud memory. This
is a huge bug.

Now for case 2: str2 invokes its copy ctor and makes a deep copy of
str1. So that str2 has the same value as str1 but has its own memory space for each of its variables.

So in general create a copy constructor , regular constructor, destructor and assignment operator, when your class dynamically allocates
memory for a variable.

mrnutty 761 Senior Poster

>>i not sure what is the single quote means...

Its usually used with char variables :

char quit = 'n'; 
if( quit == 'n' ) 
   cout<<"Still playing\n";
mrnutty 761 Senior Poster

More bullshit, what does it really matter?

A new Missing Link has been found. If what they say
is true then all that I have been taught by be anthropology
teacher and alike has been bUllShit. Its all crap. What does it really
matter if we "come" from moneys or sasquatch , turtles or whatever?

I swear all this crap is making me sick. Instead of spending millions
of dollars on trying to find dried up bones that have no use, we
could be helping people like this :

:

mrnutty 761 Senior Poster

You forgot to enable GL_COLOR_MATERIAL, when using lighting. I
changed it a little, take a look at the comments.

#define GLUT_DISABLE_ATEXIT_HACK
#include <stdlib.h>
#include <GL/glut.h>


// Globals
void display();
void myInit();
//Set up material
	GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
	GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
	GLfloat mat_ambient_color[] = { 1,1,1,1 };
	GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
	GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat no_shininess[] = { 0.0 };
	GLfloat low_shininess[] = { 5.0 };
	GLfloat high_shininess[] = { 100.0 };
	GLfloat mat_emission[] = {1,1,1,1};

//Set up lighting
	GLfloat light0_pos[]={20.0, 100.0, 1.0, 0.0};
	
	GLfloat diffuse0[]={1.0,1.0, 1.0, 1.0};
	GLfloat ambient0[]={0.4, 1.0, 1.0, 1.0};
	GLfloat specular0[]={1.0, 1.0, 1.0, 1.0};

	GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
   GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
   GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
   GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
   GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
   GLfloat local_view[] = { 0.0 };
   GLfloat global_ambient[]={0,0,0,0};

void myReshape(int w, int h);
void drawSphere();
GLsizei wh=800, ww=800;

void myInit()
{
  
   glClearColor(0.7f, 0.9f, 1.0f, 1.0f);	// Set background color to sky blue.
   glEnable(GL_DEPTH_TEST);
   glShadeModel(GL_SMOOTH);

   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
   glLightfv(GL_LIGHT0, GL_POSITION, position);

   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
   glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);

   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);
   glEnable(GL_NORMALIZE);	
	//NEW
   glEnable(GL_COLOR_MATERIAL);

  glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient);
  glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
}


void display(void)
{	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

//Draw sphere
   glPushMatrix();
   glTranslatef (-4.0, 5.0, 0.0);
   glColor3f(1.0f,1.0f,0.0f); …
mrnutty 761 Senior Poster

Hello

I am not sure if my function below will completely delete a linked list. I am worried it will delete every element of the list except for the head(first node)?

For example; If I have this linked list below


Will my function delete the 1st node (1)?

void destroy(node * & L)
{

    node *temp = L;
    while(temp != NULL){
        temp = temp->next;
        delete L;
        L = temp;
    }
}

I think it will delete all nodes, lets see say node is 1->2->null , has 2 nodes

node *temp = L; //temp now is node1
    while(temp != NULL){ //while temp is != null
        temp = temp->next; //increment temp, temp is now node2
        delete L; //delete node1 
        L = temp; //head is now node2 
    }
mrnutty 761 Senior Poster

Here's my code.

int main ()
{
   int input;
   cout <<"Enter a sequence of numbers (0 to quit): ";
   cin >> x;

   if (x != 0)
   {
     if (x % 2 == 0)
      {
         cout << x << " is even" << endl;
      }
      else if (x % 2 != 0)
      {
         cout << x << " isn't even" << endl;

      }
  }
}

Your almost there :

1) change : int input; TO int x;
2) Wrap your code in a do while loop.

int x = -1;
do
{
  /* get user input and check for even/oddity */
}while(x != 0) //quits if user enters 0
Towely commented: You rule! Thank you so much! +1
mrnutty 761 Senior Poster

cant you just do this :

void xSinX(int * A, int SZ, int lim){
  for(int i = 0; i < lim && i < SZ; i++){        
   A[i] = i*sin( float(i) ); //xsin(x)
  }
}
mrnutty 761 Senior Poster

Suggestions from a college student :

Reasons on why some Professors are better than other

1) Enthusiastic about their subject.
2) Knowledgeable about their subject
3) Clear with explanation
4) Start from basic to advance
5) Be descriptive on even the small things
6) Realize that the MOST easy thing to a teacher might be a hard thing for a student.
7) Asks students if anything is unclear
8) Make a good 1st impression (see 1) )
9) Not too much theory, give examples, more the better.
10) Realize that the attention span for an average student (From what I was told by a friend that a Psychology major) is 15 minutes. So keep them interested by "changing" it up.

mrnutty 761 Senior Poster

1. Do you love what you do? why?

Yes, otherwise I probably wouldn't do it often.

2. Do you interact with other people or other engineers a lot?

I guess, I am a college student in class with other engineering students.

3. How dangerous is your job? Have you ever been seriously injured?

Very dangerous. There are times when I wan't to throw my protoboard or the computer that I am working at people/wall/Object.

4. How did you get interested in the field you work today?

Right now, "work" means something different to me that it would have
in future years, but I got interested when I took my first programming
class( C++ ).

5. How are the work hours? Do you get breaks or off days?

Work on weekdays, party on weekend, sure its college.

6. Do you feel you have accomplished what you wanted?

No, Not yet. I will feel accomplished when I have enough language
to create my own programming language that becomes revolutionary.

7. Would you suggest this field to me? Why or what field would you suggest?

Maybe, but I don't know you.

8. Where do you do most of your work, in an office, outside, etc.?

In my dorm, engineering building , and even more obviously a computer.

9. How were you able to pay for your …

mrnutty 761 Senior Poster

Hint, a odd number plus a odd number equals a even number.

mrnutty 761 Senior Poster

Maybe the quality of the game and/or the features of the game is
not supported by your video card.

mrnutty 761 Senior Poster

wanna send me the file so I can take a look at it. Make sure you
have enabled lighting and LIGHT0