PDA

View Full Version : Help!




Soulstorm
Nov 9, 2005, 12:36 PM
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.

#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
Nov 9, 2005, 12:39 PM
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
Nov 9, 2005, 12:52 PM
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
char *prot1 = "Hello World";

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

B

Soulstorm
Nov 9, 2005, 01:01 PM
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)
What happens if you define prot1 as a constant string like
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...

Also, next time, please use a more descriptive subject for your post than "Help!".
B
I will remember that :)

cait-sith
Nov 9, 2005, 01:18 PM
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
Nov 9, 2005, 01:31 PM
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
Nov 10, 2005, 05:10 AM
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
Nov 10, 2005, 01:18 PM
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?

superbovine
Nov 10, 2005, 04:50 PM
this is a good reference for you:

http://www.amazon.com/gp/product/0766846822/002-7565324-0340008?v=glance&n=283155&s=books&v=glance

Selivanov
Nov 11, 2005, 12:48 AM
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
Nov 11, 2005, 06:30 AM
Just one question: Why is the
prot1++;
inside the
if(*prot1==ch){...}
wouldn't that make kind of hard to get the while loop to work properly...? :confused:

Selivanov
Nov 11, 2005, 07:43 AM
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

if (*prot1++ == ch) {
...
}

but if the current char of string is not used inside loop anymore.

SamMiller0
Nov 11, 2005, 10:47 AM
#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>

#include <conio.h>


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

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

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
Nov 12, 2005, 07:23 PM
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

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++
#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>

#include <conio.h>


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

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

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...