mrnutty 761 Senior Poster

OK so I'm trying to convert my string to lower case. Shouldn't this get the job done:

for (int i = 0; i == len; i++)
	{
		if(isalpha(sentence[i]))
			temp[i] = tolower(temp[i]);
								//Stores all characters as lowercase in a temporary string
	}

Fix :

for (int i = 0; i < len; i++){  
     temp[i] = tolower(temp[i]);								
}

No need to check isalpha.

mrnutty 761 Senior Poster

Sorry to have to say this. Puncuation and spaces can sometimes be very important in codes!!

when checking for palindrome, it does not matter.

mrnutty 761 Senior Poster

Helpful tips :

create a function that :

1) Returns the lower or uppered case of a string
2) Removes all punctuation from the string
3) Removes all spaces from the string

Then what you are left with is something like this for example :

< Original string > Vanna, wanna V?
< after going through the functions above > vannawannav

Then all you need to do is check the (last - n) character with the (first+n)
character.

mrnutty 761 Senior Poster

Java has an easy to work with GUI.

mrnutty 761 Senior Poster

Good job.

mrnutty 761 Senior Poster

>>I need to write a program that count how many nubers can be divide in a interval from k to n, exemple in a interval from 1 - 10 theare "3" numbers that can be divide with 3, here is my program:

Explain a bit more.

mrnutty 761 Senior Poster

>>(all sentences are on one line)

why?

>> Write a C++ program that indicates whether each line on an input file...

mrnutty 761 Senior Poster

To make it easier on your self. Make a function that concats two strings.

//add two string together and places the result in the result variable
void concat(const char *left, const char* right, char * result);

The you can just concat title with first name. Then the result of that
gets concated with the last name. Then finally display the final result.

mrnutty 761 Senior Poster

First

int n, num;
    cout << "Enter the Number of matrix -> ";
    cin >> n; 
    int s[n][n];

The n variable needs to be constant, unless your compiler allowed it,
which is not good.

To pass a multidimensional array you need this :

const int FIXED_COL = 5;
void print(int A[][FIXED_COL], int rowSize);

The compiler needs to now the size of the column for a 2d array.

mrnutty 761 Senior Poster

The reason why one would use one over the other is for efficiency.

Some operations like inserting takes constant time in linked list, while
it vectors it might not.

The only thing you will learn in school is how to study on your own.
To become a good programmer, you will have to start studying on your
own. Get a good C++ book. Relearn the concept. Then move onto other topics like data structures.

Just to see where you are at, what is the more complicated program you
have made?

mrnutty 761 Senior Poster

Use the stl's : set_intersection , set_difference . They also have other variants of those.

mrnutty 761 Senior Poster

You will need a GUI for that. Look up allegro,opengl directX.

mrnutty 761 Senior Poster

When you don't specify a copy constructor the compiler does a shallow
copy. Which means that it just copies the value from one class to another.

MyClass obj ( anotherMyClassObj ); //shallow copy

Now if inside MyClass contains a pointer variable( allocated by new).
Then that copy constructor does not copy the pointers value, but
they both point at the same location. This causes many troubles.

mrnutty 761 Senior Poster

In c ( or C++) : 0 is false, and any other number is true.

kvprajapati commented: Yes. +6
mrnutty 761 Senior Poster

Seems fine. Although you might want to Make a const MAX_NUM_OF_NAVY
then use that where ever you need it.

mrnutty 761 Senior Poster

is there any way to prevent this???

Yes there is, probably.

mrnutty 761 Senior Poster

people

mrnutty 761 Senior Poster

In your example you have a function template: maximum<>,
but what I have is a template class myclass<>!
And my problem is that the classes have different member functions
and I cannot use them!
For example my template parameter can be (I would like to be)
double or my wn defined complex class.
But I can not use member function of my complex class,
because double dont have them,
since in runtime I dont call it with double.

Can you post a code of your problem or explain a bit more clearer?

mrnutty 761 Senior Poster

Maybe this will help :

bool save(std::ostream& saveFile, string fileName){
 //save data
}
mrnutty 761 Senior Poster

Care to explain how?

Since your array is Array[5] , then only words of 4 chars or less will
work because it needs to store the null character. If not then its a bug.

The NULL padding was a requirement for my project. How else should I go about it ?

Use string. And you won't have to worry about any of this.

mrnutty 761 Senior Poster

This is not right :

