How do I call book[j] in a void function in C++?
-
Friday, May 04, 2012 2:47 AM
void WSU()
{
if(input1 == "open" && input2 == "book")
{
cout << "...you are studying... " << book[j] << endl;
}
j = j + 1;
}Is it possible to do this or do I have to alter the program?
matthew g mutch
All Replies
-
Friday, May 04, 2012 3:20 AM
It's possible if input1, input2, book, and j are all defined at file scope. Otherwise they need to be passed in as parameters.
Most of the time, defining variables at file scope causes more problems than it solves.
-
Friday, May 04, 2012 3:35 AM
I have no idea how to pass in a string as parameters... ?
Can you give me an example?
matthew g mutch
-
Friday, May 04, 2012 3:58 AM
#include <cstdlib> //
#include <iostream> // cout feature
#include <string> // use of strings
#include <sstream> //
#include <iomanip> // setw command
using namespace std;volatile int i = 1;
volatile int j = 1;
string input1;
string input2;int main(int argc, char** argv)
{do {
const string book[9] = {"",
"Travels in the Congo",
"West African Studies",
"Astronomical Almanac 2011",
"C++: Starting Out with Early Objects",
"OpenGL Programming Guide",
"Mathematics for 3D Game Programming and Computer Graphics",
"Engineering Problem Solving with C++"};getline(cin,input);
istringstream is(input);
is >> input1 >> input2;}
if(i == 2)
{
WSU();}
}while(input1 != "quit");
void WSU()
{
if(input1 == "open" && input2 == "book")
{
cout << "...you are studying... " << book[j] << endl;
}}
Here is the barebones version of the problem...
matthew g mutch
-
Friday, May 04, 2012 4:00 AM
i has a similar defintion as j but I forgot to put in the code above but hopefully it gives you a better picture...
matthew g mutch
-
Friday, May 04, 2012 4:04 AM
A smarter way is to pass parameters to the WSU() function, as in:
void WSU (string& s1, string& s2)
{
if (s1 == "open" && s2 == "book")
{
// etc. etc.
}
}
-
Friday, May 04, 2012 4:37 AMI would like to read the strings in the book array not from the input....
matthew g mutch
-
Friday, May 04, 2012 5:00 AMLogically, explain to me how you decide which book is being studied? Using pseudo-code, what is your algorithm? It certainly is not clear from what you have posted.
-
Friday, May 04, 2012 5:12 AM
I am writing a text based adventure and I will be placing the books in different locations through out the gaming environment.
I am certain I will need to do one of two things:
1. Place the book references in the main() code.
or
2. Place the book references in a void() code.
My programming experience is limited to the internet and the books I have...
that being said I would think that at this point I would like to somehow
pass the string book array to the void function if it is not too difficult...
What I am avoiding right now is learning about Public and Private Classes
which may be of use for this problem... just if you could advise me on the
next logical approach I would appreciate it...
matthew g mutch
-
Friday, May 04, 2012 7:56 AM
why can't I do this?
void location(int argc, char** argv);
void book(int argc, char** argv);
void music(int argc, char** argv);int main(int argc, char** argv)
{location(int argc, char** argv);
cout << main_character << "... " << endl;
cout << location[i] << endl;
book(int argc, char** argv);
cout << "...you are reading " << book[j] << endl;
music(int argc, char** argv);
cout << "...you are listening to " << music[k] << endl;
cout << "What's the business?" << endl;// etc. etc.
}
and then
void location(int argc, char** argv)
{
const string location[13] = {"",
"you are in the heart of the city of Detroit...",
"you are at Wayne State University...",
"you are at Jacoby's German Biergarten...",
"you are at Hart Plaza",
"you are on the Eastside",
"you are at the Cosmic Cafe...",
"you are in the New Center area",
"you are along the Cass Corridor",
"you are at the Mediterranean Cafe",
"you are at the MGM Grand",
"you are at the banks of the Detroit River"};
}?????????????????????
matthew g mutch
-
Friday, May 04, 2012 9:06 AM
What you're doing it that code is meaningless.why can't I do this?
void location(int argc, char** argv);
void book(int argc, char** argv);
void music(int argc, char** argv);int main(int argc, char** argv)
{location(int argc, char** argv);
cout << main_character << "... " << endl;
cout << location[i] << endl;
book(int argc, char** argv);
cout << "...you are reading " << book[j] << endl;
music(int argc, char** argv);
cout << "...you are listening to " << music[k] << endl;
cout << "What's the business?" << endl;// etc. etc.
}
You're not calling any of your functions from main().
All you've done is repeat the function prototypes.
I suggest you review how to use functions:
Functions
http://www.cprogramming.com/tutorial/lesson4.html
Functions (I)
http://www.cplusplus.com/doc/tutorial/functions/
Functions (II)
http://www.cplusplus.com/doc/tutorial/functions2/
Also, where are there any *arrays* named location, book,
and music?
>cout << location[i] << endl;
>cout << "...you are reading " << book[j] << endl;
>cout << "...you are listening to " << music[k] << endl;
- Wayne -
Friday, May 04, 2012 9:24 AM
I thought that this was an array:
const string location[13] = {"",
"you are in the heart of the city of Detroit...",
"you are at Wayne State University...",
"you are at Jacoby's German Biergarten...",
"you are at Hart Plaza",
"you are on the Eastside",
"you are at the Cosmic Cafe...",
"you are in the New Center area",
"you are along the Cass Corridor",
"you are at the Mediterranean Cafe",
"you are at the MGM Grand",
"you are at the banks of the Detroit River"};matthew g mutch
-
Friday, May 04, 2012 9:33 AM
>I thought that this was an array:
>const string location[13] = {"",
> "you are in the heart of the city of Detroit...",
[snip]
Sure it is. But that's in a function called location().
It's not known to the main() function. In main you are
referencing three arrays in the cout lines I quoted. They
don't exist as local variables in main(), and I don't
see them defined as global variables either. So how
would the main() function be able to use them?
- Wayne- Marked As Answer by NK-7 Friday, May 04, 2012 9:48 AM
-
Friday, May 04, 2012 9:37 AMOK ...I will try to make them global variables and see what happens...
matthew g mutch
-
Friday, May 04, 2012 9:49 AMYou're a genius ...it works now!!!!!
matthew g mutch

