Thread: Runtime error for a simple scenario

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Runtime error for a simple scenario

    hi ...
    my i/p canbe of format.
    $ $ $ . . $
    $ . $ . . $
    here the number of rows and cols is 2 and 5.
    i have to consider whenevr the dots come into play.
    as i encou nter each dot i increment a counter meaning that is 1st dot,2nd dot and so on...
    in the first row i have two dots... so i name them as 1 and 2...
    whenever 2 dots are adjacent or one below the other i need to associate those dots...
    here in the i/p above i have to associate
    1 2
    1 4
    and 2 5

    this seems to be simple...but am getting runtime error for the following processing... can anyone help me plzz....
    Code:
    #include<iostream>
    #include<string.h>
    #define max 100
    using namespace std;
    int main()
    {
    long long int t,i,j,count=0,a[max][max],m,n;
    char c[max][max];
    
    cin>>m>>n;
    
    for(i=0;i<n;i++)
    {
    for(j=0;j<m;j++)
      {
    scanf("%c",&c[i][j]);
    
    if((c[i][j]=='.')&&(count==0))
           {
              count++;
              a[i][j]=count;
              goto s10;
            }
    if((c[i][j]=='.')&&(count!=0))
              {        
               count++;
               a[i][j]=count;
               if(i!=0)
                    if(c[i-1][j]=='.') cout<<"\n"<<a[i-1][j]<<"and"<<a[i][j];
                if(j!=0)
                      if(c[i][j-1]=='.') cout<<"\n"<<a[i][j-1]<<"and"<<a[i][j];
               }
    s10:
    ;
    }
    }
    return 0;
    }
    Last edited by dpp; 02-22-2009 at 03:03 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your code is an unindented mess with goto statements.
    Nobody wants to go anywhere near it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    well i ve indented now....
    the idea is simple....that anybody can understand

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, you have not properly indented your code, and you should redo the code such that it does not use goto.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You still have a row of } in the left column.

    Try this
    Code:
    #include<iostream>
    #include<string.h>
    #define max 100
    using namespace std;
    int main()
    {
        long long int t,i,j,count=0,a[max][max],m,n;
        char c[max][max];
    
        cin>>m>>n;
    
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf("%c",&c[i][j]);
    
                if((c[i][j]=='.')&&(count==0))
                {
                    count++;
                    a[i][j]=count;
                    goto s10;
                }
                if((c[i][j]=='.')&&(count!=0))
                {        
                    count++;
                    a[i][j]=count;
                    if(i!=0)
                    if(c[i-1][j]=='.') cout<<"\n"<<a[i-1][j]<<"and"<<a[i][j];
                    if(j!=0)
                    if(c[i][j-1]=='.') cout<<"\n"<<a[i][j-1]<<"and"<<a[i][j];
                }
    s10:
                ;
            }
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Now that's done, consider replacing the goto with an else
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  3. Help me with these simple programs
    By Help me in forum C Programming
    Replies: 4
    Last Post: 11-08-2001, 10:38 AM
  4. Need help with simple data types
    By partnole in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2001, 08:36 AM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM