kes166 37 Practically a Master Poster

Kes and fbody - Oh my, thanks alot..I have a working code, tested. made the changes to line 15 and 58 of my original code and outputs correctly. This is my working code for all that its worth..

#include<iostream>
#include<string>
using namespace std;

void sortnames(string [], int);

int main()

{

	int num_of_students;
	int x;
	
	string name [25];

	cout << "How many student" << endl;
	cin >> num_of_students;
	if (num_of_students != -1)
	{
	while (((num_of_students < 1)||(num_of_students > 25)) && (num_of_students != -1))
	      {
			cout<<"Invalid entry"<<endl;
			cout<<"Please enter number greater than 0 and less than 26"<<endl;
			cin>>num_of_students;		
	       }
	    
		   for (x=1; x <= num_of_students; x++)	
	         {
	           cout << "Enter student's first name" <<endl;
               cin >> name[x];
	         }
	       
	       sortnames(name, num_of_students);  
		cout << "Line formation list of names are: " <<endl;
		for (x=1; x <= num_of_students; x++)	
	         {
	           cout << name[x] <<endl; 
	         }
	         	
	}
	else
	{
		cout << "Terminating......."<<endl;
		system("PAUSE");
		return 0;
	}
	system("PAUSE");
	return 0;
}
	
void sortnames(string name[], int num)
{
	int i,a, min;
	string strName;
	for (i = 1; i <= num; i++)
	{
		min = i;
		strName = name[i];
		for (a = i + 1; a <= num; a++)
		{
			if(name[a] < strName)
			{
				strName = name[a];
				min = a;
			}
		}
		name[min] = name[i];
		name[i] = strName;
	}
}

Many thanks again - and excuse me for asking basic/silly questions, just learning and there is alot to process.

It looks like you are still not using name[0], and you declared string name[25] which will create locations for name[0], name[1], ... …

kes166 37 Practically a Master Poster

The way someFunction() is declared should be okay because it is a member function. As a member function, it has access to the private members of both objects. Something like this would be fine:

void SomeObject::someFunction(SomeObject & other)
{
  if ((this->b) > (other.b))
    cout << "This b is greater than other b" << endl;
  else
    cout << "Other b is greater than this b" << endl;
}

int main()
{		
  SomeObject s1(1,2), s2(3,4);
  s1.someFunction(s2);

return 0;
}

@OP:
NOTE, this code is very similar to yours, but it will not work within your program as written. IT IS ONLY AN EXAMPLE.

Would something like this work to return the two values?

:

void SomeObject::someFunction(SomeObject & other, int *firstb, int *secondb)
{
  if ((this->b) > (other.b))
    cout << "This b is greater than other b" << endl;
  else
    cout << "Other b is greater than this b" << endl;
  *firstb = this ->b;
  *secondb = other.b;
}

int main()
{		
  int *valueA, *valueB;
  SomeObject s1(1,2), s2(3,4);
  s1.someFunction(s2, valueA, valueB);

return 0;
}

I'm not sure if the syntax is correct, but I know I've used the concept of passing pointers back to main before. valueA should point to the value in s1.b and valueB should point to s2.b

kes166 37 Practically a Master Poster

Edited because I was wrong:
You can call someFunction by s1.someFunction(s2) and have it return whichever value you want, but you will still need to make it a void function if you want both values returned. It would be easier to make a seperate function to access s1's b vaule.

Depending on what you want the function to do, return the highest, lowest, or whatever, you'd just return whichever one you wanted. If you wanted to return both, you would need to use pointers in the function and make it void.

You also need a constructor as already stated.

bmos31 commented: s1.someFunction(s2) is exactly what I was looking for..Thanks! +1
kes166 37 Practically a Master Poster

You have a logical error in your sort. In your main, you leave name[0] unused but in your second for loop, you don't take that into account on your variable num.

void sortnames(string name[], int num){
	int i,a, min;
	string strName;
	for (i = 1; i <= num; i++)	{
		min = i;
		strName = name[i];
		for (a = i + 1; a < num; a++){
			if(name[a] < strName){
				strName = name[a];
				min = a;
			}
		}
		name[min] = name[i];
		name[i] = strName;
	}
}

Lets assume:
name[1] = Mike
name[2] = Paul
name[3] = John
num = 3 (num_of_students from main)

on the first pass through:
i = 1
min = 1
name = Mike
strName = Mike
a = i + 1 = 2

The if statement reads as if (name(a) < strName) which is Paul < Mike, it's not, so the for loop reiterates.

Now a increments by 1, and becomes 3, then cancels out of the inner loop because a < num is now false, and returns to the outer loop and poor John is left hanging at the end. You can fix that by adding to the inner loop

for (a = i + 1; a <= num; a++){

an equal sign. That should fix the issue.

kes166 37 Practically a Master Poster

I may be missing something here given the antihistamine I took an hour ago, but since I've never hesitated to sound foolish in the past I shan't be bashful at this point either.

What size is a list and how would the compiler know how much memory to sequester for an array of lists? List objects should have their own addresses (distinct from the address of the frist node of the list, etc) so an array of pointers to lists seems more intellectually satisfying as I'm thinking about it, though I admit to never having written such a structrue to know whether an array of lists works or not. (Could the name of a user defined object degenerate to the address of the object similar to the name of an array degenerating to the address of the first element of the array eventhough that doesn't happen for the names of native data types?)

