Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the code i am about to an exercise i am using in trying to be effective to debug a code. it has several errors and deliberately do but i am trying to learn the art of programming by looking at the types of error in the error and warning list. i will post the code , then the errors and you all guide me in debugging this program.
C++
#include <iostream.h>
#include <conio.h>

int min(int* &a, int b);

void main()

{
int* x;
int i=0;

 x = new float [0];

 cout<<"\n Enter whole numbers, enter any letter when finishd.";

 while


    cin>>x[i];

   {i++};

   cout<<"Enter smallest number is:<<min(x,i)";

   getch();

   return 0;:

   }

   int main (int*&a , int b);

   {int m = a[0];

   for (int i = 1; i<b; i++)
   {
      if (a[i]<m)
      {m=a(i);}
      }
      return m;}


line cpp 12,5 cannot convert 'float*' to  'int'
line cpp 18,1 while statement missing (
line cpp 20,9 statement missing;
line cpp 20,13 'main()'cannot return a value
line cpp 26,14 unreachable code -warning
line cpp 26,14 expression syntax
line cpp 28,5 statement missing
line cpp 32,15 undefined symbol 'a'
line cpp 34,24 undefined symbol 'b'
line cpp 39,16 'main()cannot return a value.
line cpp 39,1 compound statement missing }
line cpp 40,1compound statement missing
x is assigned a value that is never used

Now!!! how do i use these warnings to debug my program?
what does the second number contained where the error shows up but 12,5 why the , between 12 and 5 and what does the 5 represents?
Posted
Updated 5-Dec-11 21:13pm
v4
Comments
[no name] 6-Dec-11 2:07am    
EDIT: added "pre" tag
Stefan_Lang 6-Dec-11 8:42am    
I just had a thought: since in my country the 'decimal point' is represented as ',', "line 12,5" would mean "line 12.5", or, literally, halfway between line 12 and 13.

Sorry, couldn't resist ;-p

hi..

12 means line and 5 means columns, see in your code starting from
C++
#include <iostream.h>
count 12 line(downward) you will see

C++
x= new float [0];


where x is int and you instantiate with float, obviously it cause an error..

hope find the rest of errors in your code,... enjoy the debugging :)

hope it helps...

don't forget to vote if it helps you


thanks
 
Share this answer
 
v2
Comments
Emilio Garavaglia 6-Dec-11 2:31am    
fixed an HTML spurious tag
Don't!
You are mixing up different concept and issues.

First: the code you post contains ERRORS not BUGS (there is a subtle difference: an error is something the violates the language syntax, that makes your code not translatable into machine code. A bug is a "logical mistake" that doesn't violate the language rules: simply you tell the machine to do something different than what you think).
Actually you cannot debug it because you have nothing (yet) to debug.

Second: don't consider long error sequences: an error can make the parser to be fooled about the meaning of the next text sequence. Correct the errors one at time and recompile, and see how the error list changes.

Third: We cannot fix a code if we don't know what that code is intended to do.
For example: you have an
int* x;
that is assigned as
x = new float [0];

Apart the nonsense of float[0], you are trying to treat as int what is allocated as float. This is incongruent, but what is the correction? change all to int, or all to float? It depends on what you want to do.

Four: you have
while cin>>x[i] 
.
The correct syntax for while is
while(condition) statement

The missing () makes the compiler parser to loose about how the next statement should be interpreted. hence the following errors are not accurate.

You should correct one by one and recompile until all the syntax had been adjusted.
At that point you have a program that can be executed, and -probably- don't behave as you want to.
At that time you must debug it to see what, in fact, goes on while statements are executed and variables assigned.

Now, can you edit the question, perpending a brief explanation on what you want that code to do?
 
Share this answer
 
v2
Comments
carlmack 6-Dec-11 2:41am    
the code submitted was taken from a work sheet exercise, now naturally i would like to understand fully what am doing when a code has errors where to look and what to look for when those errors a given after compiling, so i posted that example for guidance as to how to approach a solution in removing the errors, so far the responses have been good, thank u much
Emilio Garavaglia 6-Dec-11 4:33am    
beware to certain "exercise": there are many dirty ways to correct them, but may go to "bad coding".

For example: cin and getch() can mess each other (one is buffered, the other not, and the first may use the second internally ...) and shouldn't be mixed up without cares.

Another example: while(cin>>x[i]){i++} reads until there are readable numbers than blocks. But doesn't take care about the size of the x array!
To track that you have to rewrite most of the logic (there is nothing in the cpde doing it). But C++ has classes (like std::vector) that can manage by their own dynamic sizes.

Despite the number of errors in logic, it looks code coming from an OLD and outdated bad book!
Stefan_Lang 6-Dec-11 8:37am    
I agree - whereever that working sheet came from, it's a bad source. For one, any compiler in existance will die on you after just a few lines of that code. Second, the error messages will look completely different; on the one side the error messages nowadays are much more understandable, on the other side compilers often mistake a syntax error for another, since there are sometimes multiple ways how an incorrect syntax could be fixed into correct syntax. The effect is that you will get a mix of completely clear and completely messed up messages, not such a list of vague but essentially correct 'errors'.

The only way to learn from this would be to put that code into a real compiler, and then step by step understand the messages and fix the errors.
BrainlessLabs.com 7-Dec-11 2:49am    
Well referring to a standard C++ text will be more useful to you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900