int sort(int arr[], int vals)
 {
      int temp;
 
            for(int cnt=0; cnt< (vals-1);cnt++)
              {
                       if(arr[cnt]>arr[cnt+1])
                        {       temp=arr[cnt];
                                arr[cnt]=arr[cnt+1];
                                arr[cnt+1]=temp;
                        }
                }
}
mrnutty 761 Senior Poster

This is also troublesome in many ways:

for (i=actual_len; i<=4; i++) {
		// Pad NULLs
		lesser_msg[i]=NULL;
	}
mrnutty 761 Senior Poster

Oh, well - This is just a part of the bigger code.
This actually is the part where the msg is less than 5 chars.
I should have mentioned this, but the input I gave was less than 5 chars and it worked as expected on the UNIX machine (under g++).

Surprisingly, its the first time I'm noticing something that doesn't work in Windows, but does under UNIX (Very rare!)..

The input should be 4 character or less. And just use string!

mrnutty 761 Senior Poster

IN line 33, 49, and 87, remove the semicolon so the while loop executes
like the above post said.

mrnutty 761 Senior Poster

@OP : I think you need to rethink your code (if you have time).

You are hardcoding it way too much. It makes it hard for us to help you.

For example you are doing a lot of :

if (students >> studentid[i] >> fname[i] >> lname[i] >> Q1[i] >> Q2[i] >> Q3[i] >> MidTerm[i]  ...

By making it into a loop, you code would be about 50 percent shorter
and more understandable.

What do you think?

mrnutty 761 Senior Poster

An array and a list are two different things. Its a lot easier to sort an array than linked list.

Depends of the definition of "easier" and relative to whom :

int A[4] = {4,5,-1,2};
list<int> B(A,A+5);

std::sort(A,A+5); //sort array
list.sort(); //sort list
mrnutty 761 Senior Poster

google the standard std::sort function.

mrnutty 761 Senior Poster
char lesser_msg[5];
	actual_len = strlen(message.c_str());
 
	for (i=0; i<actual_len; i++) {
		// Pad data
		lesser_msg[i]=message[i];
	}

What happens if the actual_len is greater than 5?

mrnutty 761 Senior Poster

Why exactly do you need the runtime template parameter ?

And check your spelling for sizeof.

mrnutty 761 Senior Poster

Well you cannot just dump some random code( in our perspective) and
ask for help without providing us no clue.


For a soduko solver, I think recursive backtracking is a ok candidate.
Google it. As for your solution, I have no idea what how it works so I
cannot help.

mrnutty 761 Senior Poster

remember sin and cos takes angle in radians. But other than that
I don't know whats your problem?

mrnutty 761 Senior Poster

No but you can adjust I guess.

string type;
cout<<"Choose Type  (int,string,float) : ";
cin >> type;

if(type == "string"){
myClass<string> stringClass;
//do stuff
}
else if(type == "float"){
myClass<float> floatClass;
//do stuff
}
//and so on.

But C++ does not runtime templates.

mrnutty 761 Senior Poster

Is this a spam ?

mrnutty 761 Senior Poster

Your prototype :

void read_salary(double , int);
int max_salary(double, int);

Means that they take a double variable and an int variable.

Instead use this :

void read_salary(double Array[], int);
int max_salary(double Array[], int);
mrnutty 761 Senior Poster

when you know PI by heart : 3.141592653589793

mrnutty 761 Senior Poster

What are you trying to again? Just need a bit more explanation.

mrnutty 761 Senior Poster

>Because if it was const double& then you can't just say
>realObject + 5, because 5 is a literal and we can't have a refrence to it.

Fortunately, binding an rvalue to a const reference is legal. I simply default to passing built-in types by value as a matter of habit. ;)

Ok, you proved me wrong.

mrnutty 761 Senior Poster

This is a good library. Try inputting 500!
in their sample program and see what you get.


P.S : its factorial and not fectorial

mrnutty 761 Senior Poster

Is there any reason dear Narue that you write

real operator+ ( const real& lhs, const real& rhs );
real operator+ ( const real& lhs, double rhs );
real operator+ ( double lhs, const real& rhs );

And not "const double&" like this?

real operator+ ( const real& lhs, const real& rhs );
real operator+ ( const real& lhs, const double& rhs );
real operator+ ( const double& lhs, const real& rhs );

Because if it was const double& then you can't just say
realObject + 5, because 5 is a literal and we can't have a refrence to it. Instead you would have to create a variable, and do :

double dummy = 5.0;
realObject + dummy;
mrnutty 761 Senior Poster

>If you want to do it that way, you will need to make that function a friend function.
Yes, thank you for the correction. I'll blame a disconnect between my brain thinking friend and my fingers thinking member. ;)