That's basically what it is, an array of pointers. But studentTable[0] will always point to the first node in the list. To navigate that, or any of the other 9 lists in the array, you would have to create a temp node and go through all the next pointers.

The space needed is on a by node basis. As a new node is needed, the space is created.

studentTable[0] points to the first node in it's list so studentTable[0].data will reference that specific node.
studentTable[1] points to the first node in it's list so …

kes166 37 Practically a Master Poster

If

cout << songname << endl;

works as intended and prints the song names on each individual line, then the send function is missing the endl. Is send in a library or a function you created? I'm not familiar with it if it's from a library.

kes166 37 Practically a Master Poster

I always thought reading as a string, then type casting to something else was always a hassle because of coding the error catching. In what way does it give more control?

kes166 37 Practically a Master Poster

The value is returned to the location where the function call came from

total(num1,num2);

However, you didn't assign it to a variable. The return statement returns the value sum to the function call.

You can do it two ways, either set the returned value to a variable declared in the block of code where you used it, or print the value directly.

foo = total(num1,num2); //foo holds sum

or

cout << "The value is: " << total(num1, num2);

Either way should work.

JDean89 commented: Thanks alot +0
kes166 37 Practically a Master Poster

You need to include the switch statement in your do while loop otherwise, you will never exit the do while loop.

Also, with your switch statement, everything in your default will never run.

//None of this code will ever run. If you enter a number to break your do while loop, none of your other cases will run. Your default doesn't have a break in it.
default:		
         system("cls");
	cout<<endl;
	cout<<"You have ordered the following:"<<endl;
	cout<<"___________________"<<choice<<endl;
	if(beefQuant!=0){
		cout<<beefQuant<<"  #1      "<<beefTotal<<endl;
		}
	if(pkbfQuant!=0){
		cout<<pkbfQuant<<"  #2      "<<pkbfTotal<<endl;
		}
	if(veggQuant!=0){
		cout<<veggQuant<<"  #3      "<<veggTotal<<endl;
		}
	if(gdpgQuant!=0){
		cout<<gdpgQuant<<"  #4      "<<gdpgTotal<<endl;
		}
	if(drnkQuant!=0){
		cout<<drnkQuant<<"  #5      "<<drnkTotal<<endl;
		}
	cout<<fixed<<setprecision(2)<<endl;
	cout<<"___________________"<<endl;
	subTotal=(beefTotal+pkbfTotal+veggTotal+gdpgTotal+drnkTotal);
	tax=(subTotal*.12);
// this program is set to the current tax rate implemented by the Philippine Government any changes must also be updated if any
	totalDue=(subTotal+tax);
	cout<<"Subtotal:  "<<subTotal<<endl;
	cout<<"Tax:  "<<tax<<endl;
	cout<<"___________________"<<endl;
	cout<<"Total Due   "<<totalDue<<endl;
kes166 37 Practically a Master Poster

Yes, you can make an array of any defined data type, whether built-in or user-defined. But you can't create, for example, and array of integers and store more than one integer in an element, if you attempt to do so, you'll simply overwrite the element with the new integer value. Hence the reason I said it's not directly possible.

Did you even read the rest of the post?

Like I said, I didn't really understand what he's looking for.

After rereading it, it kind of sounds like he's looking for an array of linked lists. I'm not 100% sure on syntax and I have no compiler to test it, so I won't bother trying to write it as I would probably just butcher it.

kes166 37 Practically a Master Poster

Your intent is a little confusing, it sounds like you want to add multiple items to the same element of an array. Which is not directly possible.

Actually, I think if you create a structure, and then create an array of that structure, you can do what the op is suggesting. It's not a linked list though.

struct foo{
int myFirstData;
int mySecondData;
};

int main(){
foo myArray[10];
myArray[0].myFirstData = 1;
myArray[0].mySecondData = 2;
}

Although, I'm not 100% sure of what the Op is asking.

Edit:

I think you can make an array of classes also.

kes166 37 Practically a Master Poster

yes, i need to declare a new student and add it to the list.
heres what i have so far, but i get an error.

void add(string student, string university, int id)
       {
           //create a new node
           node * somenode;
           somenode = new node;
           
           //put student in the new node!
           somenode->data = (student, university, id)
          
              
           
           //put new node at front of list!
           somenode->next = start;
           start = somenode;
       }

no no ... you need to break that up ...

somenode->data = student;
somenode->university = university;
somenode->id = id;

Edit:

I also recommend you don't use university or id in the function call. I usually found it to be bad programming practice to use class variables and function variables confusing if they were using the same name.

kes166 37 Practically a Master Poster

One other thing I noticed is in your class studentnode

class studentnode{
public:
       string data;
       studentnode * next;
 	   // string to hold student name
       string student; };

You don't use student to hold the students name. You use data. You can remove string student; from the program, and it should still function.

