| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I have a runtime error that I cant figure out
the runtime error I get is
Run-Time Check Failure #2 - Stack around the variable 'chChount' was corrupted. I cant see anything wrong with what I am doing here can you? here is my code #include <iostream> #include <string> #include <fstream> using namespace std; int main() { ifstream infile; string buffer; char chbuf; long chCount[256]= {0}; //memset(chCount, 0, 256 * sizeof(int)); cout<<"please entere a filename to analyze: "; getline(cin, buffer); infile.open(buffer.c_str(), ios::in); if(infile.is_open()) { while(!infile.eof()) { chbuf = infile.get(); chCount[chbuf]++; } for(int i =0;i<256;i++) { if(chCount[i] != 0) { cout<<char(i)<<": "<<chCount[i]<<endl; } } infile.close(); } return 0; } |
|
#2
|
|||
|
|||
|
Logical Flaw in your code at the point where you have written stmt.
Code:
chCount[chbuf]++; Here you are incrementing array chcount ( i.e trying to change the address of array. This is not allowed with an array. Secondly, i presume you are trying to count characters.Then also chBuf ( though act like an integer) but can give error. Do chCount[chbuf++] instead. |
|
#3
|
|||
|
|||
|
Quote:
would that really increment the array aelement int the chbuf location? I may be wrong but it looks to me that it would simply call teh the chbuf + 1 element in chCount array then do nothing with that element. What I want to do is increment the integer located in the chCount[chbuf] location I dont understand why this causes a problem. an array is just a pointer to data. why cant I increment the data at that location? |
|
#4
|
|||
|
|||
|
I figured it out.
The problem is that I wasn't checking to make sure that the data I pulled from the file was an array. when I tried to access the chbuf location of a size 256 array and chbuf > 256 well that is a problem isn't it. hehe but my method of incrementing the worked ok. |
|
#5
|
|||
|
|||
|
An Array though a pointer is different from a normal pointer in that it is a constant pointer with fixed address assigned to it during compile time.In case of normal pointer address can be changed using ncrement operator.
|
|
#6
|
|||
|
|||
|
Quote:
I hink you are right.Sorry, I was mistaken. It will increment the elelemnt pointed to by that position in the array. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > I have a runtime error that I cant figure out |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|