• Welcome to Valhalla Legends Archive.
 

getting an error at runtime, help please

Started by Eli_1, February 06, 2004, 10:37 PM

Previous topic - Next topic

Eli_1

I'm trying to make the switch from VB to C/C++. I'm new to it and I'v only been trying to use C++ for a few days now. I wanted to make a quick program that would let me compare the Left() and Right() functions I used in VB to code that would do the same thing in C++. but I get 3 errors at runtime:
1.) MAIN caused an error in MAIN.exe - MAIN will now close.
2.) MAIN caused an error in MAIN.exe - MAIN will now close.
3.) MAIN caused an error in Kernel32.DLL - MAIN will now close.

btw, this code was compiled with a Borland c++ compiler


#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#define max_char   256

int main() {
   char *input;
   string new_string;
   bool go_again=true;

   while (go_again==true) {
      cout << "Enter a string to change: ";
      cin.getline(input,max_char);

               if (strcmp(strlwr(strdup(input)),"/quit")==0)
                       go_again=false;
               else if(strcmp(strlwr(input),"/clear")==0 || strcmp(strlwr(input), "/restart")==0)
                       clrscr();
               else if (strcmp(strlwr(input),"/help")==0)
                       cout << "Commands:\n" << "/quit\n" << "/clear\n" << "/restart\n" << "End of commands.\n";
               else {
                       cout << "\nYou entered: " << input << "\n";
                       cout << "\n";
                       cout << "Here is the string after being manipulated in VB code and in c++ code.\n\n";
                       cout << "VB code:  string = right(data, 2)\n";
                       cout << "c++ code: string=data.substr(b-2,2);  b = strlen(input);, data=string(input);\n";
                       int b;
                       b=strlen(input);
                       new_string=string(input);
                       cout << "****New string: " << new_string.substr(b-2,2) << "\n";
       
                       cout << "VB code:  string = left(data, 2)\n";
                       cout << "c++ code: string=data.substr(0,2);  data=string(input);\n";
                       cout << "****New string: " << new_string.substr(0,2) << "\n";
               
                       cout << "VB code:  string = left(data, len(data) - 2)\n";
                       cout << "c++ code: string=data.substr(0,b-2);  b=strlen(input); data=string(input);\n";
                       cout << "****New string: " << new_string.substr(0,b-2) << "\n";
       
                       cout << "VB code:  string = right(data, len(data) - 2)\n";
                       cout << "c++ code: string=data.substr(2,b-2);  b=strlen(input); data=string(input);\n";
                       cout << "****New string: " << new_string.substr(2,b-2) << "\n";
                       go_again=true;
               }
       }
       return 0;
}

:'(

K

#1
First, some suggestions:


#include <iostream>
#include <string>
#include <cstdio> // are you actually using this?
#include <cstdlib> // ditto
#include <conio.h>

using namespace std;


As to your problem:

when you declare a pointer like this:

char* szInput;

It doesn't point to anything; it has no memory / storage space asosciated with it; it points to some random address in memory.  So when you try do this:

cin.getline(szInput)

You write to some random place in memory that you don't own. (A Bad Thing (tm)).

Options:
A) use a std::string (getline(new_string))
B) use an array:  char szInput[255];
C) dynamically allocate and deallocate the memory.
char* szText = new char[255];

cin.getline(szText)
// ...

delete [] szText;

Eli_1

thank you very very very much, K.  ;D

well than I'm confussed at exactly what char* is, I assumed it was like... a char with unlimited size... like a string...

K

A char* is a pointer to the first index of an array of characters; there is no native string type in c/c++.  With that said, the std::string class found in <string> provides exactly what you want.  You can do things like:


string s = "hello ";
s += "world";

if (s == "hello world")
    s = s.substr(0, s.find(" "));

cout << s << endl;