mrnutty 761 Senior Poster

You can't really do that, but there might always be some hack. How about you create
the most general lookup tables and work with that? Maybe something like readMemoryFunctionTable, writeMemoryFunctionTable,...

mrnutty 761 Senior Poster

Your problem is that those function do not belong in you abstract class. It seems like you are not abstracting enough, if you want only
some function of the base class to be implemented. Whats wrong with simply letting the child develop the necessary functions? You might
not necessarily need polymorphism depending on your problem. If you want state your problem and we'll be glad to help.

mrnutty 761 Senior Poster

@OP: your compare arguments has to be of type myStruct. Here is an example :

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

struct Foo{
	string name;
	int age;
	Foo(){}
	Foo(const string& n, int a)
		: name(n), age(a){}
	void print()const{
		cout << "("<<name << "," << age << ")\n";
	}
};

bool compareNameFirst(const Foo& lhs, const Foo& rhs){
	if(lhs.name != rhs.name) return lhs.name < rhs.name;
	else return lhs.age < rhs.age;
}
int main(){       
	list<Foo> student;
	student.push_back(Foo("Alex",19));
	student.push_back(Foo("Maria",20));
	student.push_back(Foo("muhamed",20));
	student.push_back(Foo("Jeniffer",20));
	student.push_back(Foo("Alex",20));
	student.push_back(Foo("Maria",21));

	student.sort(compareNameFirst);
	
	list<Foo>::const_iterator itr = student.begin();
	while(itr != student.end()){
		itr->print();
		++itr;
	}
  return 0;
}
mrnutty 761 Senior Poster

@OP: outfile closes when its destructor is called, and main returns a int implicitly.
So all that means is you don't need(according to your program) the outfile.close()
and return(0)] statement. Also as mentioned already, you need to seed the rand(),
to see rand, just add srand(time(0)) before ofstream outfile.... And you
also need to add #include<ctime> header.

mrnutty 761 Senior Poster

>>Is there any way to only allow insertion into string streams

If you want to only insert into streams, then use ostringstream. Similarly,
if you want only extraction into the stream, then use istringstream. If you
want both extraction and insertion into the stream then use stringstream.

But if you really want to use stringstream, then you can tell the constructor that it
should only be use to insert into the stream like so :

stringstream stream(std::ios_base::in); //only allow input into the stream.

See here for more details.

mrnutty 761 Senior Poster

Hey Jackk, I don't know if you been drinking Jack or something, but you need to watch
your manners. This isn't your house, so you don't get to shout or demand.

mrnutty 761 Senior Poster

That's nothing a quick google can't help you with. But since you asked here it is :

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

int main(){
 srand(time(0));
 for(int i = 0; i != 10; ++i){ 
   cout << rand() << endl;
 }
}
mrnutty 761 Senior Poster

Yes I did, and I showed you what you should have done. What you did made no sense.
Every animal has a name,weight, and age. Thus the animal class should contain all.
Instead of defining a operator<< for all three, you only need to define one operator<<.

So you see your structures does not really make sense. But to answer your question,
yes that is one way you can and did achieve what you wanted.

mrnutty 761 Senior Poster

What you are doing is hard coding each stream insertion operator. This is what you want :

struct Animal{
 int weight, age;
 string name;
 Animal(string name = "", int weight = 0, int age = 0)
 : name(name), weight(weight), age(age){}
 ~Animal()=0{}
};
struct Mammal : Animal{
};
struct Dog  : Mammal{
};
std::ostream& operator>>(std::ostream& outStream, const Animal& animal){
 outStream << "Name : " <<  animal.name << "\n";
 outStream << "Weight : " << animal.weight << "\n";
 return outStream << "Age : " << animal.age << "\n";
}
int main(){
 Dog dog("Rudolph",150,7);
 cout << dog << endl;
}
mrnutty 761 Senior Poster

You don't need to use the ignore function, you can just do something like this :

const int SPACE = ' ';
//peek the next character to see if its a space, if so then discard it
if(cin.peek() == SPACE) cin.get(); //opt #1

char ch = 0;
cin.get(ch); //Opt # 2
if(ch != SPACE){ /*do something */ }

//opt #3
string term;
cin >> term; //read in a term and skip white spaces
prase(term); 

//Hint on opt # 4
stringstream ss("3x^6 + 2x^2 + 9");
string str;
while(ss >> str){ 
 cout << str << endl; //prints each term
}
mrnutty 761 Senior Poster

First this code will show you what type null is :

cout << "NULL type is " << typeid(NULL).name() << endl;
cout << "NULL value is " << NULL << endl;

