// This program asks the user to enter the length of a rectangle
 // then for the width of a rectangle , to find the perimeter by 
 // this 2 * length + 2 * width 
#include <iostream.h>
return unsigned long int Perimeter ( unsigned short int , unsigned short int ) ;
                                                                 // function prototype shown
int main ()
{
 unsigned short int length ;
unsigned short int width ;
unsigned long int Perimeter ;

 cout << " Please enter the length of the rectangle: " << length << endl ;
cin >> length ;
cout << " Please enter the width of the rectangle: " << width << "\n" ;
cin >> width ;


length = 2 * length ;
        width = 2 * width ;
        Perimeter = Perimeter ( unsigned short int length , unsigned short int width ) ;
        cout << " The Perimeter of the rectangle is: " << Perimeter << endl ;
        return 0 ;
    }

    return unsigned long int Perimeter(  unsigned short int length , unsigned short int width )
    {

        return   2 * length + 2 * width  ;
    }

This doesn't work.I know I screwd up with the variables names somehow but can't just find it :sad:

Here is the error report:

Compiling...
Bug Busters.cpp
D:\Microsoft Visual Studio\VC98\Bin\Day 5- Bug Busting\Day5Bugbusting\Bug Busters.cpp(2) : error C2143: syntax error : missing ';' before 'return'
D:\Microsoft Visual Studio\VC98\Bin\Day 5- Bug Busting\Day5Bugbusting\Bug Busters.cpp(18) : error C2144: syntax error : missing ')' before type 'unsigned short'
D:\Microsoft Visual Studio\VC98\Bin\Day 5- Bug Busting\Day5Bugbusting\Bug Busters.cpp(18) : error C2064: term does not evaluate to a function
D:\Microsoft Visual Studio\VC98\Bin\Day 5- Bug Busting\Day5Bugbusting\Bug Busters.cpp(18) : error C2059: syntax error : ')'
D:\Microsoft Visual Studio\VC98\Bin\Day 5- Bug Busting\Day5Bugbusting\Bug Busters.cpp(23) : error C2143: syntax error : missing ';' before 'return'
D:\Microsoft Visual Studio\VC98\Bin\Day 5- Bug Busting\Day5Bugbusting\Bug Busters.cpp(24) : error C2143: syntax error : missing ';' before '{'
D:\Microsoft Visual Studio\VC98\Bin\Day 5- Bug Busting\Day5Bugbusting\Bug Busters.cpp(24) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.

Bug Busters.obj - 7 error(s), 0 warning(s)

Somebody plz show the error and explain why..

Recommended Answers

All 10 Replies

#include <iostream>
 
// function prototype
unsigned long int Perimeter (unsigned short int, unsigned short int);
 
int main ()
{
unsigned short int length;
unsigned short int width;
unsigned long int Perimeter;
 
cout << " Please enter the length of the rectangle: " << length << endl;
	cin >> length;
cout << " Please enter the width of the rectangle: " << width << "\n";
	cin >> width;
 
length = 2 * length;
width = 2 * width;
Perimeter = Perimeter (length, width);
cout << " The Perimeter of the rectangle is: " << Perimeter << endl;
return 0;
}
 
unsigned long int Perimeter(unsigned short int length, unsigned short int width)
{
  return 2 * length + 2 * width;
}

Oh my god..How stupid of me I kept thinking of what should it return so I put return keyword in the definition and kept compiling again and again..anyway...now this is the error report:
ompiling...
Bug Busters.cpp
D:\MICROSOFT VISUAL STUDIO\VC98\BIN\DAY 5- BUG BUSTING\Day5Bugbusting\Bug Busters.cpp(20) : error C2064: term does not evaluate to a function
Error executing cl.exe.

Bug Busters.obj - 1 error(s), 0 warning(s)
I know that I don't really need to enter parameter names in function prototype and it depends on position not names...but I'm still confused...

Which is line 20?

Perimeter = Perimeter(length, width);

Try removing the space between the function name and the parameters passed in?

yeah line 20 is : Perimeter = Perimeter ( length, width ) ;
I tried this way,exactly:

#include <iostream.h>

// function prototype
unsigned long int Perimeter (unsigned short int, unsigned short int);

int main ()
{
unsigned short int length;
unsigned short int width;
unsigned long int Perimetercoke;

cout << " Please enter the length of the rectangle: " << length << endl;
    cin >> length;
cout << " Please enter the width of the rectangle: " << width << "\n";
    cin >> width;

length = ( 2 * length );
width = ( 2 * width );
Perimetercoke = Perimeter ( length, width );
cout << " The Perimetercoke of the rectangle is: " << Perimetercoke << endl;
return 0;
}

unsigned long int Perimeter ( unsigned short int length , unsigned short int width )
{
  return  ( 2 * length ) + ( 2 * width );
}

Thought the same variable name and function name was causing the problem and there were no errors but 2 warnings:
Day5Bugbusting - Win32 Debug--------------------
Compiling...
Bug Busters.cpp
D:\MICROSOFT VISUAL STUDIO\VC98\BIN\DAY 5- BUG BUSTING\Day5Bugbusting\Bug Busters.cpp(13) : warning C4700: local variable 'length' used without having been initialized
D:\MICROSOFT VISUAL STUDIO\VC98\BIN\DAY 5- BUG BUSTING\Day5Bugbusting\Bug Busters.cpp(15) : warning C4700: local variable 'width' used without having been initialized

