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

There are probably several ways to do it, but I would use the string search feature of std::string class

std::string line = "<FILE_NAME>Testfile.dat</FILE_NAME>";
std::string filename;
size_t pos;

if( (pos = line.find("<FILE_NAME>") > string::npos)
{
    filename = line.substr(pos+11);
    // truncate the </FILE_NAME> tag
    pos = filename.find('<'); 
    filename = filename.substr(0, pos);
}
Comatose commented: Very Nice +9
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>lint 9: *randno[j],

That is not leagal way to declare an array because i and j must be constants. What you have are two uninitialized integers.

If you want to declare a two-dimensional array of integers but you don't know how many rows and columns then declare it with a double star like this: int ** randno = 0; Now, to allocate both dimensions

arraysize = 100; // number of rows
randno = malloc(arraysize * sizeof(int *)); // allocate array of pointers
//
// now allocate the columns for each row
for(i = 0; i < arraysize; i++)
{
    randno[i] = malloc(10 * sizeof(int)); // each row has 10 integers
}
Alibeg commented: Straight in center +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

wxWidgets and OpenGL -- google for them.

Salem commented: cool efficiency +27
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You can design the text file any way you want to, unless you were told otherwise. What you posted will work. Another common method is to sepate fields with a comma, assuming a comma does not occur in the book name or other fields.

BillyBob, CAL 123.45

And you use ifstream for input. ofstream is only for output.

[edit]What ^^^ said works too :) [/edit]

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

Oh I see now -- the problem is that you misspelled "namespace".

>>If I`m doing mistakes sorry but I`m still practising English.
Your English is pretty good -- you are doing a good job at it :)

ddanbe commented: RESPECT! You have a fine eye! +3
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Ezzaral commented: I love that site :) +16
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here is one of several ways to do it. Run this little program so you can see what it is doing.

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

int main()
{
    size_t pos;
    string tm = "01:02:03";
    char c;
    int h = 0, m = 0, s = 0;
    stringstream str(tm);
    str >> h >> c >> m >> c >> s;
    cout << h << " " << m << " " << s << "\n";
}

>>Is that the situation where i have to use binary i/o?
No. The c++ fstream class will make that conversion for you, just use the << and >> operators to save/restore the integers

int main()
{
    ifstream in("myfile.txt");
    int n;
    in >> n; // get int from the file
}
vmanes commented: good clear response +7
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

double posted here

Salem commented: Thanks for the heads-up +25
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

ExitThread() belongs inside a thread, not in main().

ExitThread is the preferred method of exiting a thread in C code.

There are other methods available to keep main() alive until the threads terminate on their own.

#include <windows.h>
#include <process.h>
#include <iostream>

using namespace std;
unsigned __stdcall Display(void* p)
{
	Sleep(500);
	cout << "Display" << endl;
	cout << *((int*)p) << endl;
	return 0;
}
int main()
{
	unsigned taddr = NULL;
	unsigned createFlag = 0;
    HANDLE handles[5] = {0};
 	for(int i=0;i<5;i++)
	{
		if (handles[i] = (HANDLE)_beginthreadex(NULL,0,Display,&i,createFlag,&taddr))
		{
			cout << "success" << endl;
		}
		else
			cout << "failed" << endl;
	}
    WaitForMultipleObjects(5, handles, TRUE, INFINITE);

	cout << "main's execution is over" << endl;
}
Agni commented: Thanks +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

use nested loops

for(int year = 1; year < 2; year++)
{
    for(int month = 1; month <= 12; month++)
    {
        // blabla
    }
}
anbuninja commented: thanks +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you could use <map> to map the loop counter i with a string. A simple example, uses an array of maps to represent the 4 kinds of cards.

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

map<int, string> deck[4];
int main()
{
    deck[0][1] = "Ace of Spaces";
    deck[1][1] = "Ace of Clubs";
    deck[2][1] = "Ace of Diamonds";
    deck[3][1] = "Ace of Hearts";

    string kind = deck[2][1];
    cout << kind << "\n";
}
mrnutty commented: Even though I haven't learned this, this post is helpful because you made it clear of the use of <map> . Thank YOU +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

winsock IS part of win32 api. If you are looking for more complex client/server console code then you can find it by downloading the library here

>>No Windows API is different. Console applications are the DOS window.
You don't have to write a windows program to implement some win32 api functions.

Freaky_Chris commented: Thank god i'm not alone +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

No fix that I know. Try multiplying everything by 100 and use int math.

Freaky_Chris commented: thanks +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>string ****Nipples;

OMG what in hell are you trying to do here??? If you are trying to code an array of string's then its string Nipples[4]; But the reading part should look like this: (I hope you change the variable names before turning in the assignment because your teacher may not be that humerious.)

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

