another c++ program question - help needed

Discussion in 'OT Technology' started by D1G1T4L, May 15, 2005.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    so this is another program that had global variables and now i remaded it so it doesnt have any global variables except 1 constant

    Code:
    // Purpose: Prints out all the words and how many times they appear 
    // All the words that are typed are converted to lower case.
    // Comparison of the strings are done by string compare method (strcmp)
    // which compares lowercased words.
    // BUGS: None
    // Additional Info: Compiled with Microsoft Visual C++.NET 2003
    // Running on Windows XP
    // Notes: My program uses bubble sort but first i tried selection sort
    // and selection sort doesn't work too great
    // Project1_8.41_c.cpp
    
    #include<iostream>
    #include<cctype>
    #include<string>
    #include<iomanip>
    using namespace std;
    //standard libraries
    
    
    
    //function prototype for bubble sort
    void bubbleSort(char array[512][512], const int size);
    
    
    //prototypes
    void readInput(char sentence[]);
    void calculate(int storageValue[], char sentence[], char storage[512][512], int counter);
    void displayResults(int storageValue[],  char storage[512][512], int counter);
    
    //user input max length
    const int INPUT = 512;
    
    int main()
    {
    	//array that holds the number of times the word appears
    	//if the word is a duplicate, it's going to set 0
    	int storageValue[INPUT] = {0};
    
    	//user input
    	char sentence[INPUT] = {0};
    
    	//will contain the words
    	char storage[INPUT][INPUT];
    	
    	//counts total input
    	int counter = 0;
    	
    
    	
    	//read input
    	readInput(sentence);
    	
    	//count word lengths
    	calculate(storageValue, sentence, storage, counter);
    
    	//display the results
    	displayResults(storageValue, storage, counter);
    	
    		
    	return 0;
    }
    void readInput(char sentence[])
    {
    	int nLines; //number of lines of input
    	char *usrInput;
    	string combinedInput;
    
    	//asks the user for the number of lines he'll enter
    	//the input must contain not more than 1024 characters
    	cout << "\nHow many lines of input is there going to be?"
    		<< "\n(All combined input can only contain up to 1024 characters): ";
    	cin >> nLines;
    	usrInput = new char[INPUT+1];
    	cin.get(); // reads the empty character \n from the buffer
    	for(int i = 0; i < nLines; i++)
    	{
    		
    		cout << "\nEnter a sentence: "; 	//gets the input from the user
    		cin.getline(usrInput, INPUT, '\n');
    		strcat(sentence, " "); //adds a space between the words
    		strcat(sentence, usrInput);
    	}
    
    	//delete the memory and set pointer to null	
    	delete [] usrInput;
    	usrInput = 0;
    }
    void calculate(int storageValue[], char sentence[], char storage[INPUT][INPUT], int counter)
    {
    	
    	//tokenizer pointer
    	char *tokenPtr = NULL;
    
    	//tokenizing the setnence
    	tokenPtr = strtok(sentence, " ");
    
    	//loop that goes through and tokenizes each word
    	//and finally copies the word into a storage array
    	while(tokenPtr != NULL)
    	{
    		
    		 strcpy(storage[counter], tokenPtr);
    		 tokenPtr = strtok(NULL, " "); // next token
    		 counter++;
    	}
    
    
    	//sort the array
    	bubbleSort(storage, counter);
    
    	//2 loops that go through the storage array's values
    	//and add up the number of times the word appear
    	//the adding takes place in storageValue array
    	//if the word is a duplicate, it sets it to -1 
    	//and doesnt display it in the end result
    	int counter2 = 0;
    	for(counter2 = 0; counter2 <= counter; counter2++)
    	{
    		for(int counter3 = counter2+1; counter3 <= counter; counter3++)
    		{
    			if(storageValue[counter2] == 0)
    				storageValue[counter2] = 1;
    			if(storageValue[counter3] == 0)
    				storageValue[counter3] = 1;
    			if(storageValue[counter2] != -1)
    			{
    				if(strcmp(storage[counter2], storage[counter3]) == 0)
    				{
    					storageValue[counter2]++;
    					storageValue[counter3] = -1;
    				}
    			}
    
    		}
    
    
    	}
    	
    }
    
    void displayResults(int storageValue[], char storage[INPUT][INPUT], int counter)
    {
    	
    
    	//counter that goes through all the input
    	//and displays the word and the number of times
    	//the word appeared
    	int counter2 = 0;
    	while(counter2 < counter )
    	{
    		
    		if(storageValue[counter2] != -1)
    		{
    			cout << storage[counter2] << " appears " << storageValue[counter2] << " times\n";
    		}
    		counter2++;
    
    	}
    	
    	
    }
    //bubble sort function to sort alphabetically
    void bubbleSort(char array[512][512], const int size)
    {
    	
      int i, j;
    
      for (i = (size - 1); 0  <= i; i--)
      {
        for (j = 1; j <= i; j++)
        {
          if ((strcmp(array[j-1],  array[j])) == 1)
          {
    		char *hold = new char[strlen(array[j-1])+ 1];
            strcpy(hold, array[j-1]);
            strcpy(array[j-1], array[j]);
            strcpy(array[j], hold);
    		delete [] hold;
          }
        }
      }
    
    
    }
    
    

    The purpose of the program is written on the top

    The program used to work fine when i had global variables, but now i made storageValue, sentence, storage, counter, tokenPtr as a local variables, but the program doesnt function because it gets the input but doesnt print anything out!


    i tried using the visual studio.net debugger and noticed taht on line 97 it says that tokenPtr is <bad ptr>, however thats the same way it worked in previous program, i also dont understand how to use the "watch 1" tab, cant insert any variables there....

    help me out? :hsd:
     
  2. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    also btw, when i click Step Into, it takes me out of my cpp file and opens bunch of .c files like crt0.c and crt0dat.c which have weird ass code, i dont get what that's all about
     
  3. turbo91

    turbo91 New Member

    Joined:
    Jun 14, 2002
    Messages:
    2,521
    Likes Received:
    0
    Location:
    San Diego, CA
    don't step into functions that you didn't write...if you step into "printf" for example, it will bring up the CRT (common runtime) library code. If no source is available for the function, it will step into the assembly.

    You want to use "step over" for functions you didn't write.
     
  4. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    ok thx, btw what do you think is wrong here?
     
  5. Krakerjak

    Krakerjak Active Member

    Joined:
    Jul 7, 2003
    Messages:
    8,288
    Likes Received:
    0
    Location:
    Edmonton eh
    Your displayresults() function never reallly runs.

    When you get to that point in the program "counter" passed in is = 0
    In that function you set counter2 = 0

    So that while loop, "while(counter2 < counter)" will always evaluate to false.



    Also, you don't have a routine that converts any uppercase letters to lowercase so it treats "dog", and "Dog" as two different words.

    Finally, your calculate function seems to loop one too many times.
    Here is a sample test:

    Enter a sentence: Hello this hello this
    Enter a sentence: testing This This testing

    once sorted and calculated, you get this.

    storage[] contains
    0: Hello
    1: This
    2: This
    3: hello
    4: testing
    5: testing
    6: this
    7: this

    and storageValue[] contains

    0: 1 ---> Hello
    1: 2 ---> This
    2: -1
    3: 1 ---> hello
    4: 2 ---> testing
    5: -1
    6: 2 ---> this
    7: -1
    8: 1 ---> ??????
    9: 0...0...0...etc...


    Hope that helped
     
  6. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    ok i found the problem, i wasnt just returning counter variable from calculate function, as soon as i did taht, everything works fine


    p.s. in this program letter case letters and upper case letters should be counted as different, dont mind the documentation :hsd:

    just dont mind the "// All the words that are typed are converted to lower case." hehe, anyways thanks man!
     

Share This Page