This is the output you will probably get :

NULL type is int
NULL value is 0

Second this code :

if(strlen(str)==0)

checks if the length of the string str is 0. The function strlen returns the
length of the null terminated string passed to it.

And in code str[]="". The length of str is 0. It contains only the null-terminated character, which does not count of the actual length of str.

And last of all, avoid all of these complication by using std::string. Unless you
are doing this as practice or something.

mrnutty 761 Senior Poster

There is another swap function/. It swaps the two variables. So now its up to you to use that to swap variables around in your vector.

mrnutty 761 Senior Poster

I think you are getting confused. Think of the Nested class as a class of its own. The
only difference is its scope. You can only access it from the outer class. The nested
class has no association with the parent class, besides the scope resolution.

What you are doing is creating a instance of Nested class in the Wrapper class.
Therefore every different instance of wrapper will have different instance of Nested.

mrnutty 761 Senior Poster

First :

template<class P>
P add(P a, P b){
  P result;
  result = a + b;
  return result;
}

is simply :

template<class P>
P add(P a, P b){
  return a + b;
}

second, what are you expecting when adding chars? Do you expect M + U to equal "MU"?
If so then consider using string as a type instead of char.

@sundip: not a good advice, since now instead of adding chars, he will be trying to
add char pointers, literally! and not its values.
So for this to work, he has to specialize his add function. So rather than complicate things, just use std::string.

mrnutty 761 Senior Poster

If you are trying to erase all contents from the beginning of input until the index
j, then do this:

input.erase(input.begin(), input.begin() + j)
mrnutty 761 Senior Poster

Do this instead :

if(input[i] == "@") //notice the double quotes
{
 //...
}
mrnutty 761 Senior Poster

Take another look at this: input.at[j].

.at is a function and thus to call a function you need to use the parenthesis, "()" ,
and not the square brakets, "[]".

Also do you really need to use .a()? You aren't even handling if it throws an error.
Just use input[j] instead.

mrnutty 761 Senior Poster

Why would you want to do that when you can simply use the relational operators :

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

int main(){		
	string s = "100";
	string n = "200";
	
	cout << boolalpha;
	cout << "s == n : " << (s == n) << endl;
	cout << "s <= n : " << (s <= n) << endl;
	cout << "s >= n : " << (s >= n) << endl;
	cout << "s != n : " << (s != n) << endl;
}
mrnutty 761 Senior Poster

>>The only question I have is if this is performance-wise the best solution? Are there solutions that are faster performance-wise?

You don't need to worry about performance at this stage. Your bottleneck is likely to
be somewhere else. I wouldn't see why this is bad. Your not casting things. You are
simply telling the compiler that you don't want to hide the function of the base
class.

mrnutty 761 Senior Poster

You want the using declarative :

#include <iostream>

using namespace std;

struct B{
	void p(){ cout << "Base p()\n";}
};
struct D: B{
	using B::p;
	void p(int i){ cout << "Derived p(" << i  << ")\n";}
};

int main(){	
	D d;
	d.p();
	d.p(1);
}
mrnutty 761 Senior Poster

I'm not sure what you are asking for. But Do you want to call Base::computeFunction() inside of Derived::computeFunction()

mrnutty 761 Senior Poster

Instead of while(!sourceFile.eof()) , do this while(getline(sourceFile,extract_line)){ }

Now the extract_line should contain the whole line.

mrnutty 761 Senior Poster

I hope you know that there are libraries already made for this. If you are doing
this just for practice then go ahead.

To get the whole string and not be limited by cin, use getline(cin,stringVariable) . This puts the whole line into the stringVariable. Work from there.

mrnutty 761 Senior Poster

When you print the whole array, output the row sum value. So in your code it would be like this :

for(int i = 0; i != 5; ++i){
	  for(int j = 0; j != 6; ++j){
		  cout.width(10);
		  cout << array[i][j] << " ";
	  }

          cout.width(10);
          cout << colSumRow[i];

	  cout << endl;
  }
mrnutty 761 Senior Poster

You almost got it, all you had to do was print the result correctly and call the function correctly :

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void call(int array[][6], const int i){
  int colRowSum[6] = {0};

  //sum to column row
  for(int i = 0; i < 5; i++){
     for( int j = 0; j < 6; j++){
        colRowSum[j] += array[i][j];
      }      
  }

  //print array
  for(int i = 0; i != 5; ++i){
	  for(int j = 0; j != 6; ++j){
		  cout.width(10);
		  cout << array[i][j] << " ";
	  }
	  cout << endl;
  }

  cout << "-----------------------------------";
  cout << "-----------------------------------\n";

  //print result
  for(int n = 0; n != 6; ++n){
	  cout.width(10);
	  cout << colRowSum[n] << " ";
  }

  cout << endl;
}

