grandtotal us used both as a function and a variable name. You can't do that. See lines 17 and 34 (as well as others)
line 59: max is undefined. But I guess you would have know that had you read your compiler's error messages.
grandtotal us used both as a function and a variable name. You can't do that. See lines 17 and 34 (as well as others)
line 59: max is undefined. But I guess you would have know that had you read your compiler's error messages.
lines 53-60 are still incorrect. You should not have used nested if statements
for (i = 0;i < numTemps;i++)
{
if ( temp[i] > maxtemp)
{
maxtemp = temp[i];
}
if ( temp[i] < mintemp)
{
mintemp = temp[i];
}
cout <<"The minimum temperature is:" << mintemp << "in reading #" << i << "\n";
}
The two loops at lines 51 and 60 can be combined into one loop then find both min and max within the same loop because its more efficient that way.
line 62: min doesn't work because you have the test backwards. Use the < operator instead of >.
>>I'll have to read up again to remind myself how to do the line numbers, apologies for that
Not a problem -- I just like to see them. Here's how
[code=cplusplus] // your code here
[/code]
line 8 is also wrong in this and the previous post. should be hightemp = hourlytemps[i];
line 7 is wrong. Should be testing against hourlytemps, not the loop counter. if( lowtemp > hourlytemps[i])
Oops! I see that now. Your function is correct afterall. Please forgive an old man -- blind in one eye and can't see out of the other :)
I have a function written to display my output, but I've never called a display function before and I'm not sure how to do it. Here is the function
void displaytemp( const int hourlytemps[], int numtemp, double averagetemp) { cout << " Average Temperature: " << averagetemp << endl; }
how do I call this in main?... I know it's not really necessary to create an output function, it's just practice writing functions and calling stuff from arrays and other functions
Call it just as you would any other function
int main()
{
int hourlytemps[24];
int numtemp;
double averagetemp;
displaytemp( hourlytemps,numtemp, averagetemp);
}
Your function displaytep() is also incorrect. You can't display an array like that -- have to print each element one at a time, something like this:
displaytemp( const int hourlytemps[], int numtemp, double averagetemp)
{
for(int i = 0; i < numtemp; i++)
{
cout << hourlytemps[i] << "\n";
}
}
You can not put array declarations in a header file then include that header file in two or more *.cpp files. If that's what you did then you should declare the array with the extern keyword everywhere except in ONE and only ONE *.cpp file.
// header file
[b]extern[/b] int array[8];
Then in a *.cpp file declare it again without the extern keyword.
You have to change the registry. create a command prompt then run regedit.exe.
expand HKEY_CLASSES_ROT then scroll down until you find .cpp folder. delete it as you would any other folder in windows explorer. Do the same with .c and .h. Then exit regedit.
Next open Windows Explorer and double click on any *.cpp file. This will bring up a dialog that asks you which program you want to use to run that file. I was given the option of either Dev-C++ or VC++. I chose VC++ and it was launched. Close VC++. Now every time you use Explorer and click a c++ file the os will launch VC++ without further prompting. You can do the same with *.c and *.h files if you wish.
depends on whether vehicle_Detals derived from People as public or private
class People
{
// blabla
};
class vehicle_Detals : public People
{
// blabla
};
class Visitor : public vehicle_Detals
{
// blabla
};
In the above Visitor has the same assess to People as vehicle_Details has, in otherwords it has access to all public and protected data, but does not have access to private data.
class People
{
// blabla
};
class vehicle_Detals : private People
{
// blabla
};
class Visitor : public vehicle_Detals
{
// blabla
};
In the above, Visitor does not have access to anything in People
you could check the success/failure of the open
ofstream fout ("ride.out");
if( !fout.is_open())
{
cout << "Failed\n";
return 1;
}
[edit]commet and group buffers are too small. You didn't leave room the the null teriminator byte.
forget exam1 and exam2. What you have in that structure is an array of exam grades. If you want the list of grades you posted in your post #28
stu.examcode.push_back(3);
stu.examcode.push_back(13);
stu.examcode.push_back(34);
... // etc etc for the complete list.
Now if you want to check if exam1 and exam2 are in the list above
for(int i = 0; i < stu.examcode.size(); i++)
{
if( stu.examcode[i] == exam1 || stu.examcode[i] == exam2)
{
// found it so now do something
}
}
That loop is just crazy. How can that loop ever execute correctly if the value of values is read from a file. You need a completly different variable when inputting the file
int i = 0;
while( in_file >> numbers[i] )
{
++i;
}
A for loop typically has 3 clauses, you've got 2.
.
In this program it is ok because the loop counter is incremented inside the loop.
but now that I look at that line some more it is all screwed up. for(input>0;counter<=limit;)
sould be for(input=1;counter<=limit;++counter)
then you can delete line 23
My guess is that you are trashing memory some place -- could be buffer overruns, out-of-bounds problem or any number of other things. One way I've used to narrow down the problem is to comment out large blocks of code until I eventually find where the program breaks. Of course the first resort is to use my compiler's debugger to inspect variable values.
that is stub.h ?
line 18: limit is an uninitialized variable, so it could be any old random number including 0. You need to initialize it with something if you expect that line to work properly.
line 21: you need to test if input is <= 0 and if it is break out of that loop
line 36: delete it because its outside the loop. No need to test the value of input here.
Is there another way to solve this problem instead of using files...
Of course -- just store the encrypted data in memory. But of course when your program ends to does that data :)
Can you duplicate the problem with a simple test program? According to the assembly code you posted it looks as if you pass the 'this' pointer to a non-class function or to a static function of a class.
you have missed a couple braces (at lines 2 and 13) and you have lines 5&6 and 10&11 backwards. (can't display something after the function returns).
also delete line 1, or make it a comment, because it does nothing.
That functions does not do what the comments on line 5 says. Doing a bit-wise and operation on two variables does not check to see if either of those values exist in the student class. You have to make comparisons
if( (stu.exam1 == esam1) && (stu.eam2 == exam2))
{
return true;
}
else
{
return false;
}
Do you have even the slightest idea of what you are doing?
line 8: performs bit-wise and operation on the two values,
line 9: toss the result set by line 8 straight into the bit bucket and set the value of r to 1.
So why don't you just delete line 8 because it's not used for anything.
If you remove all the fluf and unnecessary code, your function boils down to this:
// function example
#include <iostream>
using namespace std;
int students (int exam1, int exam2)
{
cout << "The result is 1";
return 0;
}
The problem with that insertion sort function is the the function doesn't know how many elements are in those two arrays -- could be as few as 1 or as many as can be held in an integer, which is a several million. Put a number inside those brackets so that the function will know how big those arrays are. I'm using an arbritrary number here so you would use the correct number. void insertion_sort(int numbers[255], int* array[255])
(ummm.... i wasn't the one having the problem with the code, i know for a fact that the code i posted works... i was posting it so sfurlow2 could see how to do it, else i wouldn't have commented it i would just have put a code snipet for all to see and copy... sorry just sayin....)
My apologies -- I misunderstood. I thought you were asking if it worked.
Great looking code, but what is the problem with it?
First you have too many loops. You don't need that outer loop that starts on line 11. So delete lines 11 and 20.
line 14: replace number/2
with LIMIT.
put open bracket at line 15 and close at line 20.
Align the program indentions correctly so that you can see what's going on.
The brackets are fine. .
Apparently you didn't bother to read/understand the program. There are not enough brackets { and }.
>>ok, should more look something like this:
Did you compile and run that if there are no compile errors or warnings? That's the easiest way to find out if your code is correct or not.
>> lazer printer
Laser printer? American spelling? But that wouldn't really make sense... :-/
You have to check the AD dictionary to find that spelling :)
Your program as posted compiles without error for me. using VC++ 2008 Express. Looks like a logic error though -- you need more brackets around that last while loop (lines 14-18).
All you did on line 12 is display the value of highest. You never set it to anything. Initialize the value of highest to 0 before the loop starts, then when the if condition is true set highest = x
you also need to incude <iostream> at the top of your program and add the using namespace std;
statement or add std::
before each cin statement. I use the using statement because it doesn't require as much typing, but many programmers don't like that. Unless your teacher says otherwise its your call which way to do it.
when sharing memory among threads you must synchronize access so that only one thread at a time has access to the object(s). One way to do that is to create a semiphore. How to do that depends on the operating system you are using, and possibly the compiler. Do a google search for semaphore and you will probably find what you need for your os/compiler.
line 37: delete the brackets [] after array. You don't normally use those in a parameter unless you put a number inside.
line 53: i is an integer, array is a pointer -- you can't compare them. i < array
is wrong.
line 65:If you want to display the array values you have to do it one element at a time in a loop.
suggestion
int main()
{
// put your code here
return 0;
}
Ok, so lets assume you know how to do the above. You need a for/next loop that counts from 0 to 5, display a prompt, get input using cin, then keep track of the highest number entered (you need another variable to do that).
Never worked on web programming -- don't know the first thing about it either. All my experience is with desktop and wireless (barcode scanners) devices and interfacing with other equipment you might find on assembly lines in manufacturing plants. Probably the most interesting one was writing a program for a toothbrush manufacturer in Tennessee, that interfaced PC, database, vt100 terminals, and lazer printer that etched text on the sides of toothbrushes. Over the 12 years I worked for that company I got to visit quite a few manufacturing plants of all sorts.
how about this? this->textBox2->Text = "";
Start by figuring out how to get the 3, 5, 6 and 9. If you know how to do that them its easy to find all those numbers below 1000. If you can't do it in your head then write it out with pencil & paper. Think about the process that is needed to get those numbers. Hint: use the mod operator % to determine if a number is evenly divisible by either 3 or 5 and a loop to test each number between 1 and 1000.
[edit]what Nick said too ^^^^^^^[/edit]
So in row 1 3 meet 138 is 9 because there are 9 numbers between those two values.
Lets assume we have those values in an array
int array[11] = {3, 15, 36, 56, 71, 83, 97, 106, 118, 135, 138};
int counter = 0;
int a = 3;
int b = 138;
int i;
// find first value in the array
for(i = 0; i < 11 && array[i] != a; ++i)
;
// now increment i until you find the value of [b]b[/b]
// and increment [b]count[/b] on each loop iteration
//
// solution for this is not shown -- don't want to spoil your homework :)
>>3 meet 36 is 1
how did you get that result? what is the mathametical formula ?
It won't help if it is a text file. I was assuming it was a binary file, which I don't think you have.
I think what you might want to do is set SIZE to the maximum size you will need. If you don't need them all then no harm done. Then you don't need lines 32-57 at all.
const int SIZE = 16284;
int array[SIZE] = {0}; //array sized for each file input and initialize all elements to 0
Check limits.h to see if an int on your system can hold that large a number. If not then you should probably use a long, if sizeof(long) is not the same as sizeof(int)
First, the array you declared has only ONE element in each of the two dimensions. If you want them to have two elements then declare it like this: int array[2][2];
>> Ive tried changing it to array[1,0]
That doesn't work in C or C++.
>>myIncomingMessage = reinterpret_cast<char*>
you can not return a character array from a function when it has been declared on the stack . The below is wrong because the array will disappear when the function returns, and typcasting will not help that.
char* foo()
{
char myIncomingMessage[255];
return myIncomingMessage; // <<<< WRONG
}
There are a couple work-arounds.
1) declare the array as static
char* foo()
{
[b]static[/b] char myIncomingMessage[255];
return myIncomingMessage; // <<<< OK
}
or dynamically allocate the array
char* foo()
{
char *myIncomingMessage = new char[255];
return myIncomingMessage; // <<<< OK
}
Its not necessary because the array can be treated either way in most cases. Post an example of what you are not sure about.
line 16: That will statically allocate the array based on some random value of SIZE because SIZE was not initialized to anything. What you want is a pointer
int SIZE = 0;
int* array = 0;
lines 32 to 57 is not needed. You can easily get the file size after opening the file, seek to the end of the file, call tellg() and tellg() will give you the file size in bytes. Divide that by sizeof(int) and you have the answer you want.
Now after setting variable SIZE as above you have to allocate memory for the array.
size = new int[SIZE];
lines 60-64 are wrong because eof() doesn't work that way, and because it doesn't write to the array correctly. getline() works with strings, not integers, although you can read integers as strings but then you have to convert the string to an int.
int count = 0;
while(count < SIZE && infile >> array[count] )
count++;
Create four more variables, min, mintempnum, max, and maxtempnum then on line 46 check to see if the temp just read is greater than max then set max to temp, and if temp is less than min set min to be temp. set mintempnum and maxtempnum to be the value of i whenever you change min and max.
int min = 0, mintempnum = 0;
int max = 0, maxtempnum = 0;
if( temp[i] > max)
{
max = temp[i];
maxtempnum = i;
}
// do the same with min
Those are C, not C++ header files. You don't use any of them in c++ programs. Some very old Borland compilers such as Turbo C will use graphics.h, but it is not supported by any other compiler.
run the program from a command window instead of from Dev-C++ IDE.
file.seekg(...) will work in all cases if you seek to the beginning of the file.
otherwise, if the streampos that you use for the seek wasn't one returned by an earlier tellg on the same stream/filebuff, all bets are off. c++ throws in locale dependent character code translation, even when the char_type of the stream is just a char. you migh end up on the second byte of a multibyte character sequence or on the lf of a cr-lf sequence.
if you open a file stream (where the char_type of the stream is just a char) in binary mode and imbue the "C" locale, which will always result in a constant character width equal to one, you can seek to an arbitrary position.
Huh??? I'm sure what you said is probably accurate, but I have no idea what it is.
seekg() works the same in both text and binary mode on both *nix and MAC, Only in MS-Windows and MS-DOS do we run into trouble with text mode seeks due to the two-byte line terminators. I have no idea how it works on non-English file systems, presumably the same.
>>ok, so file.eof() would only work (correctly) if the file is opened in binary mode
No, it works in text mode too. But not the way yo would think it should. fstream only sets the eof flag after an attempt to read beyond the end of the file. The last time getline() is executed it will fail because eof has been reached, the flag is set, but in your original program x is increatemented anyway causing the value of x to be one too many.
>>then file.clear() and file.seekg(...) shouldn't work correctly either
clear() and seekg() work in either binary or text mode. seekg() just has a few limitations in text mode when working in MS-Windows operating system because of the two-byte record terminator in the file system. *nix and MAC don't have that problem because they only use one character record terminators.
hi, this should be a fairly simple question to answer. I have a function in my program that takes the content of a text file and populates an array with all the information. First it has to see how many lines the file has (as it can change) so it can create a dynamic array based on the number of lines.
if(file.is_open() && (get_file_size() > 0)) //get_file_size checks file size { while(!file.eof() ) { getline(file,temp); x++; //number of lines } }
That part works fine, it's this next part that gives me the trouble.
No it does not work fine. Actually it may probably results in the wrong value of x because eof() doesn't work the way you think it should. Here is the correct way to code that loop
while( getline(file, temp) )
++x; // number of lines
while(!file.eof() )
{
end = 0;
getline(file,temp);
for(int z=0;z<2;z++) //hardcoded for number of fields to get
{
end = temp.find(" ");
data[y][z] = temp.substr(0,end);
temp = temp.substr(end+1);
}
y++;
}
The above code has the same problem with eof().
im running msvs6.0
is it my compiler?
Yes. Why are you still using that ancient compiler? You might as well chizzel out your program on a piece of rock with Hammer & Chizzel :) VC++ 6.0 will let you do a lot of things that aren't c++ complient. Download the latest free Microsoft VC++ 2008 Express.