Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Put only function prototypes and extern object declarations in header files, such as

// A.h file
extern void foo();
extern int f1;

Now, in one and only one *.cpp file you have to declare the same without the extern keyword.

// A.cpp file
#include "A.h"

int f1 = 0;

void foo()
{

}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 30: why are you attempting to stuff 60 numbers into an array that only contains 12 elements? That loop is asking for the 12 monthly sales 5 times !

Line 45 has the same problem, but a lot more obvious. The array only has 12 elements but it is attempting to sum up 60 numbers.

If you want 60 monthly_sales then declare the array like this (line 15): int month_sales[MON * LOT]; Those two functions don't need to return anything, just make both of them void functions.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 1: create an ifstream object to read from a file.

line 2: Opens a file for reading

line 3: declares a character array of BUFLEN number of bytes.

line 4: sounds the internal computer speaker, assuming it has one.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>to start off thanks for the help in advance,

You forgot to ask question(s)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Welcome to DaniWeb.

Hi,
How to get all session in an application which is login by different users....

That makes absolutely no sense to me. If you are not a native English speaker, then use Notepad to write out your question in your language then use a translator to translate it to English.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>>Is this the right way to do it?
>No.
No. When you use nothrow, new returns a null pointer instead of throwing bad_alloc.

Your catch is also malformed. ;)

Yes I realized that after posting (open mouth and shoved in foot). I probably changed my post since you read it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Is this the right way to do it?
There are two ways to do it. The preferred way is to use try/catch blocks

int main()
{
    int * bobby;
    try
    {
        bobby = new (nothrow) int [5];
    }
    catch (std::bad_alloc &ba)
    {
        cout << "Oops!  memory allocation failed\n";
        return 1;
    }
    cout << "memory allocation ok\n";
};

The other way (for legacy code) is to use the std::nothrow version

//standard C++ code
#include <new> //required for nothrow new

CWindow p;
p = new(std::nothrow) DerivedWind;
if (!p) //fine now
{
 cout<<"allocation failure!"
 exit(1);
}
//...continue normally

Here is a good link you should read

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Is that code in main, the code that creates a link list of a linked list?

yes

And what's the big problem ?

Compare the structures I posted with the ones you posted and you will see the big problem in your structures.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I moved this to a new thread to avoid hijacking the other thread. Continue the discussion about the new operator here if you wish.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Yeah, you were right, in the book they we're talking about dynamic memory allocation with the new operator, and when allocation with new fails it returns a special pointer value: NULL , otherwise the pointer is pointing to the newly assigned memory :)

Either you are reading a very very old book or its just plain wrong. new operator does not return NULL, it throws an exception and leaves the pointer unchanged.

mplementation

In compilers conforming to the ISO C++ standard, if there is not enough memory for the allocation, the code throws an exception of type std::bad_alloc. All subsequent code is aborted until the error is handled in a try-catch block or the program exits abnormally. The program does not need to check the value of the pointer; if no exception was thrown, the allocation succeeded. The implemented operations are defined in the header <new>. In most C++ implementations the new operator can also be overloaded to define specific behaviors.

http://en.wikipedia.org/wiki/New_(C%2B%2B)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
struct Fast_Food_Business
{
    string name;  
    struct cash_registers* RegHead;
    struct cash_registers* RegTail;
};

struct cash_registers
{
    int register_number;
    float total_sales_this_register;
    struct Customer* CustHead ; 
    struct Customer* CustTail;
};
                         
struct Customer
{ 
    int n;
    struct foodItem* foodHead;
    struct foodItem* foodTail;

};

struct foodItem
{
   string food_name;
   int quantity;
   float price;
};

int main()
{
    struct Fast_Food_Business* BusinessHead = new Fast_Food_Business;
  
    BusinessHead->RegHead = new struct cash_registers;
   
    BusinessHead->RegHead->CustHead = new struct Customer;
}

You still have a big problem with those structures. The structures would be allocated something like the above. Note: this is not complete, you still have to add the loops to allocate however many nodes in the linked lists that you want.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