Bug Busters.obj - 0 error(s), 2 warning(s)

and the output was horrible like :
" Please enter the length of the rectangle : 52428
6
Please enter the width of the rectangle : 52428
7
The perimeter coke of the rectangle is : 52 "

nasty bug ..argh!

Try this (your function has the same name as a varibale( Parimeter=Parimiter()):

#include <iostream.h>
unsigned long int Perimeterf ( unsigned short int , unsigned short int ) ;

int main (void)
{
unsigned short int length ;
unsigned short int width ;
unsigned long int Perimeter ;

cout << " Please enter the length of the rectangle: " << length << endl ;
cin >> length ;
cout << " Please enter the width of the rectangle: " << width << "\n" ;
cin >> width ;


length = 2 * length ;
width = 2 * width ;
Perimeter = Perimeterf ( unsigned short int length , unsigned short int width ) ;
cout << " The Perimeter of the rectangle is: " << Perimeter << endl ;
return 0 ;
}

unsigned long int Perimeterf( unsigned short int length , unsigned short int width )
{
return 2 * length + 2 * width ;
}

you also have a mistake in your code, you multiply length and width by two twice (func(perimiter) and length = 2 * length ;
width = 2 * width ;)
Ilya
P.S.: Hmm.. when I entered I was able to see only one message but now I se all of them)

Hello.Valmian.I guess you have'nt compiled your code.You don't need to.Could this be the problem with my compiler? I installed the visual studio enterprise edition in d drive but it also got installed in program files so to save the space in c drive program files i saved my projects in d drive...I followed your advice..it works , though not perfectly :

#include <iostream.h>
unsigned long int perimeter( unsigned short int,unsigned short int) ;

int main (void)
{
unsigned short int length ;
unsigned short int width ;
unsigned long int Perimetercoke ;

cout << " Please enter the length of the rectangle: " << length << endl ;
cin >> length ;
cout << " Please enter the width of the rectangle: " << width << "\n" ;
cin >> width ;


Perimetercoke = perimeter( length ,width);
cout << " The Perimetercoke of the rectangle is: " << Perimetercoke << endl ;
return 0 ;
}

unsigned long int perimeter( unsigned short int length , unsigned short int width)
{
return 2 * length + 2 * width ;
}

the error report was:

confusion.cpp
D:\Microsoft Visual Studio\VC98\Bin\Day 5- Bug Busting\perimeterconfusion\confusion.cpp(10) : warning C4700: local variable 'length' used without having been initialized
D:\Microsoft Visual Studio\VC98\Bin\Day 5- Bug Busting\perimeterconfusion\confusion.cpp(12) : warning C4700: local variable 'width' used without having been initialized

confusion.obj - 0 error(s), 2 warning(s)
and the output:
please enter the length of the rectangle: 52428
5
please enter the width of the rectangle: 52428
5
the perimetercoke of the rectangle is:20"
Thankx a lot to you for pointing out my stupid mistake it would take me extra 4 hours to make the code work only by myself.This is why I like this forum - experts can revise their expertise by solving beginner's probs... :)

thanks 4 ur email camelNotation - i wish i could help but unfortunately i don't know anything, as yet, about c++

Try the following, but note that I'm using a compiler that doesn't have <iostream.h> so I'm using the standard header <iostream> in its place and using namespace std.

#include <iostream>

using namespace std;

long int perimeter(int, int) ;

int main()
{
    int length ;
    int width ;

    cout << "Please enter the length of the rectangle: " << flush ;
    cin >> length ;
    cout << "Please enter the width of the rectangle: " << flush ;
    cin >> width ;
    cout << "The Perimeter of the rectangle is: " << perimeter( length ,width) << endl ;

    return 0 ;
}

long perimeter(int length , int width)
{
    return (length + width) * 2 ;
}

I've made some minor changes, hopefully they won't confuse you too much.

The error you were getting:
- local variable 'length' used without having been initialized
is because you were outputting the value of length:
- cout << " Please enter the length of the rectangle: " << length << endl ;

before it has been assigned a value, so it is uninitialised, and local non-static variables like this that haven't been initialised have a random garbage value. In fact you don't need to output the value at this stage, compare your version with mine.

I've also used flush instead of endl, to flush the buffer but without a newline, so the user input is on the same line as the prompt.

I made a minor tweak to the return expression from your parameter function.

Also, the paremetercoke variable, in this program at least, isn't required as you can output the temporary value returned from the function directly (if you wish to, that is).

I would suggest using int as a general case rather than unsigned int or unsigned short int, unless you have a good reason to. Perhaps you were trying to avoid overflow in your program, which is fine (and I haven't) but you might need something more sophisticated in terms of error handling anyway, so I wouldn't worry too much about it at this stage (unless you're required to, if this is an assignment).

Hope that helps a little.

Thankx for the post Bob - forwards as backwards :)
I got my problem sorted out it was what Valmian pointed out , basically .
I was multiplying the length and the width twice , nothing else. The reason I have been using unsigned short int is that my compiler takes int as long it-4 bytes.I still have loads of projects to work on so I don't wanna fill up my hard drive... :)

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.