C++ :: Functions With Variable Number Of Arguments
Apr 21, 2014
Write a function write with variable number of arguments that takes a string first argument followed by any number of arguments of type double and prints on the screen a string formatted by the rules described below. The first argument may contain formats in curly braces of the form {index[:specifier]}, where the square brackets show optional parts (this is :specifier may be missing), and index is the sequence number of an argument of type double (starting from sequence number 0).
Rules for formatting: In the printed string the curly brackets and their content will be replaced by the argument with the given index, formatted according to the given format specifier. If the format specifier is missing, the argument will be printed with its default format. For example:
write("The number {0} is greater than {1}.", 5, -3);
will print
The number 5 is greater than -3.
write("There are no format specifiers here.");
will print
There are no format specifiers here.
The format specifiers and their meanings are listed in the following table
Specifier MeaningFormat Output for 1.62 Output for 2.0
none default {0}1.62 2
ccurrency{0:c}$1.62 $2.00
escientific{0:e}1.620000e+000 2.000000e+000
ffixed point{0:f}1.620000 2.000000
iround to int{0:i}2 2
Limitations: You may limit the maximum number of arguments your function can process to a certain value, for example 10.
Suggested extensions:
-Add an optional alignment specification in the format , e.g., make the format of the form {index[,alignment][:specifier]}, where alignment is an integer specifying the width of the field in which the corresponding argument will be printed. If alignment is positive, align to the right, if it is negative, align to the left.
-Accept an optional integer after the specifier letter, specifying the required precision in the output. For example, {0:f2} will print the number 1.6234 as 1.62, but {0:f5} will print it as 1.62340.
View 1 Replies
ADVERTISEMENT
Mar 6, 2015
I need to create dynamic string by given format(%d,%s,%f,%lf,%c) using variable number of arguments in function. This code gives me an error(main.exe has stopped working):
Code:
#include<stdio.h>
#include<stdarg.h>
#include<string.h>
#include<stdlib.h>
char *form(char *format,...);
char *form(char *format,...)
[Code]...
I assume the error is in functions(itoa,fcvt,ecvt).
View 1 Replies
View Related
Mar 14, 2015
I need to create dynamic string by given format(%d,%s,%f,%lf,%c) using variable number of arguments in function. This code gives me an error(main.exe has stopped working):
#include<stdio.h>
#include<stdarg.h>
#include<string.h>
#include<stdlib.h>
char *form(char *format,...);
char *form(char *format,...)
[Code] ....
I assume the error is in functions(itoa,fcvt,ecvt).
View 2 Replies
View Related
Feb 4, 2013
Having a little trouble passing arguments to functions.
I wrote this simple program to get the hang of it but I'm quite stuck. I'm sure you will be able to get at what i want the program to do...
Code:
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <windows.h>
void menu(int HP,int Gold,int Armour);
[Code] ....
View 3 Replies
View Related
Aug 23, 2013
I'm having some problems with a function. The function is supposed to find the two largest values in an array.
Code:
void find_two_largest( const int *a, int n, int *largest, int *second_largest){
largest = a;
int temp;
second_largest = a;
for ( int i = 1; i < n; i++){
if (*(a + i) > *largest){
temp = *largest;
[Code]....
I don't see any mistake with the code of the function, but when I try to call it inside my program it only returns 0 for both largest and second_largest.
Code:
int *find_middle( int *a, int n);
void find_two_largest(const int *a, int n, int *largest, int *second_largest);
int main()
{
int n;
[Code]...
Do I have to declare the variables largest and second_largest as normal integer variables and then pass their addresses as arguments to find_largest or is that incorrect?
View 10 Replies
View Related
Sep 6, 2013
How to pass arguments from other functions to main. i want to write a program like nano well not exactly like nano editor. I have a function f_read(char* filename[]), and fopen() get filename[1] as file name and *filename[2] as "r" read mode and rest of the code will read from a file.
I want is this char filename[] to main(int argc , char argv[])
how can i do that??
View 4 Replies
View Related
Mar 12, 2014
I have a program that is working very well when I pass C++ vectors as arguments to my functions by reference, but I get some compilation errors when try to make a modification. I am also posting the entire program and its output below. so that you can see what is going on. I have commented out the line that causes an error.(Some of the indentation that got corrupted when I copied the code to the browser.)
This program basically calculates the coefficients of a least square polynomial and then evaluates this polynomial at artificial data points and verifies that this actually reproduces the original data within reasonable floating point error.
The function that computes the coefficients of the least square polynomial is Code: vector<double> LSPVecValued_GSL( const int, const vector<float> &, const vector<float> &); and as you can see it returns a vector by value, and this vector contains the coefficients of the least square polynomial.
There is also a function that evaluates this polynomial by accepting a vector argument by reference : Code: float evaluate_polynomial(double, vector<double>& ) ; I have also created another version of the evaluation function which accepts the same vector argument by value: Code: float evaluate_polynomial_ByValue(double t, vector<double> vec_a) ; In the program I call the first evaluation function (whose vector argument is passed by reference) by first using an intermediate vector variable containing the coefficients, and then I pass this vector as an argument to the evaluation function, as follows:
Code:
vec_a = LSPVecValued_GSL( deg, vec_x , vec_y);
for(int j=0; j< n ; j=j+20 ) {
cout<<"x["<<j<<"] = " << vec_x[j] << " ,y["<<j<<"] = " << vec_y[j] <<" , p(x["<<j<<"]) ( EVALUATED FROM REFERENCE) = "
<< evaluate_polynomial( vec_x[j], vec_a) << endl; // This version works without error
[Code] .....
As you can see above, I am also able to call the second evaluation function (the one whose vector argument is passed by value) directly by plugging in the function LSPVecValued_GSL"(...)" and this works without error, and this is a one step process, only one line of code is involved.
However, I get a compilation error (line number 12 that I have commented out above) if I try to plug in the function "LSPVecValued_GSL(...)" into the first evaluation function that expects a vector argument by reference. I tried to put a "&" in front ofLSPVecValued_GSL but this did not fix the bug.
What syntax is appropriate to use the first evaluation function (which accepts a vector argument by reference) if I want to plug in the vector-valued function LeastSquarePolynomial_GSL directly in the the first version of the evaluation function which expects a vector argument by reference?
View 14 Replies
View Related
Feb 2, 2013
There are, or course, better ways to do this, but I need to stick to some rules:
(1) Use only pointer variables and not arrays or structs.
(2) Use the three functions shown--regardless of easier methods.
The program should ask for some input, operate on those numbers, and then display the results. I know I am confused over these things:
(1) All that syntax using '*' and '&' or neither.
(2) How to use the char type correctly.
(3) How to use a char type input as an operator (a + b).
(4) How to use the pointer of the operator variable (+,-,*,/) in an actual equation.
Code:
#include <stdio.h>
#include <stdlib.h>
// *** Prototype Functions ***
void Post_Results (float*);
void Calculate (float*, float*, char*, float*);
void Get_Numbers (float*, char*, float*);
[Code]......
View 5 Replies
View Related
Dec 6, 2014
what should be the prototype for the following function.
Code:void addition(int x, ...);
I am getting compilation errors. I have written the prototype as :
Code: void addition(int, va_list);
View 3 Replies
View Related
May 15, 2013
In the following code:
#include <iostream> // For stream I/O
using namespace std;
int function(int a) {
return a;
}
int main() {
function(int b);
}
Why is creating a variable inside the function argument list not allowed. Any reason other then for the language syntax or just for the language syntax?
View 19 Replies
View Related
Nov 3, 2013
What I have is a main function that takes input characters from the command prompt during the main function call, and coverts it to an integer array a using atoi. (starting at the 2nd character - the 1st is reserved for another call that I plan to reference later, and the 0th is obviously the ./function). A function is then called to find the mode of an array (the range of values in the array is 1-30). Now, when I run the whole thing, I get a segmentation fault (core dumped) for even number of arguments. It's late and I've been staring at it for too long...
Code:
#include <stdio.h>
#include <stdlib.h>
int get_mode(int a[], int count);
[Code]......
View 1 Replies
View Related
Aug 19, 2013
i'm trying building my how Write() function:
template <typename T>
....
Write(T Argument1[,T ArgumentX])
how can i calculate the numeber of Arguments added? (in C we used an argument for tells how many we putted in a function, but not in these case)
View 2 Replies
View Related
Mar 2, 2013
I am using thread on VC 2012 (very close to VC 2010). When the argument list is short, it works fine. However, when I add a function with more arguments, the compiler indicates "no thread constructor match the argument list....etc", and when I reduce the number of arguments, it works.
Is there a limit about thread constructor? I didn't see this in ISO C++11 standard. How can I fix this limit?
View 5 Replies
View Related
Mar 21, 2015
So, I'm supposed to do : Create a function with unlimited number of arguments, which forms a dynamic string based on the following form (%d, %s, %f, %lf, %c), with the following prototype:
char*create(char*form, ...);
The function is supposed to have the following output:
create("Peter is %d years old and is in %s-%c class.",7,"second",'A');
-> Peter is 7 years old and is in 7-A class.
create("His GPA is %lf.",4.96);
-> His GPA is 4.96.
create("His favourite subject is math!");
-> His favourite subject is math!
I've managed to do the following :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
char *create(char *form, ...) {
char *res =(char*)calloc(1,1),*pos_int,*pos_float,*pos_str,pos_char,*pos_long;
[Code] ....
The part with %d and %s string was not that hard, but now I'm supposed to convert %f and %lf to string, I've tried using sprintf but I've had no luck so far, another problem is the fact that I've gotta use lists to complete the task. I've been trying to convert float to string for the past 2 hours, but I'm drawing a blank now.
View 4 Replies
View Related
Oct 26, 2014
Write a function that takes 3 arguments. The function has to bring back a new number that has been generated with the replacement of the figure that is on a given position in the number with a figure that is been transferred as an argument(have in mind that the position of the figure is being counted from right to left,starting from one). Write a main program in which the newly formed numbers will be printed for numbers of a range written by the user.
Example: if you wrote the numbers 2276,3 and 5 the function should bring back the number 2576
If you didn't understand the text, the example shows that in the number 2276, the number has been counted from right to left by the second argument "3" and in the place of the figure "2" has been put the figure "5".
This is where I got stuck, I can't figure out how to make the replacement.
int argument(int x,int y,int z) {
return 0;
} int main() {
int a,b,c;
printf("Enter a value for a(100-999):");
scanf("%d",&a);
[Code] ....
View 3 Replies
View Related
Feb 19, 2014
So I have a template, part of a larger code, that is designed to calculate the number of multiplications it took to reach a certain number. The problem is, whenever I execute the program, mults is always printing out a strange number, perhaps its actual address.
template <class T>
T power3(T x, unsigned int n, unsigned int& mults) {
if (n == 0) return 1;
if (n == 1) return x;
if (n == 2){
[Code] ....
View 10 Replies
View Related
Mar 27, 2014
Some coding standards document to follow for naming a variable, functions and using braces for if etc and other important standards i have to follow while writing the code. Right now in a hurry to finish the program i am naming a variable what ever comes to mind which is creating a lot of problems for me.
View 2 Replies
View Related
Oct 11, 2014
I'm trying out friend functions not in just one source file to see how it works but I'm getting an error.
/* ----- ClassOne.h ----- */
#ifndef CLASSONE_H_
#define CLASSONE_H_
#include "classTwo.h"
using namespace std;
class ClassOne {
[Code] ....
Basically, I just want to print out the private members of ClassOne using ClassTwo's friend function twoPrintsOne().
The error is in classTwo.cpp and it says that m_a and m_b in the twoPrintsOne function are not declared in this scope.
View 3 Replies
View Related
Aug 31, 2014
So I have a class object that contains the private member variable spot and the public member function MoveLock. Within MoveLock, is a member variable called numbers that holds the place where a user is on a "lock knob". Now, what I'm trying to accomplish is that whenever the user turns the "knob" in the wrong direction, the position is updated with that current numbers so that the clicks needed to unlock the first state is also updated. But I get these errors:
Error E2096 C:Users...switchtest.cpp 34: Illegal structure operation in function main()
Error E2294 C:Users...switchtest.cpp 39: Structure required on left side of . or .* in function main()
Ultimately, what I have in main() is a piece of what I'm going to implement in a class member function. I'm also thinking about moving the if else statements out of the for and creating a second one for the else portion.
#include <iostream>
#include <windows.h>
#include <iomanip>
using namespace std;
HANDLE inKeys = GetStdHandle(STD_INPUT_HANDLE);
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
[code]....
View 10 Replies
View Related
Mar 13, 2014
Ok so when the program runs the first function the data is stored and displayed in the file. The second function is supposed to read the name entered, compare it to the ones in the file then take the price with it BUT I seem to have done something wrong when reading the files (or maybe it has to do with the global function I'm not sure). Here's parts of the code :
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
[Code]....
View 7 Replies
View Related
Mar 4, 2014
I am wondering is there a way to make function A uses random number stream say starting from srand(1) and function B uses random number stream say starting from srand(2) in the same program.
View 3 Replies
View Related
Oct 9, 2013
Last night I was trying to see if is possible to multiply one number to another without using any given function(like pow) or the multiply sign or whatever. Just using addition and subtract signs (+and -) with a for loop. It worked.
Now I am trying to understand if is possible to elevate one given number (inserted by the user) to another number without using again any function or the multiply / divide/ etc functions. Just add and subtract functions.
View 3 Replies
View Related
Jan 13, 2013
My source works perfect.The problem is that the (remove(),rename()) functions works but return nonzero number, so they fails all the time.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <fstream>
using namespace std;
int main() {
char array[100]={"1)open-write,2)open-read,3)rename,4)delete"};
[Code] ....
View 1 Replies
View Related
Feb 22, 2015
I'm having trouble finishing this program. What I'm trying to do is create a class for Rational numbers. Get input from user for denominator and numerator. Store the numbers without a sign, and for the sign store it separately in char. I'm not supposed to use accessor functions. The part that I can't seem to know how to do is use the info that was stored in the addUp function. This function need to add two rational numbers. i need to make this function general enough that it can handle two fractions, or a fraction and a whole number, or two whole numbers. What I already have here is readin function which reads in the numerator and denominator. setnumerator and setdenominator to assign positive values. setsign should get the sign of the fraction. Finally addUp should addUp which I explained before. I have some ideas about writing the tests, but can't seem to know how to implement it to the program. The main program is still empty but all I'm supposed to do there is call the class functions.
Code:
#include <iostream>
using namespace std;
class Rational {
private:
int numerator, denominator;
char sign;
[Code] .....
View 14 Replies
View Related
Nov 7, 2014
I am working on a number of utility functions for two dimensional arrays of integers, or matrices. However I am having a problem with segmentation faults, most likely due to errors in using malloc in functions like copyMatrix.
Code:
matrix_utils.h~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//This function checks if two matrices are equal
int isEqual(int **A, int **B, int n);
//This function returns one row of a matrix
int * getRow(int **A, int i, int n);
//This function returns one column of a matrix
int * getCol(int **A, int j, int n);
[Code] ....
View 2 Replies
View Related
Nov 16, 2013
I am writing this code, and I every time I run question A, no matter what numbers I put in, I get "larger = 0.
#include <iostream>
using namespace std;
int awesome ();
int best ();
int crazy ();
int main () {
char letter;
int smallestNumber;
int largestNumber;
[Code] .....
View 2 Replies
View Related