Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
ATTENTION: Newbie question!

I am learning c++ in my university, but it seems that those guys are a bit... outdated. Anyway, I don't want to call them names in here (although I would certainly like to).

The purpose of this exercise is to insert a string from the keyboard and a character, and then to see how many times this character exists inside the string we gave.

They are using borland's TurboC++ (A VERY old tool, and they do not let us to use anything else). This code appears to work correctly on them.

Code:
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>

using namespace std;

void main(){
   
	char *prot1, ch;
	int count1 = 0;
	gets(prot1);
	
	cin >> ch;

	
	while(*prot1){
       if(*prot1==ch){
		count1++;
		prot1++;
	}
}

	cout << count1;
}
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
But when I run it into my OS X Xcode, or even the Windows Bloodshed DevC++ compiler, it compiles ok, but at the instant that I give the string to be processed, the program crashes! In xCode it returns a message like "application has exited with status (SIGBUS10) and in windows it gets me an error that says that my newely created program has performed an illegal operation and must be shut down! "What can I do to rectify the problem with the minimum possible changes?

Note that when porting the program to newest systems I have done the basic modifications (changed the headers a little bit and turned the main() from void to int)
 

balamw

Moderator emeritus
Aug 16, 2005
19,365
979
New England
Soulstorm said:
the instant that I give the string to be processed, the program crashes!
Not a C++ expert, but I dabble. Doesn't look like any memory was allocated for prot1. (new? malloc? array size?)

What happens if you define prot1 as a constant string like
Code:
char *prot1 = "Hello World";

Also, next time, please use a more descriptive subject for your post than "Help!".

B
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
balamw said:
Not a C++ expert, but I dabble. Doesn't look like any memory was allocated for prot1. (new? malloc? array size?)
1)How can I deal with it?
2)Why did it work in a tool that is 12 years old? (TurboC++ is from 1993)
balamw said:
What happens if you define prot1 as a constant string like
Code:
char *prot1 = "Hello World";
I know that this whould solve the problem, but it would ruin the exercise. I want to be able to give as string everything I like.
EDIT: What you suggested doesn't work. It continuously asks for "ch" and the program is never terminated for some reason...
balamw said:
Also, next time, please use a more descriptive subject for your post than "Help!".
B
I will remember that :)
 

cait-sith

macrumors regular
Apr 6, 2004
248
1
canada
uh...

i would recommend switching universities!

the problem is that

char *prot1, ch;

declares a pointer to a character string, prot1, and a character on the stack, ch.

do you know what pointers are, and how values are stored on the stack as opposed to on the heap? if not, you're in for trouble. google pointers.

anyways, char *prot1 means prot1 points to the memory location of a character. you need to allocate memory for your character string! maybe turboc does this automatically, but when you just declare a pointer in C/C++ (ansi anyways) the memory location it points to is undefined!

try char prot1[1000], ch; instead, this creates prot1 on the stack where it has (by default) memory allocated to it. or to allocate it on the heap, char *prot1 = (char *)malloc(sizeof(char)*1000).. either one gives you a string of size 1000. you need to make sure you don't exceed that bound when working with the string or you will access memory you may or may not have allocated.

c/c++ memory stuff can be dangerous -- you are in FULL control.

`mike
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
Thanks a lot. I knew memory allocation was the problem, AND i have been involved with pointers for some time, so I am not completely irrelevant with the subject.

I just asked wether there was a way of surpassing the problem without changing the fundamental structure of this code, because when someone does anything "extreme" in programming in my university (to those teachers specifically-others are ok) will never graduate! Not to change the basic structure of the exercise seemed impossible to be from the first time I saw this. I hate to see that in programming I must rely on myself alone...

i would recommend switching universities!
Actaully, it's those 2 teachers that must be fired... the rest are fine.

Anyway, thanks...
 

Selivanov

macrumors newbie
Nov 7, 2005
9
0
Voronezh, Russia
And i could recommend to set allocated memory to zero. I mean:
memset(prot1, 0, 1000);
By default you couldn't say - what will be in the memory after allocation. And getstr() is not obliged to add finalization zero.
(Sorry for my bad english)
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
Selivanov said:
And i could recommend to set allocated memory to zero. I mean:
memset(prot1, 0, 1000);
By default you couldn't say - what will be in the memory after allocation. And getstr() is not obliged to add finalization zero.
(Sorry for my bad english)
So you say that in a table that contains a string, when you use the gets() there will be no '\0' character at the end of the string? That's what is causing the problem?
 

Selivanov

macrumors newbie
Nov 7, 2005
9
0
Voronezh, Russia
Soulstorm
No, i say that gets() may not add '\0' to the end of string. That is why i recomend to call memset() for allocated memory. But the problem was in allocating memory for the string (as it said before my post).
 

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
14,657
1,465
Bergen, Norway
Just one question: Why is the
Code:
prot1++;
inside the
Code:
if(*prot1==ch){...}
wouldn't that make kind of hard to get the while loop to work properly...? :confused:
 

Selivanov

macrumors newbie
Nov 7, 2005
9
0
Voronezh, Russia
Mitthrawnuruodo
I think it was Soulstorm typing mistake... There will be an endless loop if the first char of string not equals to the entered char...

Much better to use
Code:
if (*prot1++ == ch) {
  ...
}
but if the current char of string is not used inside loop anymore.
 

SamMiller0

macrumors member
Aug 17, 2004
66
0
San Jose, CA
Code:
#include <iostream.h>
#include <string.h>
#include <stdio.h>

These are deprecated headers, use the correct ones:
#include <iostream>
#include <string> //if you want std::string
#include <cstring> //if you want to use C string methods
#include <cstdio>

Code:
#include <conio.h>

This is a platform specific header, not C++ specific

Code:
using namespace std;

This is a kludge and mangles your namespace. You should only include whatever you need out of the std library. Example: using std::string

Code:
void main(){

The C++ standard says main returns an int, not a void. You probably didn't notice most of these errors since you are using such an old compiler. I suggest using gcc and compiling with the -pedantic flag.
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
Selivanov said:
Mitthrawnuruodo
I think it was Soulstorm typing mistake... There will be an endless loop if the first char of string not equals to the entered char...

Much better to use
Code:
if (*prot1++ == ch) {
  ...
}
but if the current char of string is not used inside loop anymore.
Actually, it wasn't a mistake. I typed the exercise exactly as it was given to me. I know it's crappy code, and I DIDN'T write this code! My teachers did and I know they know nothing about c++
SamMiller0 said:
Code:
#include <iostream.h>
#include <string.h>
#include <stdio.h>

These are deprecated headers, use the correct ones:
#include <iostream>
#include <string> //if you want std::string
#include <cstring> //if you want to use C string methods
#include <cstdio>

Code:
#include <conio.h>

This is a platform specific header, not C++ specific

Code:
using namespace std;

This is a kludge and mangles your namespace. You should only include whatever you need out of the std library. Example: using std::string

Code:
void main(){

The C++ standard says main returns an int, not a void. You probably didn't notice most of these errors since you are using such an old compiler. I suggest using gcc and compiling with the -pedantic flag.
Actually, I knew all of these things, maybe you didn't see that the exercise was meant to be compiled in a compiler 13 years old. I hate it when I have to transform my code to suit the needs of a compiler 13 years old...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.