Like Tree1Likes
  • 1 Post By Salem

What's the mistake i'v did in this?

This is a discussion on What's the mistake i'v did in this? within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream.h> #include<stdio.h> #include<conio.h> int i,j; int func1(int x,int y,int z); int func2(int x,int y); void main() { clrscr(); struct ...

  1. #1
    Registered User karthikslegend's Avatar
    Join Date
    Jun 2013
    Location
    Chennai, India.
    Posts
    9

    Question What's the mistake i'v did in this?

    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<conio.h>
    int i,j;
    int func1(int x,int y,int z);
    int func2(int x,int y);
    void main()
    {
        clrscr();
        struct
        {
            int a;
            int b;
        }s1[3][3],s2[3][3],s3[3][3],s4[3][3];
        for(int i=0;i<=3;i++)
        {
            for(int j=0;j<=3;j++)
            {
                cin>>s1[i][j].a;
            }
        }
        for(i=0;i<=3;i++)
        {
            for(int j=0;j<=3;j++)
            {
                cin>>s1[i][j].b;
            }
        }
        for(i=0;i<=3;i++)
        {
            for(int j=0;j<=3;j++)
            {
            s1[i][j]=s2[i][j];
            }
        }
        for(i=0;i<=3;i++)
        {
            for(int j=0;j<=3;j++)
            {
                cout<<func1(s2[i][j].a,s2[i][j].b,s3[i][j].a);
            }
        }
    getch();
    }
    int func1(int x[3][3],int y[3][3],int z[3][3])
    {
        for(i=0;i<=3;i++)
        {
            for(j=0;j<=3;j++)
            {
                z[i][j]=x[i][j]+y[i][j];
            }
        }
        return z[i][j];
    }
    //int func2(int x,int y)
    //{
      //    return x*y;
    //}

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    void main should be int main.
    Arrays indexes go from 0 to size - 1. You have buffer overruns.
    You are overwriting the same arrays multiple times.
    You are trying to return a single index from func1 instead of the entire array (which can't be done anyway).
    Your i and j are global variables (they should be local).
    iostream.h should be iostream.
    stdio.h should be cstdio. Do you even need it? I don't think so.
    conio.h is non-standard and should be avoided. Use std::cin.get() instead of getch().
    Well, that's a start anyway. You could make the code a little prettier, too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User karthikslegend's Avatar
    Join Date
    Jun 2013
    Location
    Chennai, India.
    Posts
    9
    Thank you Elysia.. But I can't get you bcoz I'm a begginer, will u help me editing this program..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    What do you have trouble understanding in Elysia's statements?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User karthikslegend's Avatar
    Join Date
    Jun 2013
    Location
    Chennai, India.
    Posts
    9
    My compiler only runs the program if I include iostream.h and stdio.h, and what's that array index go from 0 to -1, I can't get that. But its showing a linker error.

  6. #6
    Registered User karthikslegend's Avatar
    Join Date
    Jun 2013
    Location
    Chennai, India.
    Posts
    9
    And i'v to call the function func1, so i have to declare main as void only, how can I use int there.

  7. #7
    Registered User karthikslegend's Avatar
    Join Date
    Jun 2013
    Location
    Chennai, India.
    Posts
    9
    And I'm a begginer too, so dont get annoyed with my questions.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    The first step is to get a modern IDE/compiler. Here is a list: http://cpwiki.sf.net/IDE
    Code::Blocks is a very good IDE if you download it together with gcc (there's a link on their homepage with that bundle).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User karthikslegend's Avatar
    Join Date
    Jun 2013
    Location
    Chennai, India.
    Posts
    9
    Okay... Thank you.

  10. #10
    Registered User karthikslegend's Avatar
    Join Date
    Jun 2013
    Location
    Chennai, India.
    Posts
    9
    Linker Error: Undefined symbol func1(int,int,int) in module MYSAMP.CPP

    This was the error I got

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Linker error results when you told the compiler a function exists but in reality it does not. Case in point, from your code:

    Code:
    int func1(int x,int y,int z);
    void func1(int x,int y,int z)
    {
    	// Blah
    }
    This will result in a linker error if you try to call func1 from a function before the "void func1" (the definition) is seen by the compiler (ie, the code for it is above it). The reason is that the compiler thinks the return type is int, but later when the linker tries to put the program together, it can't find such a function.
    If you don't understand this, then review how the compilation process works might help.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User karthikslegend's Avatar
    Join Date
    Jun 2013
    Location
    Chennai, India.
    Posts
    9
    Ya, I got your point.

  13. #13
    Registered User karthikslegend's Avatar
    Join Date
    Jun 2013
    Location
    Chennai, India.
    Posts
    9
    Thank you Elysia.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    For reference -> What's the mistake i'v did in this? - Dev Shed

    Understand that people hate wasting their time repeating what has already been said to you elsewhere.
    iMalc likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is my mistake
    By Dave Couture in forum C Programming
    Replies: 8
    Last Post: 08-14-2012, 11:28 PM
  2. mistake of using awk
    By lehe in forum Linux Programming
    Replies: 6
    Last Post: 04-02-2009, 05:41 PM
  3. What is my mistake ?
    By Freelander1983 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2007, 09:31 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21