Thread: Problem with strings in Turbo C

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    5

    Problem with strings in Turbo C

    Edit: I am sorry, I accidentally put this in C, rather than C++. Thank you for the person who moved this thread.
    I was assigned a program to encrypt a string by inputting a word to be encrypted and inputting a code word. The encrypting function then calculates an average of the ascii values to subtract from the ascii value of each letter of the word. Then, we output the encrypted values. For example, inputting a word of potter and a code word of the ascii value of 1 would give onssdq (Alt 1). Now when I do this, it outputs the word but then crashes with a NTVDM illegal instruction issue. I would have tried changing this to a void, but we were given instructions to leave the functions as char*s.
    Code:
    //Pranay Orugunta
    //9/12/11
    //Mr Potter 8
    #include <iostream.h>
    #include <conio.h>
    #include <ctype.h>
    #include <string.h>
    #include <math.h>
    #include <iomanip.h>
    int codenum(char* c1){               //codenum func
    	int sum=0;
    	int length=strlen(c1);
    	for (int i=0;i<length;i++){        //adds up to the sum
    		sum=sum+(int)c1[i];}
    	sum=sum/length;
    	return sum;}                       //returns sum
    char* encrypt(){
    	cout<<"Please input the Phrase to be encrypted."<<endl;
    	char* word;                       //takes in word and code
    	cin>>word;
    	cout<<"Please input the code word."<<endl;
    	char* code;
    	cin>>code;
    	cout<<codenum(code)<<endl;
    	int len1=strlen(word);
    	int value;                        //init vars
    	char* ascii;
    	for (int j=0;j<len1;j++){
    		value=(int)word[j]-codenum(code);  //finds ascii val and subtracts codenum
    		ascii[j]=(char)value;} //converts back to char from ascii
    	ascii[len1+1]='\0';
    	return ascii;}
    char* decrypt(){
    	cout<<"Please input the encrypted phrase."<<endl;
    	char* w1;
    	cin>>w1;                        //takes in enc phrase and cnum
    	cout<<"Please input a code#."<<endl;
    	int cnum;
    	cin>>cnum;
    	char* realw;
    	int len2=strlen(w1);
    	int v1;
    	for (int k=0;k<len2;k++){
    		v1=(int)w1[k]+cnum; //ascii value + code num
    		realw[k]=(char)v1;}     //converts back
    	realw[len2+1]='\0';
    	return realw;}
    void main(){
    	clrscr();
    	cout<<"Please select 1) Word Encryption or 2) Word Decoding"<<endl;
    	int form;           //asks for the form
    	cin>>form;
    	if (form==1){
    		cout<<encrypt()<<endl;}    //goes to each function depending on form
    	if (form==2){
    		cout<<decrypt()<<endl;}
    	getch();}
    Thank you
    Last edited by opalo153; 09-14-2011 at 08:39 PM.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Since you're using C++, there is no reason to use a compiler which is decades old.
    This code will not even come close to compiling on a modern compiler.
    Just try to follow the following steps:
    1. Get a decent compiler.
    2.Turn warning levels high.
    3.Drop conio.h, put <iostream> instead of <iostream.h>, use "using namespace std;"
    4.int main() instead of void main().
    5.Use C++ constructs like the std::string type instead of cin 'ing uninitialized pointers.

    6. I'd say indent better, but Tater will try to chew everyone out, since this is his style !

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    1) Required by use of our teacher.
    2) Ok thank you for this tip.
    3) Would I not require conio.h for clrscr()?
    4) Again, teacher requires void main()
    5) I do not know of this, but I will look it up, thank you.
    6) ok

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by opalo153 View Post
    1) Required by use of our teacher.
    2) Ok thank you for this tip.
    3) Would I not require conio.h for clrscr()?
    4) Again, teacher requires void main()
    5) I do not know of this, but I will look it up, thank you.
    6) ok
    1. Use a better compiler to learn... just put in .h and remove the namespace line before submitting.
    3. Why do you need to clear the screen?.. and for getch() ..use cin.get();
    4. But the standards BAN it. AFAIK even Turbo C supports int main() ....If your teacher forces you to use void main()..I seriously think you need a new one.
    5. Good...but remember not to use it when doing homeworks. (as for your cin , at least use char arrays)

    Seriously...if you are interested in programming in any way other than to pass the class, you should read up some modern books on C++.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    Ok thank you for all these tips, I am only a sophomore in high school so I still do not know if I want to pursue programming as a career/ field of study, but I may consider buying one.
    The reason our teacher suggests using void main is because he wants us to use it until he explains to us what int main(void) is, same with the using namespace.std.
    Also 3) thank you for these commands.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    he wants us to use it until he explains to us what int main(void)
    SourceForge.net: Void main - cpwiki
    Just say you already know it and go on about using int main() ... in C++ () denotes no arguments..whereas in C () denotes not sure how many arguments.

    I'm in High School too...and it isn't an excuse to be lazy.. :P ...Think about what you enjoy doing and get on learning that.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    Hehe, I will take those tips, I meant I was in high school so I was referring to if I wanted to do it as my career or as a hobby.

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by opalo153 View Post
    Hehe, I will take those tips, I meant I was in high school so I was referring to if I wanted to do it as my career or as a hobby.
    That is an easy decision to make..
    If you enjoy it.. then possibly both.
    If not..then none.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    When you do this...
    Code:
    	char* word;                       //takes in word and code
    	cin>>word;
    You are writing to memory you do not own and you're doing it half a dozen times in your program.

    Before you can store anything you have to allocte some memory for it...
    Code:
    	char* word = (char*) malloc(100 * sizof(char));  
    	cin>>word;
    and then when you're done with it you have to release that memory...
    Code:
    free(word);
    Don't do the first and you will end up with "access to protected resources"
    Don't do the second and you will end up with a program that "leaks" memory.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by CommonTater View Post
    Code:
    	char* word = (char*) malloc(100 * sizof(char));  
    	cin>>word;
    Code:
    free(word);
    those would be great if this were C, but this is C++, so it should be more like this:
    Code:
    char* word = new char[100];
    cin >> word;
    delete[] word;
    also there is no need for dynamically allocating memory on the heap for this program. simple arrays will work fine:
    Code:
    char word[100];
    cin >> word;
    turbo C/C++ was great in the EARLY 90's, but it is at least 15 years out of date, and predates any industry standard for the C++ language.

    get Visual C++ Express edition or Code::Blocks with MinGW

    tell your teacher that he/she is teaching ancient, outdated concepts, and that it will hurt the class in the long run to be taught this wrong information. insist on submitting your homework using current standard C++ syntax, and defy any attempt to force you to do it the wrong way. things will never change if people don't stand up and speak out against things that are wrong.

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    and defy any attempt to force you to do it the wrong way
    ...I more than once got 60% marks in a test for that (..not that it mattered ...), everything being correct !!...
    But after a few times or so... they started ignoring it and giving full marks..

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    @opalo153
    Are you really in the USA?
    Because backward teachers in Asia pushing Turbid-Crap is something we have to deal with on a daily basis.

    But in the USA - sheesh!
    IANAL, but I'm thinking lawsuit + negligence / incompetence / malpractice.

    > The reason our teacher suggests using void main is because he wants us to use it until he explains to us what int main(void)
    He wants to create a level playing field by first breaking everyone's legs, then teaching everyone to crawl as quickly as possible.
    This guy is an idiot who shouldn't be let anywhere near a computer, nevermind a roomful of students.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem View Post
    > The reason our teacher suggests using void main is because he wants us to use it until he explains to us what int main(void)
    He wants to create a level playing field by first breaking everyone's legs, then teaching everyone to crawl as quickly as possible.
    This guy is an idiot who shouldn't be let anywhere near a computer, nevermind a roomful of students.
    That's a dumbass excuse anyway. He's already taught you that you have to have a main function, so how hard is to say "you must return 0 or 1 at the end of your program because that's what your computer expects you to do"?


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    5
    Ok thank you everyone for your replies. I have researched some more about this issue and it seems as if that I have went outside my array. I have changed my coding and all, but it does not work.
    I am confused about the delete(word) considering the fact that my output is from a function rather than a simple cout or printf.
    Would I type in delete(encrypt()) at the end of the main program?

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I have changed my coding and all, but it does not work.
    And how does that help us to help you?

    Post your latest INDENTED code if you want more advice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Graphics problem in turbo c++
    By mushahidh in forum Game Programming
    Replies: 1
    Last Post: 10-31-2010, 08:22 AM
  2. Turbo C++, Convertion problem
    By paulroseby in forum C Programming
    Replies: 4
    Last Post: 05-08-2003, 04:23 PM
  3. I have a problem with turbo c++ v3.0
    By leeng_viper in forum C Programming
    Replies: 1
    Last Post: 12-22-2002, 02:16 PM
  4. Replies: 0
    Last Post: 09-25-2002, 04:34 AM