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

The problem occurs when you try to use that stack class to maintain doubles instead of pointers. It doesn't work because push() expects a pointer and not a double. It would make more sense for the stack class to maintain pointers to instances of the entire records structure instead of its individual members. Then whoever calls pop() will get a pointer to a records object and can do with it whatever it wants.

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

your program has at least two problems I can spot

int push(void *myobject) 
{
records * layer= (records *) myobject;
}

What is the above supposed to do? all it is doing is typcasting myobject from void* to records*, then does nothing with it. Its a "do-nothing" function.

void* pop(void) 
{
return layer;
}

Above does not do what you probably hope it does -- variable layer is an array of pointers and that pop function is returning the entire array. It is probably only supposed to return the current element in the array.

Look at the pop() function I posted to see how that should be accomplished.

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

there are two formas of function getline -- one for c-style character arrays and the other for std::string objects. You are trying to use the incorrect one. Here's what you need:

getline(cin,playerOne);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I thik below is the correct implentation of that class. One problem with this implementation is that the class is attempting to manage a list of objects that it does not own, and if some other function decides to delete one of the objects it will invalidate the pointer in the stack class. This can potentially cause a lot of grief, cursing and hair pulling by the programmer.

You will want to add some error checking in the push and pop functions to make sure variable current is within range.

