C++ Error  | |
February 22nd, 2007, 11:12 PM
|
#1 (permalink)
| | Ultimate Member
Join Date: May 2003 Location: Cheyenne, WY
Posts: 1,087
|
Hi, I'm back again with another C++ error I can't figure out. I've been searching around for about an hour and a half so far.
When I run the program it compiles and then I get an error. The error I get is: Code: Debug Assertion Failed!
Program:...
file: isctype.c
Line: 56 If I hit ignore, it pops up the same message only with line 68. I've narrowed the source of the error down to the calc function. If I move the function to after the output, the output will come up, then it will error out, or if I just comment out that code, it will run just fine. The wierd thing is that if I type in as many characters as the text array is large or more characters than the array can hold, it will run without error, but if I put less characters than the array size, it errors out. Does anyone see something I'm missing?
Here is my code: Code: #include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;
// Declare constants
const int SIZE=100;
// Function Prototypes
void intro();
void initalize(int letter[]);
void input(char text[]);
void calc(char text[], int letter[], int& words);
void output(int letter[], int words);
int main()
{
// Declare variables below here
char text[SIZE];
int letter[26], words=1;
// Initialization Section for real number output. DO NOT MOVE!
cout <<setiosflags(ios::fixed | ios::showpoint);
cout <<setprecision(2);
// Begin your "main processing" below here
intro();
initalize(letter);
input(text);
calc(text, letter, words);
output(letter, words);
return 0;
}
// function definitions below here
void intro()
{
cout << "+++++++++++++++++++++++++++++\n";
cout << "+ Word/Letter Count Program +\n";
cout << "+++++++++++++++++++++++++++++\n\n";
}
void initalize(int letter[]) //initalize all elements in letter array to be 0
{
for(int i=0; i<26; i++)
{
letter[i]=0;
}
}
void input(char text[]) //get the input
{
cout << "Enter a line of text to be calculated:\n";
cin.getline(text, SIZE);
}
void calc(char text[], int letter[], int& words)
{
for (int i=0;i<SIZE;i++)
{
if (isspace(text[i]))
words++;
}
}
void output(int letter[], int words)
{
cout << words << " words";
}
__________________ Gi | Yuu | Jin | Rei | Makoto | Meiyo | Chuugi |
| |
February 23rd, 2007, 04:19 PM
|
#2 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,552
|
Just a quick guess would be that anything past the characters you input are not being intialized. So once the check gets past the characters that are assigned you are calling isspace on garbage data and failing an assertion.
Why are you doing it this way with the fixed array anyway?
Jkrohn
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
Last edited by jkrohn : February 23rd, 2007 at 04:29 PM.
|
| |
February 24th, 2007, 12:14 AM
|
#3 (permalink)
| | Senior Member
Join Date: May 2003 Location: Aus, Gold Coast :)
Posts: 802
|
exchange the for loop for a while loop and do it until it is reaches the NULL deliminator (i think that is the terminology) or it is greater than the size... i guess you could do this in the for loop aswell but when it comes to this sort of stuff while loops always seems easier... or have some obscure character set to the size of the array like Ü and when it reaches that escape... other than that may i suggest the use of std::string
btw what does isspace do?? i have never come across it before
Last edited by exally : February 24th, 2007 at 12:17 AM.
|
| |
February 24th, 2007, 03:05 AM
|
#4 (permalink)
| | Ultimate Member
Join Date: May 2003 Location: Cheyenne, WY
Posts: 1,087
|
isspace just checks to see if the character is a space. in my program, the number of words is calculated by adding one to the number of spaces. ie. the dog ran has two spaces inbetween the words, therefore adding one will yeild 3 words.
The reason for me using a fixed array is that in all the examples I've seen of cin.getline, they all have two parameters, (variable, ##). I'm not sure how to use getline without a number to stick in there. |
| |
February 24th, 2007, 03:52 AM
|
#5 (permalink)
| | Senior Member
Join Date: May 2003 Location: Aus, Gold Coast :)
Posts: 802
|
i can guess u are passing a empty piece of memory to "isspace" and that is your error.... can u do a debug and see which part of the array you are having problem with? |
| |
February 24th, 2007, 03:54 AM
|
#6 (permalink)
| | Senior Member
Join Date: May 2003 Location: Aus, Gold Coast :)
Posts: 802
|
i just compiled and ran the code and get no errors ?!!!!??? and ran the program |
| |
February 24th, 2007, 10:22 AM
|
#7 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,241
|
open up the include file isctype.c and see what assertion is failing for the isspace function.
Just a comment on style..on one array you use a const int size and on the other you hard code the number 26....be consistent - it will save your butt in the future
You're "walking" past the boundary of the array. Yeah, it's initialized to 100 bytes, but that doesn't mean that 100 bytes are in there.
Someone mentioned a while loop...I agree
i = 0;
while input_string[i] <> '\n' // read string till null term
// do stuff
// increment i
end-while;
Last edited by Rootstonian : February 24th, 2007 at 10:26 AM.
|
| |
February 24th, 2007, 11:51 AM
|
#8 (permalink)
| | Senior Member
Join Date: May 2003 Location: Aus, Gold Coast :)
Posts: 802
|
instead of const int he should really use #define - but u go by what u live by |
| |
February 24th, 2007, 08:15 PM
|
#9 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,241
|
That would work too. #define is a precompiler directive that really just does "text" substitution. The defintion of int means the variable is IN memory, but just can't be changed (exally, I knew you knew this and I'm not trying to be smart  )
Both will work equally well unless you would want to perform any math or pass the value of that indexing variable. |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |