does anyone know what the error is?????..........

#include<iostream.h>
#include<conio.h>
#include<math.h>

void main()
{
void mtable(int ,int);

int num,n,prod;

cout<<"\n\n\tEnter the no";
cin>>num;
mtable(num,n) ;
cout<<"\n\n\tEnter the limit";
cin>>n;

cout<<"\n After one call ";
mtable(5,10);
mtable(7,10);
getch();
}

void mtable(int num,int n)

int prod=1;
for(int i=0;i<=n;i++)
  { prod=num*i;
 cout<<"\n"<<num<<" * "<<i<<" = " <<prod;
}

   }

Dani AI

Generated

and spotted the main problems: several nonstandard/obsolete items, a function call using an uninitialized value, and a malformed function definition. The practical fixes are straightforward and will make the program portable and well-defined.

Use the standard headers and a standard main signature. Modern C++ uses <iostream> and int main() (see the rules for main at en.cppreference.com). Avoid legacy iostream.h and compiler-specific conio.h/getch, which are nonstandard (see the conio.h entry on Wikipedia).

Do not pass a variable before it has been given a value: a local int with no initializer has an indeterminate value, and reading it is undefined behavior ( and undefined behavior). Also make sure every function definition has matching braces and correct syntax; a missing { or } will generate parse errors or unexpected program structure.

A few practical tips:

  • Put function declarations at file scope (or define the function before main). Declarations inside a block are allowed by the language, but they are uncommon and can confuse readers (declarations).
  • Validate input from std::cin before using it.
  • Replace platform-specific calls like getch() with portable alternatives (for example, let the console close naturally or use standard input handling).

Example of a modern, corrected approach (compact and portable):

#include <iostream>

void mtable(int num, int limit);

int main() {
    int num = 0, limit = 0;
    std::cout << "Enter number: ";
    if (!(std::cin >> num)) return 1;
    std::cout << "Enter limit: ";
    if (!(std::cin >> limit)) return 1;
    mtable(num, limit);
    mtable(5, 10);
    mtable(7, 10);
    return 0;
}

void mtable(int num, int limit) {
    for (int i = 0; i <= limit; ++i)
        std::cout << num << " * " << i << " = " << (num * i) << '\n';
}

Following these points will address the compile/run errors and make the code safe and standard-compliant.

Recommended Answers

All 5 Replies

Prestandard code, a missing curly brace, and using a value before it's defined?

Prestandard code, a missing curly brace, and using a value before it's defined?

i didnt get you!!!!

The stuff in red needs some looking into.

#include<iostream.h>
#include<conio.h>
#include<math.h>

void main()
{
void mtable(int ,int);

int num,n,prod;

cout<<"\n\n\tEnter the no";
cin>>num;
mtable(num,[B]n[/B]) ;
cout<<"\n\n\tEnter the limit";
cin>>n;

cout<<"\n After one call ";
mtable(5,10);
mtable(7,10);
getch();
}

void mtable(int num,int n)

int prod=1;
for(int i=0;i<=n;i++)
{ prod=num*i;
cout<<"\n"<<num<<" * "<<i<<" = " <<prod;
}

}

The headers are either nonstandard or unused. The main function should return an int. You call a function with an undefined n. The function getch is nonstandard. And your mtable function is missing a matching curly brace.

Also your indentation is lost when you don't use code tags.

EDIT: Dave beat me to it, but read it anyway if you like (apparently I was missing a few things).

i didnt get you!!!!

Firstly, wrap your code in CODE tags, and specify a language (i.e. [ CODE = C++ ], w/o the spaces) so that there are numbers - makes it easier to point out errors.

void mtable(int ,int);

I'm pretty sure function prototypes should be outside of other functions, though I could be wrong about that.

mtable(num,n) ;

n was declared but not defined (i.e. it wasn't initialized). It has no value. Put that line after

cout<<"\n\n\tEnter the limit";
cin>>n;
void mtable(int num,int n)

You need an opening curly bracket after this to start a function.

I believe that's it.

thanks amillion to both of you-dave and cool gamer...i was struggling with that..it was a gr8 help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.