int main()
{
    ifstream RapidShit ("C:\\Rapid.txt");
    vector<string> Nipples; // an array of strings
    string line; // one line in the file

    if(RapidShit.is_open())
    {
        while (getline(RapidShit, line) )
        {
           Nipples.push_back(line); // add to the array
        }
        // now you have the entire file in memory.  Search each line
        // for the desired entry.
    }
    cin.get();
    return 0;
}
William Hemsworth commented: hehe x) +5
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

can I just add, you need to remember to add a *dest=0; line after the while. You will need something similar in any of the other posts.

No with Arkm's algorithm you don't because it will also copy the null terminator.

StuXYZ commented: Your correct, I don't know why I hadn't noticed that! +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

How to search for something in the Code Snippets boards ??? The "Search In Forums" listbox doesn't include tutorials or code snippets. If I want to find something in C code snippes related to "FILE", the only way to do it is to read each and every post in those tutorials. Would be useful to have a quick way to search them.

Nick Evan commented: good idea! +12
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There are lots and lots of examples here and other places on the net that show you how to read a file. I know there are also examples in your textbook, if you bothered to read and study it.

std::string line;
ifstream obj1 ("C:\\file1.txt")
while( getline( obj1, line) )
{
    // blabla
}
mrboolf commented: well said, as usual :) +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

try this:

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

int main()
{
    string name1 = "Alvin";
    string name2 = "Edward";
    cout << left << setw(20) << "Name: " << name1 << "\n";
    cout << setw(20) << "Another Name: " << name2 << "\n";

}
Alex Edwards commented: Is that your real name? =P +5
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Nobody cares.

Maybe that's part of the problem -- nobody cares enough to fix it. Lets hope the new Obama adminstration will be able to solve this financial crises. If not, then all you young people are in for an awful rude awaking at the bread lines with no job and no money. Don't bother to travel to California to get free oranges to eat like people did in the Great Depression because they tore all the orange groves out and built houses on the land.

Alex Edwards commented: You tell 'em! =) +5
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I don't know where this came from, but thought it funny.

Operator: Thank you for calling Pizza Hut. May I
have your national ID number?

Customer: Hi, I'd like to place an order.

Operator: I must have your NIDN first, sir.

Customer: My National ID Number, yeah, hold on, eh, it's 6102049998-45-54610.

Operator: Thank you Mr. Sheehan. I see you live at 1742 Meadowland Drive, and the phone number is 555-1212. Your office number over at Lincoln Insurance is 555-2302 and your cell number is 555-2566. E-mail address is Seehan@home.net. I see you're calling me from home.

Customer: Huh? Where'd you get all this information?

Operator: We're wired into the HSS, sir.

Customer: The HSS, what is that?

Operator: We're wired into the Homeland Security System, sir. This will add only 15 seconds to your ordering time.

Customer: (sighs) Oh well, I'd like to order a couple of your All-Meat Special pizzas.

Operator: I don't think that's a good idea, sir.

Customer: Whaddya mean?

Operator: Sir, your medical records and commode sensors indicate that you've got very high blood pressure and extremely high cholesterol. Your National Health Care provider won't allow such an unhealthy choice.

Customer: What?!?! What do you recommend, then?

Operator: You might try our low-fat Soybean Pizza. I'm sure you'll like it.

Customer: What makes you think I'd like something like that?

Operator: Well, you checked out 'Gourmet Soybean Recipes' from your local library last week, sir. That's why I made the suggestion

Customer: All …

Ezzaral commented: hehehe Very good! +15
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That's because you enclosed the \0 in double quotes. Change it to single quotes pPtr->myArray[127]= '\0'; If myArray is less than 128 bytes then the above will cause buffer overflow.

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

Dude -- I should give you bad rep for wasting so much of my time :)

William Hemsworth commented: He warned you in the thread title :icon_cheesygrin: +4
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

since this is c++ why use pointers? Just make name and message both std::string and that will probably solve the memory leek problemn.

Niner710 commented: Thanks for all your help. You are da man! +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Your last statement is how its normally done. Create one object then call all its methods as necessary. If you declare the methods static then you don't have to create an instance of the object in order to call the function

class A
{
public:
   static int getBeta();
};

int main()
{
    A::getBeta();
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Maybe you need to upgrade your GF to a newer model :)