But my C++ Book says so, by the way, why are we otherwise checking whether an assignment has failed or not using if(ptr == NULL) ?

You misunderstood your book. Better read it again. Maybe the book is talking about smart pointers

tux4life commented: You were right, actually I don't have to doubt about what you say: You're just *always* right :) +3
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The second one is syntactically not wrong, but after a pointer declaration the pointer is automatically set to NULL , ..

That is incorrect. The pointer is assigned whatever random value is at that memory location. For example char *ptr; the address of ptr is whatever is at that memory location on the stack (assuming its not a global variable). It is never auto set to NULL by the compiler in either C or C++ languages.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Nice, but don't walk around their lawn in bare feet :) If you have ever stepped on a wet pasture pattie with bare feet then you know what I mean.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Currently, code=lang inserts line numbers starting at line #1. Is there a way to make it start with another number? For example
[code=cplusplus line=500]

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The way I did it was create a new filename every time I wanted to open the file. That way when the date changes my algorithm will automatically pick up the new date.

time_t now = time(0);
struct tm* tm = localtime(&now);
char filename[255];
sprintf(filename,"%04d%02d%02d", 
    tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
// now open the file
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

delete line 48.

That loop at lines 46-59 does not create a linked list of structures. It doesn't do anything at all.

Lines 43 and 44 are ok. That will be the head of the linked list and you don't want to change that pointer -- ever.

Now on line 48 you need to allocate a new pointer then add it to the tail of the linked list. That is what you have not done.

for(int i = 1; i < Resturants; i++)
{	
    Fast_Food_Restaurant* NewNode = new Fast_Food_Restaurant;
    NewNode->next = NULL;
        // now add to linked list
        if( aResturant -> head == NULL)  
        {
          aResturant-> head = aRestaurant-> tail = NewNode;
        }
        else
        {
         // add new node to end of linked list
         aRestaurant -> tail ->next = NewNode;
         aResturant -> t= NewNode;
    }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

if want the node clear before you get a new one

Head = NULL;
//or
Head = 0;
Node Head = new Node;

this will make sure it is empty

You are just wasting CPU time doing that. A pointer does not have to be set to 0 (or NULL) before calling new, or assigning it to some other address. There may be other legitimate reasons for doing it, but that is not one of them.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The second. The first will cause a memory leek. Although in the code you posted you don't need Head = NULL; because it just changes that pointer to whatever is returned by new.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> cout<<"Hello " << strcat(first_name, last_name) <<"!"<<endl;

Don't do that! every time that line is executed strcat() will append last_name to the end of first_name, eventually causing the program to scribble all over memory.

A solution is to declare another variable char name[255]; and after entering the last ane first names, copy them both to that variable -- only ONCE. sprintf(name, "%s %s", first_name, last_name); Do you know about std::string c++ class yet? Are you allowed to use it in your program? If yes, then replace all those character arrays with std::strings

std::string last_name;
std::string first_name;
std::string name;
...
...
name = first_name + " " + last_name;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I have run your program several times and don't see a problem other than you need to put a space between the first and last names when you print them on the screen.

If you don't want to have to type your name every time you run the program then you need to save that info to disk. Then the first thing the program should do is prompt for the account number. Check the data file for the account number and, if found, read the first name, last name, and current balance. Otherwise if the account number is not on file then prompt for the first and last number. Before exiting the program it needs to write that information back to the file.

Also the name is getting printed wierdly

Welcome to First National Bank ATM!
ATM software developed by: Shaun Dougherty
Please Enter your first name
mel
Enter your last name
sto
Hello melsto!
Please enter your account number
100
Welcome, doejohndoe<<<< HERE 
Account #:100
Current Balance: $2000.99
-----------------------------------------
 **********************************
 *               MENU             *
 *  1. Deposit     2. Withdrawal  *
 *  3. Fast Cash ($60) 4. Check Balance *
 * 0 = exit                             *
 ****************************************
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

After making the change I suggested, I get this output

ctor1 is called!
ctor1 is called!
ctor1 is called!
ctor1 is called!
//////
//////
//////
//////
dctor is called!
dctor is called!
dctor is called!
dctor is called!
Press any key to continue . . .
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Try this:

#include <cmath>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

class mVect
{
    private:
        // NO PRIVATE DATA
    public:
        double i, j, k;
        mVect();
        mVect(double,double);
        mVect(double,double,double);
        void print(); // NEEDS EXTRA LIBRARIES
};

// INITIALIZE EMPTY VECTOR
mVect::mVect()
{
    i=0;
    j=0;
    k=0;
}

// OUTPUT THE VECTOR IN THE FORM [i,j,k]
void mVect::print()
{
    cout << "[" <<  i << "," << j << "," << k << "]\n";
}


// INITIALIZE 2D VECTOR
mVect::mVect(double a, double b)
{
    i=a;
    j=b;
    k=0;
}

// INITIALIZE 3D VECTOR
mVect::mVect(double a, double b, double c)
{
    i=a;
    j=b;
    k=c;
}

void main()
{
	ofstream out_file;
	ifstream in_file;
    int y;
	char *out_name = "test.bin";
	out_file.open(out_name,ios::binary);

	double matrix[2][3] = {1,2,3,4,5,6};
	// write matrix to a disk file.
	for (y=0; y<2; y++)
	{
		for (int x=0; x<3; x++)
		{
			out_file.write(reinterpret_cast<char*>(&matrix[y][x]),sizeof(double));
		}
	}
	out_file.close();
	cout<< "Matrix has been written to test.bin."<<endl;

    // read the matrix from the former file.
	double matrix2[2][3];
	in_file.open(out_name,ios::binary);
	
	for (y=0; y<2; y++)
	{
		for (int x=0; x<3; x++)
		{
			in_file.read(reinterpret_cast<char*>(&matrix2[y][x]),sizeof(double));
		}
		mVect vec(matrix2[y][0],matrix2[y][1],matrix2[y][2]);
		vec.print();
	}
	in_file.close();
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Not sure I quite understand. Let's say the file contains "ABCEFGH". Now seek to there the 'E' appears and write "KLM". What result do you want
1) ABCKLMH
2) ABCKLMEFGH
3) something else

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

not only does [code=cplusplus] give syntax highliting but also line numbers. So are you suggesting [code] also provide line numbers?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You are going to have to clarify your request quite a bit more. There are trillions of ways to write functions, so what exactly has your instructor asked you to do ?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Does curl involve WinApi (like i believe winsock does, or at least it seems that way) ?

Probably, but its transparent to us programmers because its cross-platform. It uses the api from whatever platform we want to use it on.

I am having huge trouble finding my way around the WinApi :/

Join the party :) We all have problems with it because it is so huge.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If this is c++ why don't you just use fstream instead of FILE*.

>> fprintf(out, FindData.cFileName);
>> fputc('\n', out);

why not just this: fprintf(out, "%s\n", FindData.cFileName); >>I have to press Ignore in a block damage error messagebox everytime I tun it
What? I have no idea what you are talking about.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I see one problem -- line 61 default constructor does not allocate any memory for GridUnit so when the class is destroyed it will try to deallocate an unallocated pointer. If you don't want the class to allocate memory then set gunit = 0; and all will be well.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

OMG I don't think I will ever eat Jell-O again! Well, but then again it does tast soooo good especially when mixed with fruit before chilling.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Google search for ODBC, which is the oldest and most widely used way to access databases from almost any programming language. You will also find ODBC tutorials in C language. It would be a little easier to do it in C++, but that's beside the point since you specifically want C language.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>T.setB(4).setC(5);