kes166 37 Practically a Master Poster

i think i see where your going with this.
i have everything in one file. im using devc++ so i dont think i can make a .h file.
here is my entire code as i have it so far:

#include <iostream>
#include <string>


using namespace std;


//new class for studentnode
class studentnode
{
public:
       string data;
       studentnode * next;  
	   

	   // string to hold student name
	   string student;

};


//new class for studentlist
class studentlist
{
private:      
      studentnode * start;
      
      
public:
       studentlist()
       {
           start = NULL;            
       }
       
   
       
       // function to add a new student to list
       void add(string student)
       {
           //create a new node
           studentnode * somenode;
           somenode = new studentnode;
           
           //put student in the new node!
           somenode->data = student;     
           
           //put new node at front of list!
           somenode->next = start;
           start = somenode;
       }
       

       //function to a specified student, if not traverse the list until found.
void getStudent (string student)
{ 
     studentnode* current;
     current = start; 
     while(current!=NULL)
     {   
         if(current->data == student)   
         {    
               cout << "The Specified Student Has Been Found: "<< endl;
               cout << student <<endl;  
                 break;  
                 }  
                  current = current->next;  
                  }
                  }

       

       //display all the items in the list
       void print()
       {
            studentnode * current;
            current = start;
            
           //loop through all nodes, each time doing something
           while(current != NULL )
           {
               //display the data of the current node
               cout << current->data << endl;
               
               //move current to next node
               current = current->next;               
           }     
       }
};







//main program thats ran.
int main()
{
    cout <<"This linked list should display students …
kes166 37 Practically a Master Poster

Ok, I'm a bit out of practice, so if I'm incorrect in my information, it would be helpful if someone corrected me here.

It looks like you need to add a new node onto the linked list with the student name, the university, and the id. That is what you will need to pass to the fuction of the linked list class.

void add(string student, sting university, int id){
...
}

Once you have the information passed to the function, create the node, add the information into the node, point the previous nodes next pointer to the new node, and point the current node's point to EOF, null, or whatever.

Edit:

What does your .h file look like for this class?

kes166 37 Practically a Master Poster

Alt + F3?

http://msdn.microsoft.com/en-us/library/0zz35x06(VS.80).aspx

Or maybe Ctrl+K, Ctrl+L?

http://www.codeproject.com/KB/tips/VSnetIDETipsAndTricks.aspx

That's all I could find.

kes166 37 Practically a Master Poster

What would happen if index <= 1 ?

cout << "Please, choose the speaker's index that you'd like to add info to, 1-10: ";
cin  >> index;

You'd need a while loop to catch if it were less than one or greater than 10. If it were one, index - 1 would be 0 which would reference the correct location in the array. If index was -2 then the while loop would catch it and ask for a new input.

Edit:

Similar to this code

while (choice < 1 || choice > 4){
cout << "Please, enter a valid choice number, 1-4: ";
cin  >> choice;
kes166 37 Practically a Master Poster

Can't you just get rid of all the if statements, and use

cin >> sp[index - 1].name >> endl;
//etc ...

And then the same concept for the viewSpeaker?

kes166 37 Practically a Master Poster

I am fairly new to c++ and have been working on this problem and asked the professor for help, but he has really been non-compliant. I am lost on this and keep gettin error messages that I do not understand how to fix. Any help that could be given would be absolutely great, thanks!

Copy the following code exactly as shown. Starting with the following code base, add code in the area shown to create a function called printAllFactors. Do not change any of the other code anywhere else in the program! Write only one function (not two). You must write your own function – do not use a library function. If you break any of these rules you will get a zero for this question! Remember that a factor is any number that divides into another number without any remainders. For example, the factors of 8 are: 4 2 1. Use the screen shots below as a guide.
#include <iostream>
using namespace std;
// Do NOT change anything ABOVE HERE
// WRITE THE FUNCTION BETWEEN HERE
// AND HERE
// Do NOT change anything BELOW HERE
int main()
{
int i1, i2;
cout << "Enter an integer: ";
cin >> i1;
printAllFactors(i1);
cout << "Enter another integer: ";
cin >> i2;
printAllFactors(i2);
}

#include <iostream>

using namespace std;

class dic(object)
{
	def printAllFactors(self,x)
		while (x)
		{
		if (x %2 == 0)
			{
				print …
kes166 37 Practically a Master Poster

Guys, I got an error with the following code:

struct myStruct {
           int myVar1;
      };

      class A { //definition of A
         ...
      };
struct myStruct {
           int myVar1;
      };

      class B { //definition of B
         ...
      };
struct myStruct {
           int myVar1;
      };

      class C { //definition of C
         ...
      };

You are declaring myStruct multiple times in seperate files. You need to put myStruct in a header (explained earlier) or format the code together

struct myStruct{
     //foo
};

Class A {
     //foo
};

Class B {
     //foo
};

Class C {
     //foo
};

Are you using myStruct from main or only in the classes? It's been awhile since I actually coded, but I think you can declare the struct as private and keep the classes seperate in different files as well. I'm not 100% certain of the syntax for that though.