home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

C++ Error

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2038
Discussions: 200,950, Posts: 2,379,433, Members: 246,311
Old February 22nd, 2007, 11:12 PM   Digg it!   #1 (permalink)
Vyx
Ultimate Member
 
Vyx's Avatar
 
Join Date: May 2003
Location: Cheyenne, WY
Posts: 1,087
Send a message via AIM to Vyx Send a message via MSN to Vyx Send a message via Skype™ to Vyx
C++ Error

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
Vyx is offline   Reply With Quote
Old February 23rd, 2007, 04:19 PM     #2 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,552
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
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.
jkrohn is offline   Reply With Quote
Old February 24th, 2007, 12:14 AM     #3 (permalink)
Senior Member
 
Join Date: May 2003
Location: Aus, Gold Coast :)
Posts: 802
Send a message via ICQ to exally
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.
exally is offline   Reply With Quote
Old February 24th, 2007, 03:05 AM     #4 (permalink)
Vyx
Ultimate Member
 
Vyx's Avatar
 
Join Date: May 2003
Location: Cheyenne, WY
Posts: 1,087
Send a message via AIM to Vyx Send a message via MSN to Vyx Send a message via Skype™ to Vyx
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.
Vyx is offline   Reply With Quote
Old February 24th, 2007, 03:52 AM     #5 (permalink)
Senior Member
 
Join Date: May 2003
Location: Aus, Gold Coast :)
Posts: 802
Send a message via ICQ to exally
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?
exally is offline   Reply With Quote
Old February 24th, 2007, 03:54 AM     #6 (permalink)
Senior Member
 
Join Date: May 2003
Location: Aus, Gold Coast :)
Posts: 802
Send a message via ICQ to exally
i just compiled and ran the code and get no errors ?!!!!??? and ran the program
exally is offline   Reply With Quote
Old February 24th, 2007, 10:22 AM     #7 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,241
Send a message via AIM to Rootstonian
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.
Rootstonian is online now   Reply With Quote
Old February 24th, 2007, 11:51 AM     #8 (permalink)
Senior Member
 
Join Date: May 2003
Location: Aus, Gold Coast :)
Posts: 802
Send a message via ICQ to exally
instead of const int he should really use #define - but u go by what u live by
exally is offline   Reply With Quote
Old February 24th, 2007, 08:15 PM     #9 (permalink)
Caveat Emptor
 
Rootstonian's Avatar
 
Join Date: Mar 2005
Location: Out of my mind
Posts: 3,241
Send a message via AIM to Rootstonian
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.
Rootstonian is online now   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
Firefox/Error Code 500/Internal Server Error pandorah Applications and Operating Systems 17 August 31st, 2008 09:01 PM
DMA Driver Error. CRC Error benkk5618 Technical Support 13 January 1st, 2008 07:30 AM
DMA Driver Error / CRC Error benkk5618 Storage Related 3 May 29th, 2007 12:47 PM
Lite-On Drive Write Error: Power Calibration Error MessiaH289 General Tech Discussion 5 November 12th, 2004 03:01 PM
Error C00D10D1 on WMP. Error Downloading Codec Ranger99 Technical Support 3 December 6th, 2002 03:54 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
The disrespect of Obama by Russian .. (32)
Making Health Care Worse (176)
Is It Just Me? (2940)
Wireless Televisions. (12)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Regular Build (7)
radeon x850xt platinum & shader.. (6)
Is the PSU I received dead? (12)
Print spooler problem (15)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
windows vista security holes (9)
Dept. of HS: NSA 'Helped' Develop V.. (15)
Recent Discussions
Point and Shoot Camera Suggestions. (4)
Regular Build (7)
Multiple Restarts Required at Boot (2)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
For Sale BFG GTX285 OC2 with 10 year .. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Laptop with wireless problem. (2)
Internet Lost (1)
Hp Artist Edition + Matching Bag (0)
My monitor won't turn on after instal.. (0)
Asus P4G8X Mobo (6)
Xbox 360 GTA: SA disk error (1)
Is the PSU I received dead? (12)
windows 7 internet problem (5)
BSOD On Startup (ntoskrnl.exe) (2)
Print spooler problem (15)
Have you switched yet? (86)
screen resolution vs monitor size (2)
sms storage to PC (0)
Open With ..... Win7 (0)
java code for fibonacci (1)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (35)


All times are GMT -4. The time now is 11:24 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28