The second method you posted is preferred. The above method is unconventional and may be easily misunderstood and confused by readers.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Yes, you could just read the entire input file into memory then rewrite it back to the original file. But ... what will you do with a huge file whose size is several gigs? Using a temp file like I described is the general method to do what you want because it works with files of any size without consuming too much memory.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If this turns into another "I love you Dani" thread, I swear I will ban you! (not really, but I'd like to).

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You have to completely rewrite the file.

open original file for reading
open temp file for writing
while not end-of-input-file
   read a line from input file
   if this is the line to be replaced
        write new line to output file
   otherwise 
        write the line to the output file
end loop
close both files
delete original file
rename temp file with the name of the original file
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Test T(1).setB(4);

Should be

Test T(1);
    T.setB(4);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There is a small minor problem in the Preview window with the dashes below extending beyond the right side of the bounding box. Its ok in actual post, only Preview has that problem.

//--------------------------------------------------------------------------------------
// File: ConfigManager.cpp
//
// Implementation of configuration system functions.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "dxut.h"
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>im waiting for your reply
I'm not going to write it for you. YOU write the code and we will halp with whatever questions you may have. But the code you turn in must be your own, not ours or mine.

tux4life commented: Well said :) +3
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I noticed it when I started this thread

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>But I been searching for a week now
You mean you have wasted a week for nothing.

Didn't you look at the 2nd google link ? It has a number of threads that might help you.

Here -- that thread even has a example console program.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

return statement is missing in your code snippet.

Its optional. return 0; is implied by omission.

Poster had used DateTime datatype (.net) so it is out of C++ forum.

You might be right, I had not noticed that. If OP is writing managed code then the snipped I posted may not work because it is unmanaged code.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I've been getting wierd behavior in c++ forum too. Sometimes code tags works and sometimes it doesn't. I toggle pain text and sometimes get a blank window, close the thread, reopen it and it might work the next time. That's why I started that other thread here. Next time it happens I'll post a screen shot of it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The standard way to do it is to use the time functions in time.h

#include <ctime>

int main()
{
    time_t now = time(0);
    char* stime = asctime( localtime(&now) );
    cout << stime << "\n";
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

When there are more than one line that belongs to an if statement they need to be enclosed in { and } braces

if ( condition is true )
{
    statement 1
    statement 2
    ...
}

Otherwise, without the { and }, only the statement immediately following the if condition is executed. All other statements are executed regardless of the if condition.

if( condition is true )
    statement 1
statement 2  // this line is executed regardless of whether
                   // the if condition is true or false

In your program new_tol=tol_price-900; is executed regardless of the value of tol_price

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There are the definitions of those two words -- non-native English speakers may not realize the difference between the two words.

Envy (also called invidiousness) may be defined as an emotion that "occurs when a person lacks another’s [perceived] superior quality, achievement, or possession and either desires it or wishes that the other lacked it."[1] It can also derive from a sense of low self-esteem that results from an upward social comparison threatening a person's self image: another person has something that the envier considers to be important to have. If the other person is perceived to be similar to the envier, the aroused envy will be particularly intense, because it signals to the envier that it just as well could have been him or her who had the desired object.[2][3]

http://en.wikipedia.org/wiki/Envy

Jealousy is an emotion and typically refers to the negative thoughts and feelings of insecurity, fear, and anxiety over an anticipated loss of something that the person values, such as a relationship, friendship, or love. Jealousy often consists of a combination of emotions such as anger, sadness, and disgust. Jealousy differs from envy in that jealousy is about something one has and is afraid of losing, while envy refers to something one does not have and either wants to acquire or to prevent another from acquiring.

http://en.wikipedia.org/wiki/Jealousy

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You need to re-read my previous post -- pay attention to details this time.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

but the new tol.. how am i gonna fix it.. it's supposed to be calculated by the program.. am not supposed to equal it to anything.. right??

Yes you do need to set it to something. Look at the logic of your program -- all it does is display text on the screen for each of those if statements. You need to add something to each if statement to set the value of new_total. And don't forget to add { and } around those if statements

if( ... )
{
   cout << // blabla;
   new_total = ???
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I noticed on that site he has more advanced tutorials -- for a price of $25.00 USD.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I gather that you did not write the code you posted. You are getting a warning on the line while (ch==0); because the variable ch was never initialized to anything, it just contains whatever random junk (data) happens to be in the memory location when main() is entered. How do you fix it? initialize the variable to something (usually 0) char ch = 0;