Help!

Discussion in 'Mac Programming' started by Soulstorm, Nov 9, 2005.

  1. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #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;
    }
     
  2. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #2
    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)
     
  3. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #3
    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
     
  4. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #4
    1)How can I deal with it?
    2)Why did it work in a tool that is 12 years old? (TurboC++ is from 1993)
    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...
    I will remember that :)
     
  5. macrumors regular

    Joined:
    Apr 6, 2004
    Location:
    canada
    #5
    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
     
  6. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #6
    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...

    Actaully, it's those 2 teachers that must be fired... the rest are fine.

    Anyway, thanks...
     
  7. macrumors newbie

    Joined:
    Nov 7, 2005
    Location:
    Voronezh, Russia
    #7
    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)
     
  8. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #8
    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?
     
  9. macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
  10. macrumors newbie

    Joined:
    Nov 7, 2005
    Location:
    Voronezh, Russia
    #10
    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).
     
  11. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #11
    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:
     
  12. macrumors newbie

    Joined:
    Nov 7, 2005
    Location:
    Voronezh, Russia
    #12
    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.
     
  13. macrumors member

    SamMiller0

    Joined:
    Aug 17, 2004
    Location:
    San Jose, CA
    #13
    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.
     
  14. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #14
    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++
    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...
     

Share This Page