Compiler discrepancy between DevC++ and VS2008..

Hi there,

I am new to programming and recently had an assignment to create a function that would delete the repeated characters in a character array. Besides deleting the repeated characters, I also was tasked with keeping track of how many slots were being used in the array.

Now, to the crux of the situation. I created the program in Visual Studio 2008 primarily. I worked in both, but ultimately ended up using VS2008 to finish it. It compiled fine and displayed the right output. However, my teacher compiled it and ran it in Dev C++, and the output was different (wrong). Suffice to say, I had points deducted due to this discrepancy. Please have a look at my code and share your thoughts as to why it's different.... 'cause I sure can't figure it out.

Which compiler is more "accurately" compiling the code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

void delete_repeats (char arr[], int slots);
/* Slots is the declared size of the character array arr.
   Repeated characters will be deleted within the array, whilst the empty spaces will be moved to the end of the array. 
   The displaychars function is then called, with the modified array and the new size of the array passed to it. */
void displaychars (char arrayoutput[], int slots);
/* This function displays the characters of the array passed to it, along with the size of the array, as represented by 
   slots */
int main()
{
     char testarray[20] = {'a', 'b', 'a', 'c', 'd',
     'd', 'e', 'f', 'g', 'h', 'e', 'e', 'f', 'g', 'i', 'j', 'k', 'l', 'l', 'm'};
     int size = 20;
	 cout << "Here is the test array prior to removing the duplicate characters: \n"; displaychars(testarray, size);
	 cout << "\n\nHere is the modified array, with duplicates removed: \n";
	 delete_repeats(testarray, size);
     
}

void delete_repeats (char arr[], int slots)
{
     int index = 0;
     int compare = 1;
     int shifter = 0;
     do
     {
         do
         {
               if (arr[index] == arr[compare])  //Catches duplicates
               { 
                   shifter = compare; // shifter is used in place of compare so compare can retain integer value
					   do
						{
							 arr[shifter] = arr[shifter+1];  //Since a duplicate was found, this loop overwrites
							 shifter++;                      //the duplicate with the next character in the array
						} while (shifter <= slots);          //and proceeds to shift all characters up one position  
	                                                         //in the array
                    slots = slots - 1; //Since a match was found, the # of slots has been reduced by one.
               }       
               
			   if (arr[index] != arr[compare]) //Catches any consecutive duplicates (or rather, purposely "misses" them)
               {                               //in order to keep the integer value of compare appropriate.  
               compare = compare + 1;  //Compare is only incremented if the following character is not a duplicate too.        
               }
               
        } while (compare <= slots); //This guides the "index versus rest of chars in array" loop.
     
     index = index + 1; //Index is incremented by one to test the next character in the array.
     compare = index + 1;//Compare gets initialized to the location in the array that follows index. 
	                     //Due to the structure of the preceding loops, there will be no duplicates preceding index, 
	                     //thus index need only be compared to the array characters that follow it.
     
     } while (index <= slots); //This guides the controlling loop... should be obvious.
	
     slots = slots + 1; //This takes care of slots being decreased by one errantly on the final index comparison.
     displaychars(arr, slots);
     system ("pause");  //This was needed when working in Dev C++.  I used both VS2008 and DevC++ for this lab.
}
void displaychars(char arrayoutput[], int slots)
{     
     cout << "# of slots used = "; cout << slots; cout << "\n";
	 int tempdisplay = 0;
     do
     {
          cout << arrayoutput[tempdisplay];
          tempdisplay ++;   
     } while (tempdisplay < slots);            
     cout << endl << endl;     
}
       
              
    
    
You do know that some of your do while loops would be better replaced with a for loop?

I.e. lines 66-71:
1
2
3
for (int i = 0; i < slots; ++i) {
//code
}


Anyway...what exactly was the difference in the displays? If you are using cout...it probably would have been a lot easier to do this assignment using std::strings (unless you assignment wanted char*).
There is a discrepency in the outputs between MS Visual Studio and DEV C++.

Unfortunately it is cause by a bug in the program - Line 40.
} while (shifter <= slots); //error

Edit:
Too slow
Last edited on
delete_repeats() seems to work, any errors are likely to have arisen from displaychars(). For some reason it showed a '0' at the end of the string.
I replaced your array with this:
1
2
3
char testarray[100];
strcpy(testarray,"abacddefgheefgijkllm");
int size = strlen(testarray);

and the call to displaychars() with this: printf("%s\n",testarray);
and it worked just fine.

EDIT: Oh, yeah. I'm using GCC.
Also, VC++ has somewhat of a tendency to generate code more bug-resistant. This is not a good thing, because it means that any bug your logic has will go undetected. Said bugs will jump to your face when the program is compiled somewhere else (GCC, for example).
I want to emphasize, however, that this is not the compiler's fault. Your code, if correct, should run without problem when compiled by anything.
Last edited on
You do know that some of your do while loops would be better replaced with a for loop?

Yes... last night someone was helping me with a new project and was wondering why I was using the more cryptic and less elegant while loops. As I said, I'm new to programming, so I did it the noob way.

Anyway...what exactly was the difference in the displays?

VS2008: Correctly (so to speak) displays the characters 'a' thru 'm' and displays the correct count (which is 13).

DevC++: Displays 'a' thru 'm', but also displays a garbage/weird character after 'm', and displays the count as 14.

That's a pretty big discrepancy (to me as a c++ noob).

Unfortunately it is cause by a bug in the program - Line 40.
} while (shifter <= slots); //error


Yes, and I compensate for that (again remember I'm new so I don't fix things in the best possible way) later on in the program. If you have VS2008, run the program- the output is correct. I'm not saying the code is correct, but the output is correct... at least in VS2008. I'm just curious as to why.


EDIT: Didn't see helios post until after I posted this (just fyi).

Last edited on
Topic archived. No new replies allowed.