Some girlfriends do not need upgrading... just proper maintenance. However; if you have chosen a model that is not compatible with you, or if you have chosen a model that is beyond your means to afford/perform the proper maintenance, then I suggest trading her in for one that is more right for you. Be careful though! Some makes/models have a self-destruct mechanism that automatically initiates when they sense you are interested in another model. If you are happy with the make/model that you have currently and are just looking to upgrade, then I suggest purchasing the Engagement/Marriage package. Again... be careful! Some makes/models are not compatible with this upgrade and lose some vital functions after installation. The functions that seem to be mostly affected are the Career, Sex and Reasonability functions.


FYI... This information could also pertain to those wishing to upgrade their current boyfriend models. Although the Marriage upgrade does not seem to create as many system errors in the Sex function after installation as much as it does in the some girlfriend models, the Career and Reasonability functions could still be adversely affected.

http://www.answerbag.com/q_view/381052

Alex Edwards commented: =P +4
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you know you want exactly 20 bytes from incoming data then your program should just wait until it gets all 20 bytes before proceeding. That means it will have to sit in a loop waiting for incoming data at the port. The input buffer should be just an unsigned char array rather than a structure.

For this to work you will have to compile with 0 packing.

#pragma pack(0)
void GetData(unsigned char* buffer)
{
top of loop
   read port
   add data to tail of buffer
   have we read 20 bytes yet
   No, then delay a bit and return to top of loop
   yes, exit loop
end of loop
}

int main()
{
    struct bitfields bf;
    GetData((unsigned char*)&bf);
}
Salem commented: Exactamundo! +23
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I would also ditch the MFC file handling classes such as CFile and CArchive, but use normal standard c++ fstreams and std::string, which are a whole lot easier to use. That assumes you are using a compiler that supports fstreams and std::string -- some embedded compilers do not.

Alex Edwards commented: Compilers that don't support fstream == fail! X_X +4
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Seek and thou shalt find.

>>How do we know the type the pointer points?
You have to declare the data type when the pointer is created. int* iPointer; >>Is it compiler specific?
No. Pointers are defined by C and C++ standards, so all compilers comply.

William Hemsworth commented: :) +4
Alex Edwards commented: Though I know it's not your style, it would've been funnier if you said "GIYF" =P +4
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I wouldn't use the terms "Old Testiment" and "New Testiment" because you will immediately get critisizm from the religious people. No point immediately discarding 1/4th the potential readership (and customers).

Alex Edwards commented: Whoo! O_O, good point! =) +4
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

bool is a variable that only contains 0 (false) or 1 (true). You use it only when you want something to be either true or false. I could also have written the while statement like this: while( done == false)

tatainti55 commented: Thanks +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Instead of just asking us to do your homework for you, post what you have done and find out how to make it work the way you want it to work.

Salem commented: Absolutely! +22
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

A static class variable is like an ordinary global variable but has scope only within the class. Like global variables, there is only one instance of a static variable regardless of the number of instances of the class.

>> it complains about missing Count in DLListNode class...
Because you also have to declare it just like a global variable, but with class scope. Here is a simple example.

#include<iostream>
using namespace std;

class MyClass
{
public:
    MyClass() {x++;}
    ~MyClass() {x--;}
    static int x;
};

int MyClass::x = 0; // <<<<< declare class static variable

int main()
{
    MyClass a;
    cout << a.x << "\n";
    MyClass b;
    cout <<  b.x << "\n";

}
mrboolf commented: Really helpful and straight to the point. Thank you :) +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Go get professional help -- we aren't qualified to give you the help you need.

R0bb0b commented: Funny, I googled help me. and daniweb isn't even in the first 5 pages +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here's how -- use setfill() and setw()

#include <iostream>
#include <ctime>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::setw;
using std::setfill;

ostream& operator << (ostream&,const tm&);

tm mytime; // not quite safe

int main(){


 //   tm mytime; // safer implementation
    mytime.tm_mday = 10;
    mytime.tm_mon = 5;
    mytime.tm_year = 2008;
    mytime.tm_hour = 3;
    mytime.tm_min = 8;
    mytime.tm_sec = 30;

    cout << mytime << endl;
    cin.ignore();
    cin.get();

    return 0;
}

ostream& operator<<(ostream& out,const tm& theTime){
    out << setfill('0') << setw(2) << theTime.tm_mon << "/" 
        << setw(2) << theTime.tm_mday << "/" 
        << setw(4) << theTime.tm_year;
    out << setfill('0') << setw(2) << "\t" 
        << setw(2) << theTime.tm_hour << ":" 
        << setw(2) << theTime.tm_min << ":";
    out << setfill('0') << setw(2) << theTime.tm_sec;
    return out;
}
Alex Edwards commented: Whoo! Thanks =) +4
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

struct tm: is just a structure that can be use like any other structure. The localtime() function takes a time_t object and returns a pointer to a struct tm.

