0

I am having a run-time error in this program, it has no syntax error but crashes when it is run. i am using dev-c++ 4.9.9.2. I tried to find the error but i couldn't find it. If anyone can help then please find the errors and correct me.

#include<iostream.h>


void DisplayVUID();
void DisplayReverse(char[], int);
void StoreDiagonal();

main()
{
      DisplayVUID();
      char a[20] = "mc123456789";
      DisplayReverse(a, 20 );
      StoreDiagonal();

system("pause");
}
void DisplayVUID()
{
     int i;
     char name[20] = "mc123456789";
     cout<<"My VU id is ";
     for(i=0;i<20;i++)
     {
          cout<<name[i];
     }
     cout<<endl;
}
void DisplayReverse(char a[], int arraysize)
{
     int i;
     cout<<"MY VU id in Reverse is ";
     for(i=arraysize-1;i>=0;i--)
     {
       cout<<a[i];
     }
     cout<<endl;                           
}
void StoreDiagonal()
{
     int a[9][9] ;
     int i;
     int row, col;
     for (i = 0; i<9;i++)
     {
         for(i=0;i<9;i++)
         {
         a[row][col] = 0;
         }
     }
a[1][1] = 1;
a[2][2] = 3;
a[3][3] = 0;
a[4][4] = 2;
a[5][5] = 0;
a[6][6] = 2;
a[7][7] = 3;
a[8][8] = 9;
a[9][9] = 8;
      for(i = 0 ; i < 9 ; i ++)
              {
                for( i = 0 ; i < 9 ; i ++)
                {
                cout<<a[row][col];
                }
              }
}
Rubens
  • 14,478
  • 11
  • 63
  • 92
Sireiz
  • 383
  • 3
  • 10
  • 1
    you are not using namespace standard,try `using namespace std;` after include statement. – 0decimal0 Jun 28 '13 at 10:10
  • 1
    Next time, use a debugger first, then call us. – Martin James Jun 28 '13 at 11:05
  • @Sireiz You didn't discuss things about the problem , neither did you accept any of the answers, I would be lying if I say that I don't expect you to accept my answer but its the time which we put in answering a question and you should pay attention by discussing if it worked or not :) – 0decimal0 Jul 25 '13 at 13:07
  • @PHIfounder sorry for that but when i looked at the comments they wre very discouraging and here nobody is ready to help so i left this question and didnt take a look till you posted a comment here. – Sireiz Jul 25 '13 at 18:19
  • @Sireiz The main thing is, it seems discouraging and rude in the first place but it actually helps you improve your future posts , one of the guy downvoted me for some reason on my answer and for that very reason I got several upvotes on my other answers , so at first sight it may seem like that but it ultimately helps you ..... Sometimes you become a victim of this like http://stackoverflow.com/questions/17633405/using-goto-keyword-in-c/17633458#17633458, here I got 3 upvotes and 3 down ..... You simply gotta learn from it :) – 0decimal0 Jul 26 '13 at 02:09
  • @Sireiz In short, don't be petrified , learn from your mistakes and post questions only after searching on google and SO itself as it shows research from your part :) good luck :) – 0decimal0 Jul 26 '13 at 02:15
  • 1
    @PHIfounder :) i always find things on google before coming to this site, that why i have a very less number of questions. this was an old code and is of my friend. i made a mistake that i didn't worked on t and just posted here. Well i will try not to do this again. I am new to this site and i started learning programming a few months go. So need help from others. But thank you for your help and i will consider it in future that here things dont work like that. – Sireiz Jul 26 '13 at 06:55
  • @Sireiz yay!! that's positive ... every good invention has its plusses and minuses and SO is no exception to it , if handled with care it can be very profitable for learners. All the best :) – 0decimal0 Jul 26 '13 at 06:58

2 Answers2

4

Things don't work this way on Stackoverflow, from next time onward try hard to do things on your own, do your research and then come here.There seemed to be many errors in your program but I tried to remove some bugs and finally, It works on my system. I have also recommended some nice things via comments that you can look in the program:

EDIT: I noticed that some undefined string because of unassigned spaces in the array was printed out in the reverse function but i have corrected it now.

#include<iostream>
#include<stdlib.h>
using namespace std;// use namespace otherwise cout won't work


void DisplayVUID();
void DisplayReverse(char[], int);
void StoreDiagonal();

int main()// In C++ always declare a main function like this,its good habit
{
      int i=0;
      DisplayVUID();
      char a[20] = "mc123456789";
      while(a[i]!='\0')
       i++;// Did this to ensure that cout will print only up to the null character,earlier it was printing some undefined characters along with the data in the array.
      DisplayReverse(a, i );
      StoreDiagonal();

  system("pause");
  return 0;//return 0
}
void DisplayVUID()
{
  //int i;
     char name[20] = "mc123456789";
     cout<<"My VU id is ";
     //for(i=0;i<20;i++)// no need for this loop at least here
     //{
          cout<<name;
          //}
     cout<<endl;
}
void DisplayReverse(char a[], int i)
{
     cout<<"MY VU id in Reverse is ";
   //  for(i=arraysize-1;i>=0;i--)
     //{
       while(i--)
       cout<<a[i];//instead of the for loop traversing for the whole arraysize i have made it go up to only the null terminator this way it doesn't print undefined characters.
     //}
     cout<<endl;                           
}
void StoreDiagonal()// i don't understand by the way what this function is here for , its not an error though.
{
     int a[9][9] ;
     int i,j,c=1;
     //int row, col;// you didn't initialize row and column and placed it inside the loop 
     for (i = 0; i<9;i++)
     {
         for(j=0;j<9;j++)
         {
         a[i][j] = 0;
      if(i==j)
       {
       a[i][j]=c;//instead of manually assigning do it like this.
       c++;
       }

         }
     }
       for(i = 0 ; i < 9 ; i ++)
              {
                for( j = 0 ; j < 9 ; j ++)
                {

                  cout<<a[i][j];// the elements of the array don't display like a table , this is not an error but to make your output readable do it by your self

              }
              }
}
0decimal0
  • 3,884
  • 2
  • 24
  • 39
  • 1
    +1 for your comments in code, I don't support to answer this way(complete code) but due to comments you added, its very helpful answer. Keep it up! – Grijesh Chauhan Jul 07 '13 at 21:10
  • 1
    @GrijeshChauhan Thanks a lot!! just needed the support and guidance like this from people like you here. Actually, even I don't want to write an answer like this but I thought OP was being naive here so why not give him a guidance through specific points in the code itself . Anyway , I will take that in my consideration from now on. Thank you once again. :) – 0decimal0 Jul 08 '13 at 06:28
2
a[9][9]=8;

remove this line, you will be fine. Array indexing starts from 0 not 1.

Also I would like to point that in your function DisplayVUID() change i<20 to a[i]!='\0' because the values after '\0' will be garbage values.

banarun
  • 2,305
  • 2
  • 23
  • 40