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

first you need to declare an array of integers that represent the count for each characters -- since there are 26 letters in the English alphabet ('a' - 'z]) you will need an array of 26 integers.

int counters[26] = {0};

The abover declared the array and initialized them all to 0.
Next step, after entering the sentence, increment the counters array for each character in the sentence. Loop through the sentence and increment the array element that corresponds to the character. To get the index value, subtract the letter 'a' from the character. Example: if sentence == 'a', then 'a' - 'a' == 0. You could look up the ascii value for 'a' and use that, but its a lot easier just to use 'a'.

char sentence[80];
for(int i = 0; sentence[i] != 0; i++)
   counter[sentence[i]-'a']++;

you should be able to finish the rest of the program yourself.

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

yes getline is apart of the standard library.

I thought getline was in string.h.

NO! string.h is C, not C++. its declared as I posted above. C uses gets() and fgets(), not getline(). and both of them are declared in stdio.h, not string.h.

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

saying something is part of the standard c++ library doesn't mean it is declared in <stdlib> -- and there is no such header file. you probably mean <cstdlib>, which was derived form C language.

getline() is a method of std::string class and (I think) iostream.

#include <iostream>
#include <string>

int main()
{
  std::cout << "Enter someting" << std::endl;
  std::string line;
  std::getline(std::cin,line);
}

or

#include <iostream>

int main()
{
  char line[120];
  std::cout << "Enter someting" << std::endl;
  std::cin.getline(line,sizeof(line));
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

now fp suppose to carry the filename, am I right?

Wrong. fp is a pointer to a structure that contains information about the file -- that structure does not contain the filename. Look in stdio.h and you can see what that structure contains, although you may not understand the purpose for all structure members.

so if I used

cout << "the file name is "<< fp << endl;

is suppose to print the filename hi.txt , but actually it prints on screen 00478998.
why is that.

Because fp is not a pointer to the filename, but a pointer to FILE structure.

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

rewind() does nothing more than seek to the beginnng of file. In C you can accomplish the same thing with fseek() function. In C++ fstream use seekg(0,ios_base::beg) or seekp(0,ios_base::beg)

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

yes, it can be similated in C with structures. Its not quite the same concept as inherentence, but pretty close. And c++ class methods are not tied to any specific instance of a class -- they are more like static c++ methods.

struct Lightwave
{
  // blabla
}


struct Modeler
{
  struct Lightware lw;
  // blabla
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

It is not necessary to actually store the data in a vector or some other array. Just keep track of the min and max values as the data is read from the file.

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

[edit]I deleted my comment[/edit]

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

Your point being? There was never any assumption that the macro would be used with narrow string variables, unless you were reading a few posts that I conveniently missed. Or are you just posting pointless fluff to make yourself look smart when you can't find anything meaningful to add?

getting pretty snotty now aren't you :twisted: or are you suffering from pms today :mrgreen:

you might (or might not) be supprised at the number of people I've see try to do what I mentioned, so its not an unreasonable thing to warn people about.

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

you can use that macro anywhere you use a string literal. You can't, however, use that macro with a char*

// this will NOT work
char* hello = "Hello World";
wchar_t thello = UNICODE(hello);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That's already been answered in this thread -- just read some of the earlier posts (#13 and #14). Use sprintf() strftime to format the string.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
month= newtime ->tm_mon +1;
year=newtime ->tm_year + 1900;

don't forget the above adjustments!

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

if the file isn't too big, stat() will return its size, among other file info. you don't have to open the file to use it.

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

never never include *.cpp files inside othere *.cpp files. The *.cpp files should be compiled separately and both linked together. The header.h file should be included in both *.cpp files

// header.cpp
//
// system includes
#include <iostream>
// local includes
#include "header.h"

// header implmentation code
//
// blaba
// main.cpp
// header.cpp
//
// system includes
#include <iostream>
// local includes
#include "header.h"

int main()
{
   // blabla
}

now depending on the compiler you should have a project that includes both *.cpp files and links them both along with other system libraries to produce the final executable program.

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

At the top of the program you prototyped printdate() without any parameters. In c++ (unlike C) that means it takes no parameters. Correct the error in the protytype

void printdate(time_t d) ;

and read Dave's post about quotes!

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

what and where is the error? You need to include time.h like you did stdio.h

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

I don't know about CreateRemoteThread -- never used it.

you probably won't find any "tutorials" because there isn't that much to it. Most of the parameters are 0, unless you want more control over the thread.

First create the thread itself -- name it anything you want. If it is a member of c++ class it must be static method.

DWORD WINAPI ThreadProc( LPVOID lpParameter)
{
  // blabla

  return 0;
}

now create an instance of the thread in your program

DWORD dwThreadID;
HANDLE hThread = CreateThread(0,0,ThreadProc,0,0,&dwThreadID);

The parameter immediately following ThreadProc is a parameter you want to pass to the thread. So if you want to pass the this pointer (assuming c++ program)

DWORD dwThreadID;
HANDLE hThread = CreateThread(0,0,ThreadProc,this,0,&dwThreadID);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Assuming you have a 32-bit version of the compiler, google for ODBC and you will get lots of information. ODBC is the most common way to access any database. There are several free c++ classes on MySql web site and www.codeproject.com. You can access ODBC from either C or C++, but I don't know how much information there is for just C.

You also need to study the SQL language -- google for that too.

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

there is no guarentee that a short will occupy 2 bytes of memory. The only thing we can be sure of is that char <= short <= int <= long. Notice that on some os all could be the same size.

On operating systems where short is 2 bytes and int/long are 4 bytes, using short will save 2 bytes over using int or long. However it may not be possible to use short if the value you want to store will not fit in a short.

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

I must be near-blind or something because I don't see any of your code where you attempted to solve that problem youself :evil: Don't let that overwhelm you. Take the problem one step at a time and you will be able to solve it.

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

read my post #4 verrrrrry sloooooooooooooowly and careeeeeeeefullllllllllly. I already covered that topic. pay close attention to what I said about strstr() and the necessity to check for whitespace both before and after the string you want to find.

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

yes Dave, sorry for the error.

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

strcmp() return 0 if the comparison succeed, or non-zero if it fails.

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

what do you mean by "it doesn't work"? what doesn't work? strcmp() compares the entire line with the text you put in literals, in your example it is "int". So if the line is "int some more stuff here", then strcmp() will fail. If you only want the first three characters, the use strncmp(), or if "int" can appear anywhere in the line use strstr(). Also you have to be careful with the comparison because strstr() will also find "intabc" is the same as "abcintabc" and " int ". So if strstr() finds the text "int" the program should check for space character immediately before and after. If there is no white space before and after, then strstr() did not find a word.

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

it should work with text files of any size. Are any lines longer than 1,000 bytes? What is the purpose of checking if one of the lines = "int" ? Is there anything else following "int" on that line?

This is a c++ program -- why don't you use std::string instead of c-style arrays?

#include <string>
...
std::string line;
int v_word = 0;
int c_word = 0;
int spot;
ifstream file("c:/test.txt");

   if (file.is_open()) 
     {
	while (getline(fin,line) )
		if( (spot = line.find("int")) >= 0)
		  v_word++;
     }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

use functions in time.h -- today() returns the current date as an integer, number of seconds since 1 Jan 1970 (I think). Then call localtime() which returns pointer to struct tm. That structor contains individual fields for month,day,year, hour, ... You need to know how to use pointers and structures to make use of that information. If you don't, then study up on them in your programming book.

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

why didn't you answer it? Its not that difficult. If you know what today is, then just use sprintf() to format the filename

int day_of_month = 10;
char filename[255];

sprintf(filename,"day_%02d.txt", day_of_month);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

can't help you at all if you don't post the code you created.

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

Hi Lerner
Thank you for the declaration, but the problem still with the definition of the dynamic array, because it still when running the code gives blank screen and sending error report everytime.

what you posted is incomplete -- it contains no code to read the csv file! All that program does is allocate the array -- nothing more.

I'm really supprised your program didn't crash on that last loop that calls atoi() because the array contains uninitialized strings that contain random data. atoi() expects a null-terminated ascii string, it searches through memory until it finds either the first non-digit character or null-terminator. But since the strings are uninitialized -- garbage in, garbage out.

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

very simple -- if you have the HWND of the button

// m_hWnd is the handle of the dialog
HWND hWhd =  GetdlgItem(m_hWnd,IDC_BUTTON1);
EnableWindow(hWnd,FALSE);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Aldin: your cout statement is wrong. Load that log.csv into Excell and you will see that the last column contains all 0's.

while( getline(in,line) )
		{
			LINE ln;
			int count = 0;
			lineno++;
			while( (pos = line.find(',')) >= 0)
				{
					string field = line.substr(0,pos);
					line = line.substr(pos+1);
					ln.push_back(field);
				}
		ln.push_back(line);
		cout << line << endl;
		array.push_back(ln);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

string field = line.substr(0,pos);
line = line.substr(pos+1);
ln.push_back(field);

That isn't necessary -- the value of pos at that place is -1, so all that substr is doing is setting field = line. You can reduce the above to just one line, like this:

ln.push_back(line);

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

getline() does not append the '\n' to the end of the string like fgets() does. If the last column is missing, then you probably need to add final n.push_back(field); after then end of that loop.

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

CView and CFormView are MFC c++ classes. But you need to use VC++ 6.0 or newer to use them. Call the win32 api function EnableWindow() to enable and disable buttons and most other windows too.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
string GetColData(int row_number,string ColName);

Do you mean that line? It is not call the function -- that is a function prototype. If you want to call it you need to do something like this:

int row_number = 5;
// get value in cell at row 5, column named "Fred"
string value = GetColData(row_number,"Fred");
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

move return "" down one line, just above the last close brace.

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

And I thought you guys didn't have anything left to learn :lol:

:cheesy: :cheesy: you can always teach an old dog new tricks. writing c and c++ is a life-long learning experience. Those college degrees only give you the tools to begin learning.

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

make sense...I started out helping, ended up learning ;)

I do that a lot too :lol:

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

no because the address is always copied into theArray, so deleting theArray will also delete the memory that was allocated for ptr.

Also, I believe it should be:
ptr = new Dog();
instead of
ptr = new Dog;

Either way is ok -- I normally do not use parentheses when there is nothing to put in them.

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

You shouldn't need the for loop, should just be able to do this once:

delete [] theArray;

that will not work in his program because theArray is an array of pointers, which are allocated separately, so each one has to be deleted separately.

Mammel destructor is always called because it is the base class of all other children. so it has to destroy any allocated memory that it may have created. If it was not called then there might be a memory leak.

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

vectors are similar to normal arrays that relieve you with all the hassel and mistakes of memory allocations. The 2d vector is a vector of vectors. The inner-most vector contains the strings for each line, and the outer vector is the array of each line. So to get column 0 of row 0

vector<LINE> array; 

// get row 0
LINE& row = array[0];
// get column 0
string& s = row[0];

The first row (from your data file) contains column names, so the above will get the name in column 0. There are other tricks to get a column based on its name instead of a column number -- if you ever add or subtract columns in the data file, using column names instead of numbers makes your program more portable, but slightly slower to process.

string GetColData(int row_number,string ColName)
{
LINE& columnNames = array[0];
// find the collumn with the above column name
for(int col = 0; col < columnNames.size(); col++)
{
   if( ColName == ColumnNames[i] )
   {
        // get data for this column
         LINE& row = array[row_number];
        return row[col];
   }
   return "";
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

also, add destructors to each class and you will see how they behave too

#include <iostream>
using namespace std;

class Mammal
{
public:
	Mammal():itsAge(1) {}
	virtual ~Mammal() { cout << "Mammel destructor" << endl;}

	virtual void Speak() const { std::cout<<"Mammal speak!\n";}

protected:
	int itsAge;
};

class Dog : public Mammal
{
public:
	virtual ~Dog() { cout << "Dog destructor" << endl;}
	void Speak() const { std::cout<<"Woof!\n";}
};

class Cat : public Mammal
{
	virtual ~Cat() { cout << "Cat destructor" << endl;}
	void Speak() const { std::cout<<"Meow!\n";}
};

class Horse : public Mammal
{
	virtual ~Horse() { cout << "Horse destructor" << endl;}
	void Speak() const { std::cout<<"Hihihi!\n";}
};

class Pig : public Mammal
{
	virtual ~Pig() { cout << "Pig destructor" << endl;}
	void Speak() const { std::cout<<"Oinkoink!\n";}
};

int main()
{
	Mammal *theArray[5];
	Mammal *ptr;

	int choice, i;

	for( i = 0; i < 5; i++)
	{
//		std::cout<<"(1)dog (2)cat (3) horse (4) pig: ";
//		std::cin>> choice;
choice = i+1;
		switch(choice)
		{
		case 1:
			ptr = new Dog;
			break;
		case 2:
			ptr = new Cat;
			break;
		case 3:
			ptr = new Horse;
			break;
		case 4:
			ptr = new Pig;
			break;
		default:
			ptr = new Mammal;
			break;
		}

		theArray[i] = ptr;
	}

	for( i = 0; i < 5; i++)
		theArray[i]->Speak();

	for( i = 0; i < 5; i++)
		delete theArray[i];
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

brackets are in the wrong place

for( i = 0; i < 5; i++)
     delete theArray[i];
JoBe commented: Very helpfull person!!!! +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

look at the code -- it doesn't print anything on the screen. All it does is read the csv file and put the strings into an array. What you do with it after that is up to you because I have no idea what you want (my crystal ball isn't working today).

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

here is a c++ version that puts the strings into a 2d vector

#pragma warning(disable: 4786) // VC++ 6.0 disable warning about debug line too long
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
typedef vector<string> LINE;

int main()
{
	string line;
	int pos;
	vector<LINE> array;

	ifstream in("log.csv");
	if(!in.is_open())
	{
		cout << "Failed to open file" << endl;
		return 1;
	}
	while( getline(in,line) )
	{
		LINE ln;
		while( (pos = line.find(',')) >= 0)
		{
			string field = line.substr(0,pos);
			line = line.substr(pos+1);
			ln.push_back(field);
		}
		array.push_back(ln);
	}
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That identical set of questions has been posted before. google for the first question and you will find the answer(s).

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

make a minor change and it will be easier to use and see what is going on.(you don't have to type all those numbers every time you run the program.)

int main()
{
	int a[2][3] = {0};
	int *ptr = *a;
	int count = 1;
	for(int i=0;i<2;i++)
	{
		for(int j=0;j<3;j++)
		{
			a[i][j] = count++;
		}
	}
	for( i=0;i<2;i++)
	{
		ptr=ptr+i;
		for(int j=0;j<3;j++)
		{
			cout<<*(ptr+j)<<endl;
			
		}
	}
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
int main()
{

	int arrayA[5] = {1,2,3,4,5},
		arrayB[7] = {1,2,3,4,5,0,0},
		arrayC[5] = {1,2,3,4,5};
	int //copyFrom,
		*ptr = 0,
		subtract = 0,
		*p = 0;

	Array nums;
	Array();<<<DELETE THIS!
	//Array(Array& copyFrom);
	nums.values(arrayA, arrayB, ptr, p);<<this is wrong

The last two parameters to values function must be pointer to an integer, not an int pointer. values() is attempting to change the value to which the pointer points, but you sent it a NULL pointer that references a non-existant memory address.

int a, b;
	nums.values(arrayA, arrayB, &a, &b);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

why wait for anyone to tell you -- try it yourself and find out.

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

It appears that you may be using Microsoft Visual C++ -- all their errors are explained at www.msdn.microsoft.com. Just enter the error number in the search block.

error C2084: function '__thiscall Array::~Array(void)' already has a body

That means the destructor has already been coded, either as an inline function in the header file or in the implementation file. In your case it was already defined in the header. If you want it in the implementation file you have to remove the braces shown in RED below.

~Array() { };

When the function requires an array, all you have to do is put the array's name in the parameter list, as shown in main() in this code snipet.

int foo( int array[] )
{
  // do something
}

int main()
{
   int array[10];
   foo(array);
}