class stack { 
private:
	int top; 
	int current;
	void *layer[100]; 
public: 
	stack(void)
	{
		top=0; 
		current=0;
	}
	int push(void *myobject)	{
		layer[current++] = myobject;
		return current-1;
	}
	void* pop(void) 
	{
		return layer[current--];
 
	}
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>scanf ( "%s", &rainfall );

%s wants a character array of at least 2 bytes. rainfall is not a character array, so the above will always fail to work correctly.

char rainfall[2]; // 1 byte for either Y or N and secnd byte for null terminator
scanf ( "%s", &rainfall );

There are probably other problems, but I stopped reading after that line.
[edit]barometer has the same problem. Where do you declare variables storm etc ? Post them too.[/edit]

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
printf("%s",records[i].M_F == 0 ? "Male" : "Female")
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

all c strings are terminated by 0. strlen() only returns the number of characters in the string so you have to add 1 more for the null terminator. If you don't very nasty things tend to happen to programs (been there, done that!)

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

>>param[parateller] = malloc(sizeof(midl));

The above is also wasteful -- should allocate the length of the string + 1

param[parateller] = malloc(strlen(midl)+1);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If I understand you right, you want to split the sentence "Hello World" into an array of two strings -- "Hello" and "World". Assuming variable midl is just a temporary buffer to hold the current word, you do NOT want to use strcat() to copy the ith character into it.

int n = 0;
		for(i=0; i < (strlen(text)); i++){

			printf("Midl: %s\n", midl);

			midl[n++] = text[i];

			if( isspace(text[i]) ){
                                      midl[n] = 0; // null-terminate the string

				param[parateller] = malloc(sizeof(midl));

				strcpy(param[parateller], midl); 

				printf("Parateller: %d\n", parateller);
                                      n = 0;
				parateller++;


			}

That should handle the words before end-of-string is found, but not the last word. You need another check after that loop terminates so that you pick up the last word in the line.

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

What is causing this, and how do I avoid it?

This line is causing it

strcat((char *)&midl, &text[i]);

how to avoid it? I can't say because I don't know what you intend for it to do.

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

Turbo C??? I thought that was pretty bad 20 years ago!

At one time it was one of the best MS-DOS compilers on the market -- Microsoft compilers really sucked cannel water in those days and NOBODY used their IDE. That was when Lattice C was king of the hill.

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

ios646.h is apparently available in the C99 C standards. But I don't know how many compilers have implement those new C standards yet.

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

I guess I'm fortunant because I'm 63 and still have a full-time well-paying job. I have to wait until I'm 66 before I can collect full SS and I've thought about what to do if I do lose my job before then. Well, now I know.:mrgreen:

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

>>but can anyone write some code in C using if statements for the following:


No! write it yourself, post code and ask questions. we do not do your work for you.

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

yes, I knew that would happen. Please look at my post very carefully because I showed you the correction -- two '\' characters.

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

use the escape sequence character. and to get the '\' you need two of them

sep = strpbrk(line, "\\<=->/'\"");
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

hmm, i dont really get it. so i have to malloc each of the array (ie using loop, allocate space for each structure in the array)?

int i = 0;
for (i = 0; i < MAX_NUM; i++) {      //MAX_NUM == array size
array[i] = (StatusPointer) malloc (sizeof (struct status));
}

Yes like that. Unless your assignment requires you to use pointers -- why use them? just statically allocate all those structures and it will cause fewer problems.

struct allStatus {
struct status  array[MAX_NUM];
};

and also, should i put it before group1 mem allocation, or after ?

memory must be allocated from the outermost structure to the innermost -- and free'ed in the opposite order.

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

that is the right syntax, but it does not allocate enough memory because array inside the allStatus structure is an array of pointers and each of them must be malloc'ed as well.

[edit]Sorry Glorious -- your post was not there when I started writing the above[/edit]

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

why that doesnt work?


#include <stdio.h>

int main ()
{
char lala ;
printf ("Enter string:");
scanf("%s", &lala);
printf ("%s\n", lala );
return 0;
}

1. please use code tags as shown in ~S.O.S~'s example.

2. you did not declare variable lala correctly -- study ~S.O.S~'s example and pay close attention to how he declared the character string.

3. NEVER usr scanf() -- it is harmful to your program's health because it will let you type in a string that is larger than the buffer you created for it, such as in your example. Use fgets() instead -- again see ~S.O.S~'s example.

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

you don't really need sprintf at all. just use fprintf()

int cust_num = 123;
fprintf(fpr, "%d\n", cust_num); 
// now if you want the number to have at least 5 digits, with leading 0s when needed
fprintf(fpr, "%05d\n", cust_num);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Actually, you read from one file, then write to another file. You don't want to read and write to the same file. It gets too complicated.

I did not suggest he write to the same file that he's reading. read the sudo-code again please -- "write to new file"

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

aha !
if i want to write to a file, do i have to fread it first or just
fwrite it straight away ?
thx champ

of course the program has to read it -- how else do you think it is going to be able to know where the commans are and what to write to the new file?

for each line in the source file
   read a line
   replace all commas with semicolons
   write the string to the new file
end
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

use strchr() to find the comma then replace it

char str[] = "Hello,World";
// find the comma
char *ptr = strchr(str,',')
// replace it if found
if(ptr != NULL)
   *ptr = ';';

Now, put the above in a loop to find and replace all the commas in the string.

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

you can not use the name of functions as the name of a variables. Function names must be different from variable names. Example: residential_rate is used both as a function name and a variable name, and since the variable name is in _tmain() the compiler is treating that symble as a float, not a function.

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

you can use strtok() to extract the individual fields.

while( fgets(buf,sizeof(buf),fp) != NULL)
	{
		rSales[i].number = atoi(strtok(buf,","));
		strcpy(rSales[i].item, strtok(NULL,","));
		strcpy(rSales[i].partnum, strtok(NULL,","));
		rSales[i].cost = (float)atof(strtok(NULL,","));
		strcpy(rSales[i].day, strtok(NULL,","));
		++i;

	}

[edit] you might want to read Dave's links from this post[/edit]

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

>> i didn't get any reply
I don't recall seeing another post -- did I miss it?

1. is that a C or a C++ program? You include both stdio.h and iostream, yet you use C file i/o functions. Make up your mind which kind of program you want and stick to it.

2. the code does not even compile cleanly. before attempting to answer your question you need to fix all the compile errors, then repost the code.

3. next time please use proper indentation -- the code is terrible to read as it is now. I hope you don't write all your programs like that.

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

>>Okay, Iv'e finished learning C++.

:cheesy: :cheesy: :cheesy: :cheesy: :cheesy: :cheesy: you will NEVER be finished learning c++. That is a life-long task.

If you want to learn MS-Windows GUI programming here is a good introduction. Its c instead of c++ but then so is all the win32 api functions (not one of them are c++ because they can be called from many different programming languages).

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

also

int main()
{
// this is only a 1d array of doubles because each
// salesman has only 1 row in the data file.
double arraySales[ARRAY_SIZE];
// the next array needs to be 2d because it
// is an array of strings
char arrayNames[ARRAY_SIZE][25];

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

why are those double and int arrays 2-dimensional??? Arrays must have a name. There are a couple ways you can to it. Leave the array size unspecified and pass the size as another parameter, as in your original attemp.

void PrintArray(double array[], int array_size);
void BubbleSort(double array[], int array_size);
void GetNames(double array[], int array_size);

or just not pass the size as a separate parameter, like this

void PrintArray(double array[ARRAY_SIZE]);
void BubbleSort(double array[ARRAY_SIZE]);
void GetNames(double array[ARRAY_SIZE]);

Isn't the array passed to GetNames() supposed to be an array of strings? Names normally are not arrays of doubles

void GetNames(char array[ARRAY_SIZE][25]);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

please edit your post and insert code tags so that my eyes will not hurt so bad when I read your code.:eek: :eek:

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

since you can't even use the keyboard I see no other option but to reinstall the os from CD unless you have a backup of the registry on another disk.

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

>>I was thinking of making the while statement a function and calling it with main

Yes, that would be my suggestion too.

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

A union is just a structure in which all objects occupy the same space. For example

union 
{
   int a;
   char buf[20];
   float b;
   double c;
}

In the above, the union has only one real object but it can be used by any of the names provided, such as a, b, c or buf. The size of the union is always the size of its largest object, 20 in this case. So what you need to do is make a union between the INSTRUCTION strucure and an unsigned long integer.

uinion
{
    INSTRUCTION ins;
    unsigned long ln;
};

Since sizeof(INSTRUCTION) is the same as sizeof(ln), whatever you set ln to be will be reflected in the ins structure. So if you set ln = 0x8800080000004D2, then you can get the individual bits by using the ins structure.

leonpwhirl commented: helpful in answering questions. +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you didn't say what operating system you are using. XP is on a bootable CD, so just insert that CD and reboot your computer. Then reinstall the os on top of the current os.

Next time, back up the registry before you start screwing around with it.

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

the best solution to the problem is to always run a good antivirus program and to not visit risky web sites such as those porn sites.

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

you can't just convert the int by typcasting because (1) an int is a non-printable value and (2) the value of the integer may not be in the range '0' to '9' (single digit value). The safest way is to use sprintf() to format the string with an integer. I would suggest creating another temp char buffer to format the integer.

char tmpbuf[40];
sprintf(tmpbuf,"%d", length);
// now copy tmpbuf into buffer just like you
// do the other strings

Of course, if you absolutely know the integer is a single digit, then just add '0' to it.

len_char = (unsigned char) (length + '0');
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

look at the structure -- credit is an array of floats and you are attempting to use it as if it were a single float object. Change the structure to remove the arrays

struct cgrade
 {
    char course[50]; // room for 50 character course name
    float credit;
    float grade;
 };

>>gets(GPA.course);
never, ever use gets() function because it can cause your program to crash if you enter more characters than the input buffer can hold. Use fgets() instead

fgets(GPA[i].course, sizeof(GPA[i].course), stdin);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

making that change and the program runs without a problem. Don't know if the output is what you want or not though.

My compiler is not c99 compilant, so I use new to allocate the arrays, but the output should be the same.

sb
(?:(?:[a-z90](?:-?[a-z90]+)+)(?:\.(?:[a-z90](?:-?[a-z90]+)+)){0,4})

i
((15[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[0-9])(?:\\.(15[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[
0-9])){3})

dm
(?:(?:[a-z90](?:-?[a-z90]+)+))

h
((??:(?:(?:[a-z90](?:-?[a-z90]+)+)(?:\.(?:[a-z90](?:-?[a-z90]+)+)){0,4})\.)?(?:(
?:[a-z90](?:-?[a-z90]+)+))\.([abcd]{2,4})|((15[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[0-9
])(?:\\.(15[1-4]|1[1-4][1-9]|[0-1]?[0-9]?[0-9])){3}))(?:\:(0-9]+))?)
Press any key to continue . . .
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The problem is classic case of buffer overrun.
char i[iNumLen + 1];
The above does not allocate enough space for the result string.

Boost libraries contain a regular expression c++ class. It might be worth tossing out this poort c implementation and using boost.

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

No it works, i tried it.


Why i need to use

char*

is because windows functions only accept char*

yes they do, and std::string's c_str() returns a const char*. You need to tell us the win32 api function you are attempting to call. But general rule of thumb is that when the parameter is const char* then you can pass std::string's c_str() method. If it is char*, that normally means the function might attempt to change the string, so you have to pass it a char buffer that is modifyable, and not just some character pointer.

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

what do you mean?

I guess you didn't read my previous post. casting only makes it compile -- that doesn't mean it will actualy work. Did you test your program, something similar to what I posted? If not, then you are in for a big supprise.

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

Mr. Dragon, wont this work or do the same trick or the const nature of the returned null terminated char array pose some problems ?

It will compile but displays garbage at runtime because the string returned by substr() is only temporary object which must be copied someplace else such as another std::string object or char array.

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

int main()
{

	string str = "Hello World";
	const char *p = str.substr(2,4).c_str();
	cout << p << endl;
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

std::string.substr() returns another std::string.and you can't mix C and C++ like that. you would need to allocate space for the substring

char * str;
string str2 = "hello";
string str3; 
str3 = str2.substr(2,4);
str = new char[str3.length()+1];
strcpy(str,str3.c_str());

C isn't nearly as kind to you as c++.

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

there is no standard naming convention that all programmers use, normally when you go to work for a software house the company you work for will have a set of standards that you must follow. Lacking that, you can use whatever you like.

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

name even one case where the programmer has control over gets(). The standards committee probably left it in because taking it out might break a lot of programs that already use it. The statement you quoted is just a bunch of frabridated bull****.

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

>> there are no "always do" or "never do" rules

Never ever use gets():mrgreen:

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

speed is sometimes much more important than modularity or maintainability. I worked several years on a program that had to get data real-time from barcode readers on packages as the packages moved along an assembly line. Then the program had to look-up data in a database whose key was the barcode, format the data, and send it to a large-character printer that would print information on the side of the packages when they moved past the printer. All that had to be done in less than 1/2 second and management thought that was too slow. We even resorted to assembly language to squeeze out every millisecond we could.

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

First, the result is undefined, so all bets are off when you come to...

you mean the result of adding two numbers whos sum will not fit in the integer is undefined. Of course this is not a total disaster -- just use an integer that can hold larger numbers, which you would be forced to do if you were writing a checkbook program for Bill Gate's back account:cheesy: In that case, c and c++ would not be the desirable computer language. Fortran or C# might be better.

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

Yes, I will agree there are some cases where my comparison function doesn't work right -- when either or both numbers are negative. Its not due to some exotic bug or problem with integer overflow but an elementry property of math (subtractiing two negatives is really adding them and that will result in a value with the wrong sign returned to qsort). So my quick comparison function does not work for all values. S.O.S.'s function is better afterall .:)

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

Actuall I had forgotton about that discussion -- but if subtracting two integers were really that much of a problem then no computer program would work. So I just dismiss it as a non-issue and non-problem. It isn't really worth the effort to even think about -- wasts too many brain cells (and I need all I can get). It might only be a problem in some remote unlikely os that nearly nobody uses.

returning the difference between two integers will work correctly in probably 99.999999% of all operating systems. So who reall cares whether it may or may not result in some obscure problem. I don't stay awake at night thinking about such problems. And neither should anybody else.