Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

varadic functions in C++


  • Please log in to reply
7 replies to this topic

#1 comet@earth

comet@earth

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Local time:11:51 PM

Posted 12 July 2010 - 01:12 PM

this open parameter list program is not working....kindly help me debug the code below#include<stdarg.h>
#include<iostream.h>
#include<conio.h>
float ag(int,...);
void main()
{
int count=0;
cout<<"Enter the last fixed parameter:";
cin>>count;
float avg=ag(count,...);
cout<<"THE AVERAGE IS:"<<avg;
}
float ag(int count,...)
{
va_list ap;
int j;
float a=0.0;
va_start(ap,count);
for(j=0;j<count;j++)
a+=va_arg(ap,int);
va_end(ap);
return(a/count);
}

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:21 AM

Posted 12 July 2010 - 02:24 PM

Well, you've got several issues here.
#include<stdarg.h>
#include<iostream.h>
should probably be
#include <cstdarg>
#include <iostream>
to match C++03.

#include <conio.h>
is not a C++ header, and has never been a C++ header. It is a nonstandard extension which was put into place by some Borland compilers. Are you using a Borland compiler?

float ag(int,...);

void main()
{
	int count = 0;
	std::cout << "Enter the last fixed parameter:";
	cin >> count;
	float avg = ag(count,...); // You have to provide arguments here.
	cout << "THE AVERAGE IS:" << avg;
}
float ag(int count,...)
{
	va_list ap;
	int j;
	float a=0.0;
	va_start(ap,count);
	for(j=0;j<count;j++)
		a+=va_arg(ap,int);
	va_end(ap);
	return(a/count);
}

You've provided no varadic arguments to the function. That's almost certainly not what you want.

Varadic functions are discouraged in C++ anyway because the are not type safe. Is there any reason you can't solve your problem by pasing ag a vector of floats instead?

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 comet@earth

comet@earth
  • Topic Starter

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Local time:11:51 PM

Posted 12 July 2010 - 10:35 PM

I am an engineering student.In my college they are using the Turbo compiler.I am also learning visual c++ side by side.Visual c++ is a bit difficult to operate.So for now i m sticking to turbo c++.Would you please send me some link where i can learn how to operate visual c++.Now,in the program you said that i haven't given arguments.Well i will try writing the source code again with arguments.Please send me the correct code also.Thank u

Edited by comet@earth, 12 July 2010 - 10:36 PM.


#4 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:11:21 AM

Posted 12 July 2010 - 11:27 PM

It's not a Visual C++ thing, it's a standards-compliance thing. Generally speaking, one should avoid non-standards-compliant behavior. Turbo C++ has not been maintained in several years, and is far enough into obscurity and is dated enough that you'd be hard pressed to find standards-compliant code that will run on it.

As far as Visual Studio goes however, I don't see what's all that complicated. You say new project, pick C++ windows application, and start typing code.

I can't send you correct code because I have no idea what you're really trying to accomplish. And as you've said this is for study I'm leery of providing verbatim source code.
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#5 comet@earth

comet@earth
  • Topic Starter

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Local time:11:51 PM

Posted 14 July 2010 - 11:36 AM

sir i m trying to write a program to print the average of an arbitary amount of numbers.plz send me the code

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:21 PM

Posted 14 July 2010 - 07:24 PM

That's not how it works here. You provide your code, tell us what you think is supposed to happen, and what really happens, and we help you fix it.

#7 Romeo29

Romeo29

    Learning To Bleep


  • BC Advisor
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:01:21 PM

Posted 14 July 2010 - 09:56 PM

Old DOS based IDE of TurboC++ was last updated in 1991-92. This is obsolete.
In 2006, Borland revived Turbo C++ and released its free version as Turbo C++ Explorer. This is updated.
In 2009, Borland discontinued Turbo C++ Explorer in favor of their C++ Builder product.

If you are using the Turbo C++ Explorer, then you should be fine.

#8 comet@earth

comet@earth
  • Topic Starter

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Local time:11:51 PM

Posted 15 July 2010 - 02:09 AM

#include<stdarg.h>
#include<iostream.h>
#include<conio.h>
float ag(int,...);
void main()
{
int count=0;
int a[10];
cout<<"Enter the numbers"<<endl;
for(int i=0;i<10;i++)
cin>>a[i];
cout<<"Enter the last fixed parameter:";
cin>>count;
float avg=0.0;
avg=ag(count,a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
cout<<"THE AVERAGE IS:"<<avg;
}
float ag(int count,...)
{
va_list ap;
int j;
float a=0.0;
va_start(ap,count);
for(j=0;j<count;j++)
a+=va_arg(ap,int);
va_end(ap);
return(a/count);
}
ok,this is the code i have written.i want to find the average of an arbitary amount of numbers.Error trapping of the above code can be done easily,i think so.hw may i decrease the error trap points as well as make the code more user friendly.thank u.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users