Cant call function

I'm trying to call this function but it wont and I'm not sure how to get this to call. thank you in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
	//variables
	double numV = 0;

	nameEvent(locN, orgN);
	cout << locN << endl;

	
	system("pause");
	return 0;
}

void nameEvent(string locN[], string orgN[])
{
	//ask for the location
	std::string locationN[50];
	std::cout << "Enter name of location: ";
	std::cin >> locationN[0];
	'\n';
	
	//ask for name of org
	std::string orgN[50];
	std::cout << "Enter name Volunteer event: ";
	std::cin >> orgN[0];
	'\n';
}
Also on that note how do I make my string arrays dynamic?
What to you mean by "it won't".

One obvious problem is you have no function prototype for nameEvent. When the compiler parses line 13, it does not know what nameEvent looks like.

how do I make my string arrays dynamic
 
  string * orgN = new string[50];

Don't forget to release the memory.
I'm completely new to this. how do I make a function prototype.
well you don't NEEd a prototype, but the problem is this:
nameEvent(locN, orgN);

when you call this function, you need to pass by 2 parameters, which in this case both need to be strings

but what you are doing is you are passing in undeclared variables, what you should do before you call the function:

string one, two;

nameEvent(one,two);


okay so I ran it but it gave me an error I tried what you showed my but its still not working :/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main()
{
	//variables
	double numV = 0;
	string locN, orgN;
	nameEvent(locN,orgN);
	

	
	system("pause");
	return 0;
}

void nameEvent(string locN[], string orgN[])
{
	//ask for the location
	std::string locationN[50];
	std::cout << "Enter name of location: ";
	std::cin >> locationN[0];
	'\n';
	
	//ask for name of org
	std::string orgN[50];
	std::cout << "Enter name Volunteer event: ";
	std::cin >> orgN[0];
	'\n';
}
When you call
void nameEvent(string locN[], string orgN[]) on line 13 your compiler thinks it doesn't exists yet. It looks for a declaration (a prototype) that it can look at BEFORE the current line.

But you declared and defined it below main. It is not visible to any of the code above it unless you put a declaration before main.

Your compiler goes line-by-line. If you have no defined something BEFORE that line, the compiler will not recognize its existence.

Just add
void nameEvent(string locN[], string orgN[]); before main and then your code in main will know what 'namedEvent' looks like. You can keep the definition below main, that's fine.
1
2
	nameEvent(locN, orgN);
	cout << locN << endl;


Those variables are undefined. You didn't declare them anywhere.
Last edited on
it says name identifier not found "nameEvent"
oh okay so copy the void function and put it before main. okay ill try that
Exactly. Look at what I posted above.

You did not give a prototype BEFORE that function call that your compiler can look at. When it parses nameEvent(locN,orgN);, it hasn't actually found that function yet since you declared it AFTER the call. You need to provide a declaration that your compiler can see so that it know what namedEvent is.
okay so it now see the function but its giving me an error with the nameEvent(locN~ and orgN~)
Boop. Another note.


Inside namedEvent, you have these lines:

1
2
3
4
5
	//ask for name of org
	std::string orgN[50];
	std::cout << "Enter name Volunteer event: ";
	std::cin >> orgN[0];
	'\n';


You are redefining your argument. Your compiler will not like that.
this is what I have so far. I appreciate the help your giving me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include "volunteer.h"

using namespace std;

void nameEvent(string locN[], string orgN[]);

int main()
{
	//variables
	double numV = 0;
	string locN, orgN;
	nameEvent(locN, orgN);

	cout << locN << " " << orgN << endl;
	

	
	system("pause");
	return 0;
}

void nameEvent(string locN[], string orgN[])
{
	//ask for the location
	std::string locationN[50];
	std::cout << "Enter name of location: ";
	std::cin >> locationN[0];
	'\n';
	
	//ask for name of org
	std::string orgN[50];
	std::cout << "Enter name Volunteer event: ";
	std::cin >> orgN[0];
	'\n';
}
Why are you using arrays of strings?
You just want to get one string from the user, which the location, right?
And then you want one more string, which is the name of the volunteer event, right?

std::string locationN[50]; That creates an array of 50 strings.
 
'\n';
What is this doing? It's just sitting off by itself.
 
std::string orgN[50];
Redefinition of argument string orgN[]. Compiler will be unhappy.

It looks like you want to use a function to get input, but you want to remember that in main, right?

A better way to do this would be to send the strings in by reference. When you do that, any changes to the strings inside of your function will be reflected in the same strings that you passed into the function.
so what I was doing with the '\n' was to create a new line. what I'm trying to do is get information from the user of the name of their organization and the location of it which will be done in the function. then passed to the main where it'll be displayed.
and also how do I pass by reference I haven't looked into that yet.
Hmm. I see. I won't do something you haven't learned yet. That would just piss off your teacher.

I'll avoid using anything that I think you might not have learned.

Alright. How about you use two different functions for input? Each function can only return one value, so you can use two functions - one to get the location, and one to get the activity.

Why not try something like this?

1
2
string getLocation();
string getVolunteerEvent();


Those functions don't take any arguments, but they will return a string. You could get a string from the user inside the functions and then return the strings that the user enters and store that in a variable declared in main.
oh okay ill do that then. thank you!
Topic archived. No new replies allowed.