Hello,

I am university student and currently we are being thaught C++ and are studying Object Oriented Programming.

Since last few weeks what ever assignment I get , My program always end up with thread stop error while runtime and I cant find the reason.

I have to currently submit two assignments both of which I have completed and the logic is fine according to what I can see , I dont get any syntax error , but when running it gives me Thread Stop Error and I just keep wondering what I am doing wrong.

Here are the code for both of the programs:

1. I had to make a program which will get input string and read the words and tell how many times 1 letter word has occured, 2 letter word etc . For eg:

If user enter " I am a good boy"

Output will be :
1 letter word : 2 times
2 letter word : 1 times
3 letter word : 3 times
4 letter word : 4 times

This is the code I have written for Question 1:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
const int MAX=100;
void main()
	{
   char input[MAX];
   int length[MAX];
	  for (int i=0;i<100;i++)
   	{
      input[i]='\0';
      length[i]=0;
      }
   gets(input);
   int startpoint=0;
   int counter;
   while (input[startpoint]!='\0')
   	{
      counter=0;
      while (input[startpoint]!=32)
   		{
      	counter++;
      	startpoint++;
         }
      if (input[startpoint]==32)
      	{
         length[counter]++;
         }
      startpoint++;
      }
   cout<<setw(20)<<"Word Length"<<setw(20)<<"Occurences"<<endl;
   for (int i=0;i<100;i++)
   	{
      if (length[i]!=0)
      	{cout<<setw(20)<<i<<setw(20)<<length[i]<<endl;}
      }
   getch();
   }

The second program I had to make was:

2. A program will get user input and then see which word occurs how many times:
If user enter " My name is My name is Good Name is Good Good"

It will show:
My appeared 2 times
name appeared : 3 times
is appeared : 3 times

and so on.

The code for this is :

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
#include<cstring>
const int MAX=100;
void main()
 {
 char input[MAX];
   int times[MAX];
   string str[MAX];
   for (int i=0;i<MAX;i++)
    {input[i]='\0';times[i]=0;}
   gets(input);
   int start=0;
   int truth=0;
   string temp;
   while(input[start]!='\0')
    {
      truth=0;
      temp="";
      while(input[start]!=32)
       {
         temp+=input[start];
         start++;
         }
      for (int i=0;i<MAX;i++)
       {
         if (temp==str[i])
          {times[i]++;truth=1;}
         }
      if (truth==0)
       {
       str[start]=temp;
       times[start]++;
         }
      start++;
      }
   for (int i=0;i<MAX;i++)
    {
      if (times[i]!=0)
       {cout<<"The word: "<<str[i]<<" has occured: "<<times[i]<<" times"<<endl;}
      }
   getch();
   
   }

Both of these programes are giving Thread Stop Error .
My last week assignment did the same and the instructor coundlt figure out still after a week what was wrong with it .

Thanks , Your help will be appreciated

Your logic is completely broken and your code does not compile on a modern compiler. I can bet you are using Turbo C.

The reason your program crashes is because you are trying to write on memory which doesn't belong to you.

Output in my case:

this program doesn't work

Writing at location 4
Writing at location 12
Writing at location 20
Writing at location 173
Writing at location 264
Writing at location 276

Its interesting to note here that you have only 100 locations which belong to you...

Thanks for your reply.

I am using Borland C++ .

So can you tell me what is the solution to this problem ? I mean what modification to do in the code to solve this ? I am a novice programmer .

Thanks

My stab at it:

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
    map<string, int> wordCount;
    string str, temp;

    cout << "Enter the string (press ENTER key and CTRL + Z to end)" << endl;
    while(cin >> str)
    {
        wordCount[str]++;
    }
    map<string, int>::iterator iter;
    cout << endl;
    for(iter = wordCount.begin(); iter != wordCount.end(); ++iter)
    {
        cout << iter->first << " : " << iter->second << endl;
    }
    getchar();
}
Member Avatar for iamthwee

>My last week assignment did the same and the instructor coundlt figure out still after a week what was wrong with it .

That's because you're in a circus being taught by clowns.

>I mean what modification to do in the code to solve this ? I am a novice programmer .

There's no point trying to trick people into doing your homework. Let me guess, your next question is, erm "I'm not allowed to use STL stuff."

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.