No problem. Its good to see that you are a human as well
and not an all knowing android.

mrnutty 761 Senior Poster

You cannot initialize the member variables inside the class:

//WRONG
int n = 0;
int* ArrayOne = NULL;
//CORRECT
int n;
int *ArrayOne;
public : classConstructor(int _n, int Size) { n = _n; ArrayOne = new int[Size]; }

Get that done first.

mrnutty 761 Senior Poster

Personally I do not find reading code much helpful. Its usually has little
comments, and if not written clearly, then its not-comprehensible. I don't
get out of it as much as if I would from reading books, ASKING
QUESTIONS, coding or doing other things. But I do once in a while read
code, usually the stl code of whats available, or from some really smart
people that tries to explain certain things.

mrnutty 761 Senior Poster

>which one is necessary to overload if i would like to make -x understanable for the compiler?
The negation operator is a single argument member function:

class foo {
public:
  // Overload negation
  foo operator- ( const foo& obj );
};

That is not overloading the negation operator. Thats overloading
the subtraction operator. A negation has no arguments and
deals with the this pointer. If you want to do it that way, you will need to make that
function a friend function.

mrnutty 761 Senior Poster

The objective is to make it last long as possible. The rules is that
I say ONE word, and another person has to say ONE word which
will be a part of a sentence when more word comes.

A sample run :

Post#   The word posted
---------------------------------
1        Hello
2        This
3         is
4        Waldo
5         where
6         are 
7         though

and so on. Get it, good. Lets start!


Starting word is :

Today

mrnutty 761 Senior Poster

Can you use strings or char arrays ?

One problem is in your binary addition function. Here is how you would
add binary numbers :

1 1 1   //carry
  ------ 
    1 0 1
   +  1 1
   -------
  1 0 0 0

remember 1 + 1 = 0 with 1 carry over.

Alternatively you can get integers and use integer addition and convert
the integers into binary numbers.

mrnutty 761 Senior Poster

-But i don't know how to code this..., any help?

something like this :

for part a :

string input = "";
cin >> input;
string copyInput = input;
copyInput[0] = toupper(copyInput[0] ); //make it upper case
//then print it backwards

I assume if you couldn't get this then the code you presented
is not completely yours?

mrnutty 761 Senior Poster

By the example you gave us, this : "mcDonalds car 456 burgerKing 8
clowns are scary money leeking from 56 my pockets I like turtles 1".

I would suggest that you should extract the sentence word by word.
Then when you extract a word, you need to check if its a numeric by
using the isdigit function in cctype header file. If it is numeric, then
you can extract the data into a int variable and add it to the total sum.
Here is an example :

#include<iostream> //i.o
#include<cctype> //for isdigit
#include<sstream> //to phrase string
#include<string>

using namespace std;

//checks if a string is full of digits
bool isDigits(string word){
	for(unsigned int i = 0; i < word.size(); ++i){
		if(!isdigit(word[i])) return false;
	}	
	return true;
}
int main()
{
	string sentence = "what does 10 + 4 + 1 = ";
	stringstream extract; // extract words by words;

	extract << sentence; //enter the sentence that we want to extract word by word

	string word = "";

	int totalSum = 0;

	//while there are words to extract
	while(!extract.fail())
	{
		extract >> word; //extract the word

		if(isDigits(word)){//if the string is full of digits
			
			stringstream convert; //converts strings to ints
			convert << word; //insert the digits into our converter
			int theNumber = 0; //will hold the number inside the string
			
			//convert the string number into a int number and place it inside of theNumber variable
			convert >> theNumber; 

			//add the number to the total sum …
timlo commented: to the point, carefuly going through the steps in the code so even the most novice can understand +0
mrnutty 761 Senior Poster

But on the other hand the efficiency of these are completely different :

//with pointers
void DivideBigNumbers(BigNumberClass * num, BigNumberClass * num);
//without pointers, just object
void DivideBigNumbers(BigNumberClass  num, BigNumberClass  num);
//and there is reference
void DivideBigNumbers(BigNumberClass&  num, BigNumberClass&  num);
jasonline commented: I agree. It really depends on the situation. Passing parameters by reference/pointer is recommended to passing by value. +1
mrnutty 761 Senior Poster

Hold shift front_slash.

The frontslash is usually on top of the enter key.

mrnutty 761 Senior Poster

This is your call :

mystruct s(1.,2.);

This is mystruct constructor :

mystruct(double z)

Either make the constructor :

mystruct(double z , double z2);

Or make the call :

mystruct s(1.0);