time_t now = time(0); // get current time
struct tm* tm = localtime(&now); // get struct filled out

cout << "Today is " 
    << tm->tm_mon+1 // month
     << "/" << tm->tm_mday // day
      << "/" << tm->tm_year + 1900 // year with century
     << " " << tm->tm_hour // hour
     << ":" << tm->tm_min // minute
     << ":" << tm->tm_sec; // second

mktime: does just the opposite of localtime(). It takes a struct tm object and converts it to time_t. In addition it may also normalize the struct tm object. Lets say you want to get a struct tm and time_t for some future date, say one month from now. To do that, you first call localtime() as shown above, add 30 days to the tm_mon structure member, then call mktime() to normalize the structure members. mktime() will take care of insuring the structure contains the correct month, day and year (such as for month and year roll-overs).

difftime: simply returns the difference between two time_t objects. See example program here.

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

You can do it any way you want. You don't even have to nest the two functions like I showed you.

int n = function2(number.one, number.two);
cout << function1( n, number.two);

There are lots of ways to do what you described. Let your imagenation be your guide and try them out with your compiler to see what happens. Practice makes perfect :)

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

>>and if not, repeat. Then display the list.
Displaying the list is not a requirement as the OP posted.

You could use an array, but its not necessary because there is no need to keep all the input values. Just sum them up as they are entered. You also need another counter to keep count of the number of entries the user entered. After all done, then just do the simple math.

This is really a very simple program that requires no (zero) advanced things such as <vector>, arrays or memory allocations. Don't make this more difficult than is needed.

VernonDozier commented: Good observation. +8
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

when do you mean buy "why won't it work ...". Do you take your car to the auto repair shop and tell them "its broke -- pleas fix it." ? We need a lot more information.

In the code you posted, line 6 is returning the 6th element of the array, but line 8 only allocates an array with 3 elements.

CPPRULZ commented: it was true and helpful +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Check to see if the number is already in the list before adding it. The delete() function is not the place to do that. Write another class method that does nothing but search the list for a number, return true if the number already exists in the list or false if it doesn't. Then if it doesn't call that Insert() function.

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

>>if(people.lastname == lastname)
You can't compare two character arrays with the == operator like you do two std::strings. To compare character arrays call strcmp() functions. if( strcmp(people[i].lastname == lastname) == 0)

stilllearning commented: yup, I just realized that myself +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You mean you can't read minds?

DaniWeb newbes have asked me to do that lots of times, but I don't have a crystal ball nor is my eyesight good enough to see their monitors from where I am sitting.

R0bb0b commented: indeed +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

To be considered what?

Ezzaral commented: My exact thought :) +12
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>meanwhile the main problem is finding out how to include the header file

But the code you posted does it correctly #include <iostream> is all that is needed. The the compiler starts reading your program the #include statement causes it to open up the include file and add its contents to the *.cpp file that is being compiled. That is part of the preprocessor operations.

You are probably getting compile error messages on pen.h because you forgot to add using namespace std; after including <string> or you can use std::string Brand; to declare the namespace.

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

>>but I dunno if its allowed here.
We won't have a problem with it. But think -- do you want other students and your teachers finding that code here on the net ? Don't post it unless you are sure you and your school are ok with that.

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

>>Number 7 has two correct answers
No -- 5.9875e17 can only be stored in a double -- floats are too small.

VernonDozier commented: Good catch. +7
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Now this page is even more interesting!!

http://www.computerhistory.org/internet_history

w0w,back to the 60s!! (I had forgotton about Arpanet)

That link is all just bs. Everyone knows Al Gore invented the internet.

Alex Edwards commented: Funny comment =P +4
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>start = std::clock(); //reset clock
You can't use namespaces in *.c files.

Nick Evan commented: Oh duh... What the hell was I thinking? +10
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

OMG either you lied like hell to land that job or your employer is pretty darned stupid for hiring you in that position.

>>what causes do u think i need to do, to be able to achieve this.
Quit that job and go to college full time to earn a bachelor's degree. You can't learn IT overnight.

stephen84s commented: ROFL ............. :D +3
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

No, getline doesn't work with STL string.

What! Who the hell told you such a silly thing. There are two versions of getline() -- one works with std::string and the other works with character arrays. And for character arrays your example is 100% wrong! What you posted is the syntax for std::string, not character arrays.

// getline using std::string
std::string textstring;
std::getline(file, textstring);

// getline with character array
char textstring[255];
file.getline(textstring, sizeof(textstring));
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

His wife needs an attitude adjustment. My wife always told me "Only dead people stop looking -- looky but don't touchy."

Ezzaral commented: ++ +12