Your code is asking for trouble. You need to seed the random generator,
put this code inside the main where it will be called only once, :
srand( time(0) );
Your code is asking for trouble. You need to seed the random generator,
put this code inside the main where it will be called only once, :
srand( time(0) );
>> EDIT: fP remember when you kept getting beaten to the punch the other day, well I think it's my turn today
I am sorry, that is all I have to say:)
One more thing instead of malloc you said do this, I don't think this is correct
node* temp = new node(imissjava);
Well with that I was suggesting you create a constructor for your
node that takes that parameter and initializes the data. That way
you don't have to do temp->data = someData;
For example your definition for node is this :
struct node{
string data;
node *next; // Pointer to next node
};
What that code you have to do this :
node *temp = new node;
temp->data = someData;
temp->next = null;
head = temp;
What I suggested is that you do this :
struct node{
string data;
node *next; // Pointer to next node
node(const string& initData){
data = initData;
next = NULL;
}
};
That way all you have to do is this
node* temp = new node(someData);
head = temp;
>>srand(unsigned(time(NULL)));
You only need to do this once. Put that statement inside main, and just
call it once. It only needs to be called once.
Let me spell it out to you :
main.cpp: In function ‘int main(int, char**)’:
Inside your main function
main.cpp:15: error: no match for ‘operator*’ in ‘d * 1.0e+0’
There is no matching function function in the call :"e = d * 1.0;"
derived.h:15: note: candidates are: double Derived::operator*(const Derived&) const
It even says it here, the only valid function is "double Derived::operator*(const Derived&) const"
Unfortunately you don't show us the add() method itself. Does it make sure that the last pointer is not null? Does it create a head at the beginning if there's not one?
He shows it at the beginning :
void LinkedList::add(string imissjava) {
temp = (node*)malloc(sizeof(node));
temp->data = imissjava; // store data(first field)
temp->next=head; // store the address of the pointer head(second field)
head = temp;
}
Ok, couple of things you should change here. First do not use
malloc in C++, use the operator new. Like so :
Node* temp = new Node(imissJava);
Second, you should append the node to the head, if its null,
else to the tail. So it should be something like so :
if head is null head equals new Node. Set head data to some value
else iterate until tail->next != null. Then set tail->next equal new
Node. Set tail->next->data to some value;
In code it might look something like this :
if(head == NULL){
head = new Node();
head->data = someData;
}
else
{
Node * temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = new Node();
temp->next->data = someData;
}
Usually its convenient to have a head node and a tail node
as a member variable.
Third, get rid of the temp member variable.
Fourth, I am sorry you miss java, get over it.
it will never compile. Did you forget this was the C forum?
ya I did. Lately, I been of my game. My school started so I have no time
for leisure and I have a lot of stress right now. Sorry about that.
how is writing findArray going to help the situation i need tips on how to write find2D
I told you , in fact I showed you. Use find1D inside find2D for every
row. If found return true. If end of loop, return false;
ya something like this looks right, im just wondering where u linked the function findArray() in here where does it call it
It should be something like this :
bool findKey(const int* Array, const int KeyToFind, const int MaxSize){
size_t pos = 0;
assert( MaxSize >= 0 ); //better be positive or 0
for(; pos != MaxSize; ++pos){
if(Array[pos] == KeyToFind) break;
}
return !(pos == MaxSize); //if found pos will not equal MaxSize
}
Also, notice that your case function looks like this :
switch(number)
{
case 1: //blah
case 2: //blah
}
There is a problem with that. The problem is that you used constant
numbers in your switch case. Usually, those numbers means something,
so making a const variable for those number, would make your code
better. Its better to get into a good habit early.
So for example, you can do something like this :
const int QUIT = -1;
const int GET_BANK_NAME = 1;
const int GET_STATE_NAME = 2;
const int PRINT_NET_WORTH = 3;
while( input != QUIT )
{
//some stuff goes here
switch( input )
{
case GET_BANK_NAME : //logic here
case GET_STATE_NAME : //logic here
case PRINT_NET_WORTH : //logic here
case QUIT : //logic here
}
}
See how much more readable and better it looks?
Only this time, it wasn't.
My reply to this thread is there in the singular.
But I also got a "This forum requires that you wait 15 seconds between posts. Please try again in 1 seconds."
I rather suspect had it timed out differently, it would have been a double post.
FWIW, I did a "posts since last visit in subscribed forums" search while the post was in progress.
The same happen to me, but I only hit the submit button, and
was not multitasking.
>> It's already tomorrow in Australia
*Well the good news is that they will be dead before the people in america.
The bad news is we will still see them in the after life, *shurg*, O weLL.
------------------------------------------------------------
* Just a joke, if you take that seriously, then I will kill you, personally.
You mean something like this :
bool find2D(int Array[][COL],const int key, const int MaxRow){
bool isFound = false;
for(int row = 0; row != MaxRow; ++row){
if( findArray(Array[row], COL, key){
isFound = true;
break;
}
}
return isFound;
}
Damn as soon as I graduate college, I will be dead. I guess I should
drop out.
You do need a second for loop (one for the rows, one for the columns, in the array) It may be easier to use pointers:
int* return_ptr(int str[][20], int x, int max_rows, int max_col) { int* a_ptr = new int; for (int i = 0; i < max_rows; i++) { for (int j = 0; j < max_col; j++) { if (str[i][j] == x) return a_ptr = &str[i][j]; } } }
Not a good idea. It could potentially cause a memory leak.
Do something like this :
const int Row = 5;
const int Col = 5;
struct SearchInfo{
static const int INVALID = -1;
size_t row_;
size_t col_;
SearchInfo() { row_ = col_ = INVALID; }
}
SearchInfo find(int *Array[Col], const int Key, const int MR, const int MC){
SearchInfo ret;
for(int i = 0; i != MR; ++i )
for(int j = 0; j != MC; ++j){
if(Array[i][j] == key){
ret.row_ = i;
ret.col_ = j;
break;
}
return ret;
}
int main()
{
int Array2D[Row][Col] = { {0} };
SearchInfo info = find(Array2D, 0, Row, Col );
return 0;
}
Or if you want to represent 2d array as 1d then you can do this :
const int Row = 4;
const int Col = 4;
int find(int * Array,const int key,const int row,const int col){
int ret = -1;
for(int i = 0; i < row * col ; ++i){
if(Array[i] == key) {
ret = i;
break;
}
}
return ret;
} …
Hey, welcome to the forums. Typically this forum is used for learning/ correcting purposes even on the more advanced stuff (which is in the 'Please read before posting' section of the forum rules) which means that forum members are not typically going to do the work for you. Instead, post up what you have accomplished so far and people will be MUCH more likely to help you.
Unless there is some form of compensation involved, ahem.
Here is an example :
5. Write a C++ statement that defines a Square object named square1 with side length 5.
Square square1 = Square(5);
The above is assuming the class is Square is similar to this :
class Square{
private:
int len_;
public:
Square(const int Length) { len_ = Length; }
};
6. Write a C++ statement that changes the side length of object square1 to 10.
square.setLength( 10 );
The above assumes the class Square is defined as :
class Square{
private:
int len_;
public:
Square(const int Length) { len_ = Length; }
void setLength(const int newLen) { len_ = newLen; }
}
7. Write a C++ statement that prints the side length of object square1.
It should be something like this :
square.printLength();
Assuming the class Square is similar to below :
class Square{
private:
int len_;
public:
Square(const int Length) { len_ = Length; }
void setLength(const int newLen) { len_ = newLen; }
void printLength(){ cout <<"Length = " << length << endl; }
}
8. Write a C++ statement that prints the area of object square1.
<you>"cin >> "area = " >> square1 * square1 ; "
Not quite right unless you overload the operator *;
It should be something like this :
square.printArea();
And Square is define as :
class Square{
private:
int len_;
public:
Square(const int Length) { len_ = Length; }
void setLength(const int newLen) { len_ = …
>>1 more very good solution is use "strtok"
Not quite.
using the stringstream is safer :
string sentence = string("hello this is a sentence");
stringstream phraser;
phraser << sentence;
int wordCount = 0;
string temp;
while(phraser >> temp){
++wordCount;
}
cout << "There are " << wordCount << " words in this sentence :' "<<sentence << " ' \n";
Can you give an example of what you mean ?
if you mean for example ,
<input> hello
<output> olleh
then all you have to do is this :
string input;
cin >> input;
reverse(input.begin(),input.end());
cout << input << endl;
>> SingleList() : front_(0), back_(0) {} //I don't get what these 3 lines do
That is called the initialize list, you can assume its the same as this for now:
SingleList(){
front_ = 0;
back_ = 0;
}
>>void _addNewNodes(const size_t Size, const DataType& initValue){ //This is for adding a new item to the linkedlist I am assuming, but how exactly would I add a new node like could you give me an example? //blank }
How about you start it ovoid _addNewNodes(const size_t Size, const DataType& initValue){ //This is for adding a new item to the linkedlist I am assuming, but how exactly would I add a new node like could you give me an example? //blank }
Forget about that, try to make one like this first :
void addNewNode(const DataType& initValue){
//if head is null then add new Node to head
// and make head->next null;
//else loop until you find that node->next is null.
//when found add new node and return ;
}
>>When assigning a pointer variable, it must always point to an address.
Thats incorrect. Hint what about null pointers?
I just did this last semester for my java class. If that makes you feel better.
See the difference between these two :
MainMenu::MainMenu()
{
destanation="no destanation yet.";
departure="no departure yet.";
date="no date entered yet.";
time="no time yet.";
}
MainMenu::MainMenu(string the_name)
{
name="No name yet.";
}
In red is the added part.
You forgot to add MainMenu::
MainMenu::MainMenu(string the_name)
{
name="No name yet.";
}
Whats the error ?
You need a forward decleration. Put this at the top of your file :
template <class typos_stoixeiou>
struct akmi;
template <class typos_stoixeiou>
struct korifi{ ... }
when you use cin >> , it reads until it sees a space. Do this :
char firstName[100] = {0};
char lastName[100] = {0};
cin >> firstName >> lastName;
Thats the easy solution and error prone. The better solution is the use strings.
string firstName, lastName;
cin >> firstName >> lastName;
Even better do this :
string fullName;
getline(cin, fullName); //read the whole name
Use this :
#include <iostream>
#include <string>
using namespace std;
int getInt(const string& messageBeforeInput){
cout << messageBeforeInput;
int result = 0;
while(! (cin >> result) ){ //check for bad input
cout << messageBeforeInput ;
cin.clear(); //clear the stream bits
while(cin.get() != '\n') continue; //get rid of the junk
}
return result;
}
bool failsRange(const int value, const int min, const int max){
return value < min || value > max;
}
int main()
{
int value = -1;
while( failsRange(value,0,100) )
value = getInt("Please enter a number :" );
cout << value << endl;
return 0;
}
It could look something like this, simplified for your pleasure( not Compiled)
typedef int DataType;
class SingleList
{
public:
struct Node{
DataType value;
Node * nextNode;
Node(const DataType& initVal) : value(initVal), nextNode(0){}
};
typedef Node* NodePtr;
private:
NodePtr front_;
NodePtr back_;
public:
SingleList() : front_(0), back_(0) {}
SingleList(const size_t Size, const DataType& initValue){ _addNewNodes(Size,initValue); }
private:
//helpers
void _addNewNodes(const size_t Size, const DataType& initValue){
//blank
}
};
Of course there is more to it but you only ask for help to get started
>>I thought about using an Exception
There is no need to use exception here because it will only complicate your
code. Why not just display nothing? Or display a message saying
no employees to show. Do something like this :
if(choice == VIEW_EMPLOYEES){
if(employees.empty()) {
displayErrorMessage()
}
else displayEmployees();
}
Pointer to class becomes useful when you get into data structures.
>> Why it work properly without using your solution? What is the difference between them.
Because before you used cin >> ... , the cin ignores spaces and newline,
and in your program before, the newline character is still left in your
stream and when you try to cin something it automatically read the newline.
But if you use getline(...), then getline reads the input plus the newline character( when you press enter) so in the above code, there is no newline character left in the stream.
>> The program cannot get the name. In fact, it doesn't allow me to type
That because when you read the age, the stream reads the number and
the newline character. That new line character is being read into the
variable name. To fix that problem add this :
/*IO Example*/
#include <iostream>
#include <string>
using namespace std;
int main() {
int age;
string name;
//
cout << "Please enter your age:";
cin >> age;
cout << "Your age is: " << age;
cout << " so your year of birth is " << 2010 - age << ".\n";
{
string dummy;
getline(cin,dummy);
}
cout << "Please enter your name: \n";
getline (cin , name);
cout << "Hello " << name << " ! What a good day!";
return 0;
}
1) Do you know how to access the elements in the array
2) Do you know how to use a for loop
3) Do you know how to use add a value
If so then you know how to do it,
else ask about a even more specific question.
printf is faster than cout, in visual studio 2008 express. Try substituting
that. How about you post some code that you might think is the bottle neck of your program and we will see how you can improve it.
>>void expense::setmonth()
{
cout<<"\n Enter the Month: ";
getMonth();
}
Change to :
void expense::setmonth(std::string month){
currentMonth= month;
}
I am not sure what your class does exactly but general idea is given above.
Just wondering: are you intentionally trying to mislead OP?
Oops the sign must alternate,
Sin(x) = x - x^3/3! + x^5/5! - x^7/7! + x^9/9! ...
//Sin(x) = x - x^3/3! + x^5/5! - x^7/7! + x^9/9! ...
float Sinus( float val ){
float result = val - std::pow(val, 3.0) / factorial(3);
result += std::pow(val,5.0) / factorial(5);
result -= std::pow(val,7.0) / factorial(7);
result += std::pow(val,9.0) / factorial(9);
return result;
}
>> a+b=23
Not taking into account floating points and just positive natural numbers. Then
if b > 23 then no Answer Possible
if b = 23, then a = 0;
if b = 22, then a = 1
if b = 22, then a = 2
General Equation : B = 23 - a OR A = 23 - b
Following this pattern b could be from 23 - 0 and a could be from 0 - 23
So there are 2 * 23 = 46 different solutions.
If thats not it then clear up what you want.
>>u = new double[I*sizeof(double)];
No! This is not C. In C++ its used like this :
u = new double[ I ];
The value inside [ ], is the number of elements. No need for the sizeof.
Put your printing loop out side separately. Like this :
void Foo(const int Size, const double value = 0 )
{
double *Array = new double[ Size];
//initialize it
for( int i = 0; i != Size; ++i){
Array[i] = value;
}
//print it
for(int i = 0; i != Size; ++i){
cout << Array[i] << " ";
}
}
>>u = new double[I*sizeof(double)];
No! This is not C. In C++ its used like this :
u = new double[ I ];
The value inside [ ], is the number of elements. No need for the sizeof.
Put your printing loop out side separately. Like this :
void Foo(const int Size, const double value = 0 )
{
double *Array = new double[ Size];
//initialize it
for( int i = 0; i != Size; ++i){
Array[i] = value;
}
//print it
for(int i = 0; i != Size; ++i){
cout << Array[i] << " ";
}
}
>> Is the memory relinquished when they go out of scope?
Yes long as its declared without the new operator, and if it was then
using delete will release the memory.
>> Your code does not satisfy the OP's requirements. If the OP searches for the word "Hello" in the string "Hello, world!", your code cannot find it because it only delimits on whitespace (which would get the string "Hello," from the file, not "Hello").
Oh, I guess I should have read the exact specification.
Also in both of your examples you provide a copy constructor, how
come? There is no need for that. And if one provides a copy constructor
one should also provide a destructor and an assignment operator,
but your example does not use dynamic allocation explicitly so there
is no need for copy ctor, unless I missed something :)
>> But I feel that this is abusing memory quite a bit
how so ?
>> My feeling is that it might not be necessary, or even possible, to use the new operator with vectors.
It is possible to use the new operator for vectors. It is not necessary
to use the new operator. Only use it when variable on stack won't
work.
I think your first approach is fine.
I think either one of these will do for me :
That idea was sooo not stupiiiddd
That idea was so not stupid<sarcasm>
That idea was so not stupid, cough* bullSh**cough
compare your program to this( not compiled )
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream file("Ahoo.txt");
if(!file) return -1;
string wordToFind;
cout <<" Enter a word to find in the file Ahoo.txt\n";
cin >> wordToFind;
string word;
bool isFound = false;
while( file >> word ){
if( word == wordToFind ){
isFound = true;
break;
}
}
if(isFound ) cout << "Word is found\n";
else cout <<"Word is not found\n";
return 0;
}
Sin(x) = x - x^3/3! + x^5/5! + x^7/7! ...
Cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! ...
Cos(x) = Sin( x - 90 ) ; //assuming x is in degrees
Tan(x) = Sin(X)/cos(X)
"!" means factorial.
for example to calculate Sinus you can do this :
//Sin(x) = x - x^3/3! + x^5/5! + x^7/7! ...
float Sinus( float val ){
float result = val - std::pow(val, 3.0) / factorial(3);
result += std::pow(val,5.0) / factorial(5);
result += std::pow(val,7.0) / factorial(7);
result += std::pow(val,9.0) / factorial(7);
return result;
}
>> expected class-name before
Is wxNoteBook a class ?