int main(){
    int array[ 5 ][ 6 ] = { { 100,   567,   434,   100,   269,   324 },
                            { 100,   458,   562,   564,   305,   245 },
                            { 100,   427,   561,   591,   595,   542 },
                            { 100,   536,   491,   204,   502,   253 },
                            { 100,   482,   521,   316,   318,   495 } };

	call (array,5);

	return 0;
 
}
mrnutty 761 Senior Poster

>>adding rows and columns of a multi array

For a second I was happy because I though you were using boost::multi_array, but oh well.

As for a solution to your problem, I suggest you create an array that holds the
sum of each column. Here is some code hints :

void foo(int array[][COL], const int ROW){
  int colRowSum[COL] = {0};

  foreach(row in array){
     foreach( col in array){
        colRowSum[col] += array[row][col]
      }
  }

  printArray2D( array );
  printArray1D( colRowSum );
}
mrnutty 761 Senior Poster

>>. A search will yield a stringstream page, but doesn't give the header name.

What you do then is click on the constructor of the object( in that page ), and it will show you
a full code, using the object at interest.

mrnutty 761 Senior Poster

When I said, the value of maxPtr, that means *maxPtr . Use the deference operator to get the value of maxPtr.

If you really want to use pointers, then go ahead and do this.

float *current = temps;
const float *end = temps + MAXTEMPS;
float *maxPrt = current, *minPtr = current;

while( current++ != end ){
 if(*current < *minPtr) { /* do stuff */ }
 else if(*current > *maxPtr) { /* do stuff */ }
}

IMO, a while loop looks better than a for loop for these kinds of things.

mrnutty 761 Senior Poster

In your last for loop, use an if statement to check if the element in the array is
higher or lower than the current highest and the current lowest. Here is a psuedo-code:

set minPtr to temps;
set maxPtr to temps;

for i = 1 to MAXTEMPS{
 if temps[i] is greater than the value of maxPtr, set maxPtr to temps + i;
 else if temps[i] is lower than the value of minPtr, set minPtr to temps + i;
}
mrnutty 761 Senior Poster

It will be easier for you if you read that from a file. If not then you can just do this :

std::vector<string> listOfNum;
listOfNum.push_back( "37107287533902102798797998220837590246510135740250" );
listOfNum.push_back( "46376937677490009712648124896970078050417018260538" );
//...and so on
mrnutty 761 Senior Poster

How are your list of large number stored? Presumably its stored in a .txt file
in a specific format. Can you give an example ?

mrnutty 761 Senior Poster

Instead of creating 2 function, maximum and second maximum, just create 1 function
called sortArray. Then you can just get the maximum at element 0 or the last element,
depending if you sort it descending or ascending. You can get the
second,third,fourth... or whatever largest easy as well.

mrnutty 761 Senior Poster

First off thats not polymorphism. Second you need a forward decleration.

struct B;

struct A{
 A a;
 B b;
};

struct B{
 A a;
 B b;
};

Second, why are you using pointers? In C++ you use reference.

mrnutty 761 Senior Poster

You need to use wide char. Try this :

wchar_t *msg = L"Hello world";
wchar_t *title = L"Greeting";
MessageBox(hWnd,msg,title,MB_OK);
mrnutty 761 Senior Poster

Also remember that cosine and sinus function takes in radians and not degrees. So this

circle.x = r*cos(i) - h;
circle.y = r*sin(i) + k;

should be this :

float rad = 3.14f/180.0f * i;
circle.x = r*cos(rad) - h;
circle.y = r*sin(rad) + k;
mrnutty 761 Senior Poster

Make your getName function a const corrected function like so :

string getName()const; //the const says that it will not change any member variable, presumably

//.cpp
string getName()const{
 return name;
}
mrnutty 761 Senior Poster

>>i want it to end after a character is pressed.

Use this :

vector<int> vec;
int n = 0;
//while user inputs a number, save it
while(cin >> n){ vec.push_back(n); }
 //now reset the stream
 cin.clear();
 cin.ignore(numeric_limits<streamsize>::max(),'\n'); 

 //now carry on with your code
mrnutty 761 Senior Poster

>>I was wondering if it's possible to add for example the struct below into a binary tree:

Yes it is. All you have to do is create a relational operator that compares hours as
primary and minutes as secondary. Come to think of it. A priority_queue is the
correct data structure for this problem. You can use the time as the priority_queue.
Here is an example.

