Having a problem with functions, function not working

Hi,i'm supposed to write an input function which ask the user to input a char. This is my first time using functions, and my function doesn't display anything when I'm compiling the code, and i'm not sure what I'm doing wrong.

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
  #include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

//overloaded input functions
char input(string prompt, string error_message);
double input(double max_num, double min_num, string prompt, string error_text);
void input (int pointx, int pointy, string prompt);

//overloaded distance functions
int distance(char a, char b);
double distance(double num1, double num2);
double distance(double x1, double y1, double x2, double y2);

//Overloaded display functions
void display(const string &msg, char ch1, char ch2);
void display(const string &msg, double num1, double num2);
void display(const string &msg, double x1, double y1, double x2, double y2);

int main() {
	cout <<"Display the function between two items: letters, numbers, points."<<endl;
	
	return 0;
}
	char input (string prompt, string error_message){
		char letter;
		prompt = "Enter first letter (a to z):";
		cout<<prompt;
		cin>>letter;
          return letter;
	}
	
Where are you calling your input function in main? No where.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

char input(std::string prompt, std::string error_message);

int main()
{
	std::string prompt = "Enter first letter (a to z): ";
	std::string error { "Ooops, error!" };

	char letter = input(prompt, error);

	std::cout << "\nThe letter entered is " << letter << '\n';
}

char input(std::string prompt, std::string error_message)
{
	std::cout << prompt;
	char letter;
	std::cin >> letter;

	return letter;
}
Enter first letter (a to z): t

The letter entered is t


Preferred method for passing strings into a function would be by reference, making them const so they can't be modified in the function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

char input(const std::string&, const std::string&);

int main()
{
	std::string prompt = "Enter first letter (a to z): ";
	std::string error { "Ooops, error!" };

	char letter = input(prompt, error);

	std::cout << "\nThe letter entered is " << letter << '\n';
}

char input(const std::string& prompt, const std::string& error_message)
{
	std::cout << prompt;
	char letter;
	std::cin >> letter;

	return letter;
}
if I want to call this function twice but with a different prompt, wouldn't "const" cause an error?
No, using const wouldn't cause an error. The strings are only const within the function, not in main.
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
#include <iostream>
#include <string>

char input(const std::string&, const std::string&);

int main()
{
	std::string prompt = "Enter first letter (a to z): ";
	std::string error { "Ooops, error!" };

	char letter = input(prompt, error);

	std::cout << "\nThe 1st letter entered is " << letter << "\n\n";

	prompt = "Enter 2nd letter: ";
	error = "Another error!";

	letter = input(prompt, error);

	std::cout << "\nThe 2nd letter is " << letter << '\n';
}

char input(const std::string& prompt, const std::string& error_message)
{
	std::cout << prompt;
	char letter;
	std::cin >> letter;

	return letter;
}
Enter first letter (a to z): t

The 1st letter entered is t

Enter 2nd letter: w

The 2nd letter is w

Topic archived. No new replies allowed.