?
Solved

string

Posted on 2006-05-06
17
Medium Priority
?
269 Views
Last Modified: 2010-04-01
Hey, how do I write a C++ function named display that takes any number of arguments. The first argument must be an interger. The remaining arguments will be C-style strings (character pointers). The first argument will indicate how many strings the call contains. The function will print the strings on a single line, with arguments seperated by one space. For example, the call
     display (2, "abc", "def");
will produce the following output:
     abc def

You know what I mean?
0
Comment
Question by:shahrine99
  • 6
  • 6
  • 5
17 Comments
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 600 total points
ID: 16623797
Hi shahrine99,

This is obviously homework .. refer to these tutorials ... Try to write your function ... If you face any problems, post here and we would help
http://www.cprogramming.com/tutorial/lesson17.html
http://www.codersource.net/c++_variable_argument_functions.html

Cheers!
Sunnycoder
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16623853
Hi, I will give you some points...

1) #include <stdarg.h> (in case of C) or <cstdarg> (in case of C++)

2) Use va_list to create a pointer to the arguments.

3) Use va_start to initialize the pointer to the last known parameter. (In this case, the numeric argument)

4) Put a for loop to N no. of times, and use va_arg to iterate through each of the arguments. You need to use char* as the second parameter to the va_arg. Print each of the obtained value.

5) Use va_end to clear the pointer.


Let us know if you have any problem writing the code

---
Harish
0
 

Author Comment

by:shahrine99
ID: 16623861
nah its not a homework question..im cramming for final exam..and im just looking over the study guide..that question was on the study guide..so i didnt not understand that....my guess would be like

int display = 4;
cout << display << "1" << "2" << "3" << "4" << end;

no?



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:shahrine99
ID: 16623862
im suppose to write a program? i thought its just a small code
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16623863
Its about 10 lines of code ... Give it a try ...

>int display = 4;
>cout << display << "1" << "2" << "3" << "4" << end;
>
>no?
No :)

 display (2, "abc", "def");
will produce the following output:
     abc def
0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 600 total points
ID: 16623866
Well, is there no example in that study guide ?

From Sunny's link..

include <cstdarg>
#include <iostream>

using namespace std;

void display( int num, ... )
{
  va_list arguments;                     // A place to store the list of arguments

  va_start ( arguments, num );           // Initializing arguments to store all values after num
  for ( int x = 0; x < num; x++ )        // Loop until all numbers are added
    cout <<va_arg ( arguments, char* ); // Adds the next value in argument list to sum.
  va_end ( arguments );                  // Cleans up the list
}

int main()
{
  display(3, "abc", "def", "ghi");
  display(2, "mno", "pqr");
}
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16623868
Harish,

Which of my links has that piece of code ?

sunnycoder
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16623869
LOL I edited the program given in the first link to suit the asker's requirement. I didn't remove the comments, however.

:)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16623872
Isn't that the idea of posting a helpful link for a student? They have to read and understand and make little modifications to tailor it for their needs.

They need to change just a line or two but they need to understand the concept to make those changes.

Now that you have made those changes too, I guess they have nothing left to learn.
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16623876
I agree. But even after your comment was posted, he was asking how to do that. So I changed it.
0
 

Author Comment

by:shahrine99
ID: 16623877
dang aight sunny daddy....lol...im learning haha
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16623879
That was because they did not make any efforts on their own ...
0
 

Author Comment

by:shahrine99
ID: 16623882
whoa...harish...ur code is confusing me...wat in da world va_start and va_list are?
0
 

Author Comment

by:shahrine99
ID: 16623884
jeez sunny daddy..i feel dishearten =)...have sympathy for me...me cramming for final exam...my head is spinning lol
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16623886
Read my first comment
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16623888
> wat in da world va_start and va_list are?

In C/C++ world, they are macros defined in stdarg.h or cstdarg
0
 

Author Comment

by:shahrine99
ID: 16623891
my bad...i was scrolling too fast...thanks =)
thanks to sunny too for the links =)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question