>>Why do folks assume that old is bad?
My wife is getting old too, but she's still in good working order and I intend to keep her around for awhile. Already have her broke in, getting a new wife means I have to start all over again. :)
>>Why do folks assume that old is bad?
My wife is getting old too, but she's still in good working order and I intend to keep her around for awhile. Already have her broke in, getting a new wife means I have to start all over again. :)
start at the beginning, not somewhere in the middle of things.Here is a good tutorial to get you started.
>>n y is c++ a dead compiler
Now, what the hell does that mean? n could stand for any of several dozen words in the dictionary that begin with the letter n. y could also. Those are two very very good reasons people should spell out words -- I can understand bad english, but not that kind of crazy abbreviations. I hope you don't write like that in high school or college English classes!
what compiler (and version) are you using? What is (are) the error messages?
after including the C++ stl header files you need to declare the namespace
#include <iostream>
... other header files here
using namespace std;
If you are running Norton Antivirus turn it off during development. Sometimes it causes problems like that. Don't forget to turn it back on when you are done.
suggestion: read the 100 numbers from a file instead of entering them from the keyboard.
#include<iostream>
#include<iomanip>
using namespace std;
//function prototypes
void initialize (int numbers[], int listSize);
void readNum (int numbers[], int& listSize);
void selectSort (int numbers[], int listSize);
int main()
{
int Numbers [100];
int count= 0;
int listsize = 100;
int temp;
initialize (Numbers, listsize);
cout <<"Enter a maximum of 100 positive integers ending with -999"<<endl;
readNum (Numbers, listsize);
cout <<endl;
cout <<fixed<<showpoint<<setw(7)<<"Number"<<setw(9)<<"Count"<<endl;
selectSort(Numbers, listsize);
temp = Numbers[0];
count = 1;
for (int t=1; t< listsize; t++)
{
if(Numbers[t] == temp)
count++;
else
{
cout << "temp = " << temp << " total = " << count;
temp = Numbers[t];
count = 1;
}
}
// display count of final number
cout << "temp = " << temp << " total = " << count;
return 0;
}
void initialize (int numbers[], int listSize)
{
int index;
int Num = 0;
for (index=0; index < 100; index++)
use listSize in above loop
numbers[index] = 0;
}
void readNum (int numbers[], int&listSize)
{
int index =0;
int num =0;
listSize=0;
You don't need variable index. use listSize instead
while (num != -999)
{
cin >>num;
numbers[index] = num;
index++;
listSize++;
}
}
void selectSort (int numbers[], int listSize)
{
int index, minIndex, smallestIndex, temp;
for (index=0; index < listSize - 1;index++)
{
smallestIndex = index;
for (minIndex = index + 1; minIndex < listSize; minIndex++)
if (numbers[minIndex] < numbers[smallestIndex])
smallestIndex = minIndex;
temp = numbers[smallestIndex];
numbers[smallestIndex] …
Hello anicent dragron, the code you posted doesn't work on my compiler. How do I make it work? :sad:
see Narue's post.
>transform(myString.begin(),myString.end(),myString.begin(),toupper);
Amazingly enough, that's correct, but I'm not sure if it's correct because you intended it to be or if it's correct because you messed up your headers.
some compilers may have problems with the code I posted for the reason you mentioned (ambiguity). The code you posted is indeed more portable between compilers.
I use Microsoft compiler(s) and the code I originally posted works with them.
u can use
for (i=0;s[i]!='\0';i++) s[i]=toupper(s[i]);
that may not work because std::string is NOT a null-terminated string. It does not use 0 to determine end of string.
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string myString ("abcd");
transform(myString.begin(),myString.end(),myString.begin(),toupper);
cout << myString << endl;
cin.ignore();
return 0;
}
1. put all the numbers in an array and short it.
2. create a 2d array, the first dimension contains the number and the second dimension contains a count of the number of times the number appears. The program will have to iterate through the original array (#1 above) and maintain each array in #2.
c++ <map> will facilitate #2 above, but I'm not familiar enough with it to demonstrate its use.
The Express edition will be free for only about a year -- after that people will have to pay the $54 or so for it.
Since you want to spend your money, buy Pro edition. The Express is FREE -- you can't buy it.
you can't contantinate C style character arrays like that. Create a third buffer that is _MAX_PATH bytes long (if you are using MS-Windows os, I don't know what it is on *nix), then use sprintf() to create the string. Remember the format string uses "%s" for strings and "%d" for integers. so if you are formatting a string that will contain another string and an integer, the format specifier for sprintf() will be "%s%d".
you can use std::string object in socket programming. just use its c_str() method to convert from std::string to char*.
>> I had tried simply accessing it that way, but for some reason it wasn't recv()'d correctly.
Then there is something else wrong with your program. you should look elsewhere for the error. just make sure the last byte of the buffer is a 0, to terminate the string.
you don't need strcpy() at all
char sendBuffer[5];
char ch;
// assume code in between makes ch something
sendBuff[0] = ch;
sendBuff[1] = 0;
I agree that a delete button would be useful -- I have occationally posted bad information and the only way to delete it was to edit the post and remove all the code, leaving a post with an empty message.
you can use strtok() -- warning, that function changes the string, so if you need the original string later in the program you will have to make a copy of it before calling strtok() the first time.
std::string str = "48098, 50933, 3128, 323, 42, 5";
int n;
char* ptr = strtok((char*)str.c_str(),",");
while(ptr != 0)
{
n = atoi(ptr); // you will probably want to put the ints into
// some sort of array or vector
ptr = strtok(0,",");
}
I am reading a Java book,.
why would you read a java book to learn c++ :eek: :eek: Does that imply I can learn java by reading a c++ book :?:
or code as std::string meh = first;
since you didn't post any code we can only guess. sysName is apparently NOT declared in any of the header files that are included in server.cpp. user the extern keyword like below.
// includes.h
[b]extern [/b] char sysName[32];
I don't really know a thing about that file, but that sort of error message usually means the file is not in one of the directories in the PATH environment variable. search the hard drive to see if that file was installed. If not, then install it.
05/10/2004,00:00:45,190,0,13.94,346.81,206.67,18.46,32,3,47,6, ...
The above is taken from log_05.csv. How is that different from book.csv? The file you posted looks like just trash data, not real data. When your program first opens the file, look at the first line of data. If the first comma-separated field contains a '/' then its a date and the first column number that contains data is #2 (0 is first). If the '/' is not in the first field, then #0 is the first column you want, and subtract 2 from each of the column numbers in array "array".
the calculation suppose to be right.
Then I would suggest YOU find out what is wrong. Use (or learn to use) you compiler's debugger.
because the cout line is printing the value of the loop counter -- i -- not the value of the array, which stores the real column number.
create an array of ints that contain the column numbers you want, then use that in main().
int array[] = {5,6,15,16,17,22,23,28,29,30,37,38,39,44,45,46,50,51,52,61,71,81,91,101,111,121,131,141,151,161,171,181,191 };
// number of elements in the above array
int arraysize = sizeof(array) / sizeof(array[0]);
for(int col = 0; col < arraysize; col++)
sums[col] += sumcol(array[col],line);
all columns can be sumed as floats whether the data contains integers of floats. atof() will work correctly with both strings.
yes, that was a copy/paste error.
simply use the col variable number. You can also put that code in another function that takes the column number as a parameter.
int main()
{
int main()
{
ifstream input_file("filename");
if ( !input_file.is_open())
{
cout << "Error opening file.";
}
else
{
for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
getline(input_file, line, '\n');
count = 8; // We are in the eighth line
int col = 5;
while(count <= 1447)
{ // We have to add until 1447 lines..
for(int i = 0; i < col; i++)
getline(input_file, line, ','); // Get the first 5 columns.
cout<<line<<endl; // Print the 5th column for verification.
value = atof(line.c_str());// Convert the 5th column into float
sum += value; //add value to sum
getline(input_file, line, '\n'); // Go to the next line..
count++; // Increment the count, to go to the next line
}
cout << count;
cout<<endl<<"The sum of all values of column 5 is : "<<sum<<endl;
float avg = sum / count ;
cout<<endl<<"The avg of all values of column 5 is : "<<avg<<endl;
}
}
if (col =5)
you want the boolean == operator, not assignment = operator.
Why do you need those two if statements? With the variable you can combine them and remove if(col == ???)
make the column number a variable so that you can change it as desired.
int col = 7;
for(int i = 0; i < col; i++)
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.
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)
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.
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.
month= newtime ->tm_mon +1;
year=newtime ->tm_year + 1900;
don't forget the above adjustments!
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!
what and where is the error? You need to include time.h like you did stdio.h
yes Dave, sorry for the error.
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.
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);
That identical set of questions has been posted before. google for the first question and you will find the answer(s).
nothing in MS-Windows programming is "simple". If you want "simple" then stay with console programs. The function FillListBox() that was previously posted looks simple to me -- only 3 program lines. The rest of that post is common code that is required by all MS-Windows program using win32 api functions.
ok so would the following statement work then?
char *str[5]; str[0] = "123"; int n = atoi((LPCTSTR)str[0]);
you don't need to typecast character arrays -- that typecast is to convert a CString object to const char*.
char *str[5];
str[0] = "123";
int n = atoi(str[0]);
for CString (I assume you mean Microsoft MFC CString class)
// convert from CString to int just requires typcasting the CString
CString str = "123";
int n = atoi((LPCTSTR)str);
// convert from int to CString
str.Format("%d", n);
that still doesn't guarantee that your system is 32-bit. ;)
Actually, it says nothing at all about the system. I have a new 64-bit system and sizeof(int) is still 32 because that is what the compiler said it is, not the system. It would be impossible to run 32-bit programs on a 64-bit machine otherwise. ;)
get it from the keyboard as a string instead of an integer, then create a loop to check each digit in the string. You don't need modules, division, shifts, or any other math operations, unless, of course, that is a requirement of the problem.
you didn't do anything "wrong". There are a lot of porting issues you must deal with when porting C programs to C++. Most (if not all) the errors you are getting are probably because the functions have not been prototyped before used in main(). You need the *.h file that prototypes all the functions in that library.
If the program is already including the correct header files, make sure the prototypes contain the correct parameter list -- c++ is picky about that. For example, c++ initialize_fold(int) is not the same as initialize_fold()
Second issue: when you get everything to compile correct, it may not link with that C library. C++ normally "mangles" function names, while C does not. To keep c++ from doing that so that the program can be linked with C libraries you have to declare the functions as Extern "C". Here is an example -- you will find many other examples in your c++ compiler's standard include directory.
#ifdef _cplusplus // if compiled with C++ compiler. Note that this is not
// standard, check your compiler's docs and other header files to find out
// the correct macro it uses for c++ versus C compilations.
extern "C" {
#endif
//
// put C function prototypes here
int intialize_fold(int);
// other function prototypes here
#ifdef _cplusplus // if compiled with C++ compiler
} // end of extern "C" block
#endif
Both The Codes Works Fine.....Now What??Why Memory Allocation
There are many many cases where you do not know the text that the character array contains. For example, suppose I ask you for your name. The program has to put the answer some place, if the pointer doesn't point to some value memory locations then your program will crash big time.
// allocate space to put your name, up to 79 characters
char *name = malloc(80);
// display the prompt
printf("Enter your name");
// get name from keyboard
fgets(name,80,stdin);
the above could also be done like this
// allocate space to put your name, up to 79 characters
char name[80];
// display the prompt
printf("Enter your name");
// get name from keyboard
fgets(name,80,stdin);
what dao classes are you using -- Microsoft MFC classes, some classes that you wrote, or something else? If something else then post a link to them.
As for xor producing overflow: I do not see how xor produces overflow problems. Which is what my question was about (it wasn't about sorting): Does xor have problems when a negative number is involved that causes x ^= y; y ^= x; x ^= y; to not swap the values of x and y when x and y are not the same variable? That is, is there a hypothetical case of internal binary number representation and xor operation where a C or C++-compliant compiler would produce machine code that does not successfully swap, given certain strange values of equally-typed x and y?
The xor operator is commonly used in assembly language to set a register value to 0 -- xor if faster at that then moving 0 to the register.
xor does not have a problem with negative values of x or y because it treats x and y as unsigned integers.
Here is an example of the problem with using xor -- the result is 0 for all values of x.
#include <iostream>
using namespace std;
void swap(int& x, int& y)
{
x ^= y;
cout << x << " " << y << endl;
y ^= x;
cout << x << " " << y << endl;
x ^= y;
cout << x << " " << y << endl;
}
int main(int argc, char* argv[])
{
int x = 2;
swap(x,x);
return 0;
}
This is the output
0 0
0 0 …