FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Deliberate errors in C++ program. Can you spot them?





Peterssidan
I wrote this stupid program that prints "01234" but I have deliberately made three errors. Can you spot them? Please also explain why it's an error.

Code:
#include <iostream>

int main??()
<%
   int size = 5;
   int arr[size];
   
   for (int i = 0; "Banana"[i]; ++i)
   {
      i[arr] = i;
   }
   
   for (int j : arr)
   {
      std::cout << arr[j];
   }
%>
jmraker
Peterssidan wrote:

Code:

int main??()


The ??( in main??() is a trigraph and could be converted into the letter [ so that line could be read as "int main[)"
http://en.wikipedia.org/wiki/Digraphs_and_trigraphs
Peterssidan
Yes, that's the correct explanation.

Can you spot the other two?
jmraker
I tried installing gcc-g++ in cygwin but I can't get anything with #include <iostream> to link correctly

The only other compile error was that range-based ‘for’ loops are not allowed in C++98 mode, but it worked in a newer mode.

Code:
i[arr] = i;

I'm just guessing that the converting the integer "i" into an array "i[arr]" in the loop would cause a problem when i should be in integer for the loop, plus arr is an array and wouldn't make a good offset into an array
Peterssidan
That's not it.
Marcuzzo
I've shot myself in the foot on my last posts after reading this one.

here are my guesses Very Happy

1. "banana"[i] is not a valid condition in the loop.
2. the for each loop is not correct
cybersa
i think error will occur in this line due to array index out of size.

std::cout << arr[j];

Am i right?
Peterssidan
Marcuzzo wrote:
1. "banana"[i] is not a valid condition in the loop.

Do you know what's wrong with it? Note that this condition does not generate a compile time error.

Marcuzzo wrote:
2. the for each loop is not correct
Quote:
i think error will occur in this line due to array index out of size.

std::cout << arr[j];

Using j as an index to the array doesn't make a whole lot of sense but it works out fine in this case because the value being stored at each index is equal to the index itself: arr[j] == j
Marcuzzo
Peterssidan wrote:
Marcuzzo wrote:
1. "banana"[i] is not a valid condition in the loop.

Do you know what's wrong with it? Note that this condition does not generate a compile time error.


I was kind of drunk last night so now I have to correct myself Embarassed

it doesn't generate a compiler error because there's nothing wrong with the syntax. it's just confusing. I haven't coded in cpp for a while so I broke out gcc again to get familiar with it.

a for loop has 3 statements: initialization; condition; increase. in this case the condition is "Banana"[i] which will remain true for 0 (B), 1(a), 2(n), 3(a), 4(n) and 5 (a) after the 5th iteration the loop will exit because "Banana"[6] will evaluate false.

also, I just noticed that you used a pre-increment on i ( ++i) which is not an error either, If I remember correctly it doesn't make any difference in c++.

other then that, it works, so I'm guessing that the "errors" ( apart from the ?? after the main) aren't real errors but only not recommended practises? am I correct?
Code:
Marcom@HI21339 ~
$ g++ test.cpp -std=c++11

Marcom@HI21339 ~
$ a
01234




Peter, now please give me the sollution, you are killing me here Very Happy
Peterssidan
Marcuzzo wrote:
also, I just noticed that you used a pre-increment on i ( ++i) which is not an error either, If I remember correctly it doesn't make any difference in c++.

There is a difference, but only when you care about the return value of the expression, so in this case it's unimportant.

Marcuzzo wrote:
other then that, it works, so I'm guessing that the "errors" ( apart from the ?? after the main) aren't real errors but only not recommended practises? am I correct?

One of them causes undefined behaviour and is related to the loop condition "Banana"[i]. There is something about it that makes things go wrong somewhere else.

The other one is an error according to the C++11 standard but it's not an error using GCC with default settings. I just found out that in C++14 this is no longer an error.
Peterssidan
I will give you the solutions now because it seems no one is able to figure it out.

As I said before, one of the errors is related to the loop condition "Banana"[i]. Note that the string is 6 characters long. That means that the loop will run 6 times. In the last iteration i will have the value 5 so it will try to access the sixth element in the array (arr[5]) but that is out of bounds because the array only has 5 elements.

The other "error" has to do with the fact that arrays with automatic storage duration has to have their size known at compile time. This can easily be fixed by changing line 5 to const int size = 5;
The C language has allowed the size to be decided at runtime since C99 so that is why GCC has allowed this feature as a C++ extension for a very long time. Visual C++ on the other hand does not have full C99 support so they have never bothered adding it. What I didn't know when I created this program was that in the most recent C++ standard (C++14) this feature has now also been added to the C++ language so this "error" is no longer an error. Wink
SlashnburnSoftwareStudio
Crazy bit of code, see something different every time

int main()

{
These {} are missing?
}

% signs?

The Array size becomes 5, then i becomes 5, cant see how the loop works, guessing it starts at zero and adds 1 each time i++?

int j not declared

std:: would put using namespace std in header etc

Will need to put in editor and check, great post
SlashnburnSoftwareStudio
too good for me, got it to 1 compile error only

///////error C2447: '{' : missing function header (old-style formal list?)

#include <iostream>

using std::cout;

//using namespace std;

int main();
{
int size = 5;
int arr[size];

for (int i = 0; "Banana"[i]; i++)
{
i[arr] = i;
}

for (int j : arr)
{
cout << arr[j];
}


}; //even tried this
Peterssidan
I wrote:
in the most recent C++ standard (C++14) this feature has now also been added to the C++ language so this "error" is no longer an error.

I'm not sure if this is actually true. I read it somewhere but I can't find much information about it. Maybe it was planned/discussed but decided not to be added in C++14 after all.

SlashnburnSoftwareStudio wrote:
These {} are missing? % signs?

<% and %> are called digraphs and is a wierd way of writing { and }.

SlashnburnSoftwareStudio wrote:
The Array size becomes 5, then i becomes 5, cant see how the loop works, guessing it starts at zero and adds 1 each time i++?

You are right, but the problem is that the loop will not stop until i == 6 because that is when "Banana"[i] returns the null termination character (which is treated as false). i == 5 is a disaster because using it as an index to the arr array is out of bounds. arr only have 5 elements so the highest index allowed is 4.

SlashnburnSoftwareStudio wrote:
int j not declared

It is declared in the second loop.

SlashnburnSoftwareStudio wrote:
too good for me, got it to 1 compile error only
///////error C2447: '{' : missing function header (old-style formal list?)

All errors are not necessarily compile time errors. Remove the semicolon after int main() in your code.
Related topics
C++ TUTORIAL : INTRODUCTION TO C++
What's wrong with this C code???
One Liners:: Funny and Somewhat Inspiring
installing apache,mysql and php on windows
A C program in Linux
CLOSE PLEASE-Need a C++ program - upto 200frih$
[C] Splitting names program
Deploying a C# program
changing system time using a c program under cygwin
winsit.exe
C++ Software
C++ Program Basics
C++ program yazmaya baţlayalým artýk :=)
Writing your first C++ Program
My Small C# Program
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.