Writing function and combining them in the main loop of options

Hi, so i'm supposed to write functions: input, distance and display function and then combine them in the main loop of options. I wrote the functions and tried to combine them but whenever I test the input functions, i get an error message that isn't supposed to be there. it should only be there if a user enters an invalid option.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

//overloaded input functions
char input(const std::string&, const std::string&);
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() {
	char options;
	char letter;
	int  num;
	cout <<"Display the function between two items: letters, numbers, points."<<endl;
	while(true){
	cout << "Options: l)etter; n)umber; p)oint; q)uit:"<<endl;
	cin>>options;
		switch (options){
			case 'l':{
				string prompt ="Enter first letter (a to z): ";
				string error{ "Sorry, out of range. Try again." };
				char letter = input(prompt, error);
				prompt = "Enter second letter (a to z)";
				letter = input(prompt, error);
				
				break;
			}
			case 'n':{
				string prompt = "Enter first number (100 to -100): ";
				string error_text { "Sorry, out of range. Try again." };
				int num = input(prompt, error_text);
				prompt = "Enter second number (100 to -100)";
				num = input(prompt, error_text);
				break;
			}
		}
	}
		
	return 0;
}
	char input( const std::string& prompt, const std::string& error_message){
	char letter;
	std::cout << prompt;
	std::cin >> letter;
	while(true){
		if (isalpha(letter)==true){
			cout<<letter;
		}
		else{
		std::cin.clear();
    	std::cout << error_message<<endl << prompt<<char(std::cin.get());
		cin.ignore();
		}
	}
	return letter;
}
	double input(double max_num, double min_num, string prompt, const std::string& error_text){
		int num;
		min_num = -100, max_num = 100;
		cout<<prompt;
		cin >> num;
		while(true){
			if (num <= max_num){
				cout << num;
			}
			else if (num > max_num){
				std::cin.clear();
    			std::cout << error_text<<endl << prompt<<double(std::cin.get());
				cin.ignore();
				
			}
			if (num<=min_num){
				cout<<num;
			}
			else if(num<min_num);{
				std::cin.clear();
    			std::cout << error_text<<endl << prompt<<double(std::cin.get());
				cin.ignore();
				
			}
		}
		return num;
	}
Last edited on
Hello techielove,

Where is the rest of the program? There is no way to test it.

What error message? What does it say? Copy the complete compiler error message and post it.

Between you code being hard to read and not being able to run what you have posted it will take a bit to make your code run.

It is always best to post enough code that will compile and run and demonstrate your problem.

Andy
Hi, sorry i just edited it. my code is a little messy because I'm trying to figure out the errors but i'm not sure what is not working. It compiles but if i enter the option "l" it asks for the letter, but even if its a letter it says "Sorry try again" when that should only print if its not an alphabet or something.
how does it get out of the loop on line 55?
why does the n option use the letter input overload?
Hello techielove,

Your first code was full of errors and warnings.

Your 2nd code does compile without any errors, but it still has warnings that need to be addressed.

One warning for line 56 says that you are trying to compare an "int" to a "bool". Take a look at http://www.cplusplus.com/reference/cctype/isalpha/ especially the returned value.

The nice part of an "if" statement, "while" or "do/while" loop and a"for" loop is that what is between the () or the middle of the for loop is evaluated and converted to a "bool" variable, so most of the time a statement like: if (isalpha(letter)==true) can be shortened to just: if (isalpha(letter)). The returned value of the function is either (0)zero meaning false or anything other which is considered true.

In your number function you have an "else if" with a (;) that you do not need.

After that you need some blank lines in your code to make it more readable.

Keep in mind that the compiler will ignore white space, blank lines and comments when it compiles the code, so make it easy to read.