#include <iostream>
#include <queue>

using namespace std;

struct Action{
	int hours;
	int minutes;
	Action(): hours(),minutes(){}
	Action(int h, int m) : hours(h), minutes(m) {}
	virtual void perform()const{
		/* perform some action */
	}
};

bool operator <(const Action& lhs, const Action& rhs){
	if( lhs.hours < rhs.hours ) return false;
	else if(lhs.hours > rhs.hours) return true;
	else return lhs.minutes < rhs.minutes;
}
void print(const Action& a){
	cout << a.hours << ":" << a.minutes << endl;
}
int main(){
	std::priority_queue<Action> actions;
	actions.push( Action(1,15) );
	actions.push( Action(0,10) );
	actions.push( Action(0,15) );
	actions.push( Action(1,20) );

	while(!actions.empty()){
		print(actions.top() );
		actions.pop();
	}
}
mrnutty 761 Senior Poster

>>Yes gcd CAN be zero if say numerator and denominator are 1.

gcd(1/1) = 1;

Why would anyone start counting from 0 and up, instead of 1 and up? Maybe its getting
too late. I'm going to sleep.

mrnutty 761 Senior Poster

I don't see why gcd would ever be 0. Its minimum should be 1, because 1 can divide into
all natural numbers evenly. Also look at the for loop condition, gcd can never reach 0.

mrnutty 761 Senior Poster

You need to pass the array as you suggested. Its the same as passing an array of ints, or whatever, its just a different type. For example :

int search(Inventory* items, int size){
 /*code to search */
}
void print(Iventory* items, int size){
  /* code to print */
}

So essentially, you need to pass in the inventory.


As for your bonus question, you can use std::vector<Inventory> items;
Google on how to use std::vector.

rObOtcOmpute commented: Nice +1
mrnutty 761 Senior Poster

:cool: hi Friends,.,. !!
i want to know a perfect time consumption for a given particular program..
IN MILLISECONDs

I know the way of using following method:

start=clock();

/*...
  ...     my programming logic . . .
  ...   */

end=clock();

difference=(end-start);

But it gives time in seconds , which is not useful for me in some programs,..

Q.1
Can anyone help me to measure time in MILISECONDS ?:idea:

Q.2
Is there any TOOL which can give me perfect time consumptions ??

Please help me friends . . have a great day !

clock() gives time in milliseconds, not seconds. So you already got what you
need right there.

mrnutty 761 Senior Poster

There is no way in C++, but you can do something like this :

void printN(char c, int n){
	while(n--) cout << c;
}
int main(){
 int a = 1, b = 2;
 const int ADJUSTMENT = 30;
 printN(' ',ADJUSTMENT);
 cout<<a<<" "<<"+ x = "<<b<<endl;
}
mrnutty 761 Senior Poster

This sounds like an h.w or an exercise, so doing this :

std::sort( name, name + SIZE );
int pos = std::binary_search(name, name +SIZE, "JOSH");

would not be beneficial.

And as pointer out, the string class overloads the relational operators, so that means all you have to do is change the type from int to string.

mrnutty 761 Senior Poster
char ch = 0;
//read file character by character
while(file && file.get(ch) ){
 cout << ch;
}
mrnutty 761 Senior Poster

A couple result from googling,
1)uno
2)duos.

mrnutty 761 Senior Poster

can't to work with this, because user input is string type name (:

string stringName = "goshy";
char charName = "goshy";

cout << stringName[0] << " " << stringName[1] << endl;
cout << charName[0] << " " << charName[1] << endl;

strings are made to replicate char arrays but be better

kings_mitra commented: Are you sure this is a C syntax !!! +0
mrnutty 761 Senior Poster

some hints :

char  ch1 = 'a';
char ch2 = '#';
cout << isalpha( ch1 ) << endl; //true
cout << isalpha( ch2 ) << endl; //false
mrnutty 761 Senior Poster

Thanks for the suggestion to change the file in notepad. I understand how that would work. Unfortunately, I am working on this as part of an assignment for a class and have to deal with the file as is.

Try something like this then :

string data = "01/01/2010,37.58";
	string::size_type pos = data.find_first_of(',');
	string date = data.substr(0,pos); //contains "01/01/2010"
	string strPrice = data.substr(pos + 1); // contains "37.58"
	float price = atof( strPrice.c_str() ); //contains 37.58
mrnutty 761 Senior Poster

1) Easiest way, copy ALL of the data into notepad
2) On the notepad, click Edit->replace. Now replace all ',' with ' '
3) Now you can read in the date as a string, and the price as a double