Question

Hello,

Since I'm working on a generator and I don't want to have to put the same lines at every category, since that takes to much time and I haven't got that many time.

How do I make a block of code useable in 1 or 2 lines? Eg. below.

"Text Above":

"... costs: 3 gold".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdlib>
#include <iostream>

int commerce;

using namespace std;

int main(int argc, char *argv[])
{

cout << "Choose your category: 1 or 2.";
cin>> commerce;
     switch (commerce)
     {
      case 1: cout << "Say Text Above";
      break;
      case 2: cout << "blablabla";
      break;
     } 


}


I'm sorry if it is unclear, just give me a call.

Darryl
Last edited on
Are you talking about a sub-program/function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#include <iostream>

void someFunction(void); //no parameters, this is the prototype

int main()
{
    someFunction(); //calling the function with no variables or values as parameters
    return 0;
}

void someFunction(void) //declaring the function, again no parameters
{
    std::cout << "Hello, world!" << std::endl;
}
Hello, world!

http://www.cplusplus.com/doc/tutorial/functions/
http://www.learncpp.com/ <---chapter 7
I'm not sure. I mean something like what the cin>> does.

Eg.
1
2
3
4
5
6
7
int name; 


cout << "What's your name?";
cin>> name;

cout << "Your name is: " << name; // not sure about this line, just typed something. 


Last edited on
Just put the stuff you want to be repeated in functions like I mentioned before then call that function when ever you want that stuff to appear. If you want to add extra things to output as well give the function a parameter. PS a name should be a string of characters not an int :P. You could also (though probably not the best) is assign the repeated messages to a string like
1
2
3
4
std::string const message = "Some message to be repeated.";

//then do something like
[code]std::cout << message << " other stuff" << std::endl;
Topic archived. No new replies allowed.