In "main" you have an endless while loop with no way out. Here is 1 option:
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
int main()
{
    char options{};  // <--- ALWAYS initialize all your variables.
    char letter{};
    int  num{};
    bool done{};
    std::string prompt;
    std::string error_text;

    std::cout << "Display the function between two items: letters, numbers, points.\n";

    while (!done)
    {
        cout <<
            "Options:\n"
            " 1)letter\n"
            " 2)number\n"
            " 3)point\n"
            " 4)quit:\n"
            "  Enter choice: ";
        cin >> options;

        switch (options)
        {
            case '1':
            //{  // <--- Not necessary.
                prompt = "Enter first letter (a to z):  ";
                error_text = "Sorry, out of range. Try again.";

                letter = input(prompt, error_text);
                letter = input(prompt, error_text);

                prompt = "Enter second letter (a to z)";
                //letter = input("Enter first letter (a to z): ", "Sorry, out of range. Try again.");

                break;
            //}
            case '2':
                prompt = "Enter first number (100 to -100): ";
                error_text = "Sorry, out of range. Try again.";

                num = input(prompt, error_text);

                prompt = "Enter second number (100 to -100)";

                num = input(prompt, error_text);

                break;
            case '3':
                break;
            case '4':
                done = true;
        }
    }

    return 0;  // <--- Not required, but makes a good break point for testing.
}

Look closely at case 1. You do not need the {}s for the case statement. better to define the variables outside the switch or even better look at line 34. You do not even need to define variables just send the quoted text to the function.

Again in the function for letters you have an endless while loop with no way out.
After some testing you do not need the while loop. It works fine with out it.

Have not looked at the 2nd function yet.

What I have done so far gives this output:

Display the function between two items: letters, numbers, points.

Options:
 1)letter
 2)number
 3)point
 4)quit:
  Enter choice: 1

Enter first letter (a to z): d
d

Enter second letter (a to z): g
g

Options:
 1)letter
 2)number
 3)point
 4)quit:
  Enter choice: 4



See what you can come up with.

Andy
Hello techielove,

After some further testing I found that the while loop in the letter input function is needed. What you do not need is the "cin.clear() and the "cin.ignore()". Since you are inputting into a "char" most all keys on the keyboard are valid with the exception of keys like: the function keys, "Insert", "Delete", "Home", "End" and others.
So there is nothing wrong with "cin" that needs cleared and there is nothing in the input buffer that needs ignored.

What I found that works best is:
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
char input(const std::string& prompt, const std::string& error_message)
{
    char letter;

    while (true)
    {
        std::cout << prompt;
        std::cin >> letter;

        if (isalpha(letter))
        {
            cout << letter << '\n';

            break;  // <--- A way out of the while loop.
        }
        else
        {
            //std::cin.clear();
            //std::cin.ignore(1000);
            std::cout << error_message << '\n';
        }
    }

    return letter;
}


Andy
As a first revision, perhaps consider:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
using namespace std;

//overloaded input functions
char input(const std::string& prompt, const std::string& error);
double input(double max_num, double min_num, const string& prompt, const 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.\n";

	char options {};

	do {
		cout << "Options: l)etter; n)umber; p)oint; q)uit: ";
		cin >> options;

		options = static_cast<char>(std::tolower(static_cast<unsigned char>(options)));

		switch (options) {
			case 'l':
			{
				const string error {"Sorry, out of range. Try again."};
				const auto letter1 {input("Enter first letter (a to z): ", error)};
				const auto letter2 {input("Enter second letter(a to z): ", error)};

				std::cout << letter1 << ' ' << letter2 << '\n';
			}
			break;

			case 'n':
			{
				const string error_text {"Sorry, out of range. Try again."};
				const auto num1 {input(100, -100, "Enter first number (100 to -100): ", error_text)};
				const auto num2 {input(100, -100, "Enter second number (100 to -100): ", error_text)};

				std::cout << num1 << ' ' << num2 << '\n';
			}
			break;

			case 'q':
				break;

			default:
				std::cout << "Invalid option\n";
				break;
		}
	} while (options != 'q');
}

char input(const std::string& prompt, const std::string& error_message) {
	char letter {};

	while ((std::cout << prompt) && (!(std::cin >> letter) || !std::isalpha(letter))) {
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		std::cout << error_message << '\n';
	}

	return letter;
}

double input(double max_num, double min_num, const string& prompt, const std::string& error_text) {
	double num {};

	do {
		std::cout << prompt;

		if ((std::cin >> num) && (num >= min_num) && (num <= max_num) && std::isspace(std::cin.peek())) {
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
			return num;
		}

		std::cout << error_text << '\n';
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	} while (true);
}

Topic archived. No new replies allowed.