Question about Exception.

I'm trying to make an exception here to limit 9 characters and the program errors.

Am i doing something wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  		cout << "PC " << i+1 << endl;
		
		cout << "Model Number: ";
		while(true){
		getline(cin,xPc[i].ModelNumber);
		try{			
		
		if(xPc[i].ModelNumber<=9)
		throw xPc[i].ModelNumber;
				
		break;
			
			
		}
		catch(int x){
			cout << x << " pls enter only 9 characters.\n"
				<<"please re-enter: ";
			continue;
		}
		
	}
Am i doing something wrong?

Well...
1) you aren’t providing a compilable example which reproduces your error;
2) you aren’t describing your problem;
3) you’re asking only if you’re doing something wrong or not, so the proper answer to your question would be “Yes, you are” - period;
4) you are likely to be using “using namespace std;” (but this is a debatable point);
5) you’re throwing a std::string and catching an int.

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
#include <iostream>
#include <string>

class HDD_Purple_Heart_Mysterious_Class {
public:
    std::string ModelNumber;
};

void waitForEnter();

int main()
{
    unsigned i {};
    std::cout << "PC " << i+1 << "\nModel Number (max 9 characters)? ";
    HDD_Purple_Heart_Mysterious_Class xPc[2];
    do {
        std::getline(std::cin, xPc[i].ModelNumber);
        try {
            if(xPc[i].ModelNumber.length() > 9) {
                throw xPc[i].ModelNumber.length();
            }
        }
        catch(size_t x) {
            std::cout << "You entered " << x << " characters.\nPls enter "
                         "no more than 9 characters.\nPlease re-enter: ";
        }
    } while(9 < xPc[i].ModelNumber.length());
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

> Am i doing something wrong?

Yes; using exceptions for control flow.

What shouldn’t I use exceptions for?
...
In particular, do not use exceptions for control flow. throw is not simply an alternative way of returning a value from a function (similar to return). Doing so will be slow and will confuse most C++ programmers who are rightly used to seeing exceptions used only for error handling. Similarly, throw is not a good way of getting out of a loop.
https://isocpp.org/wiki/faq/exceptions



Something like this, perhaps:
1
2
3
4
5
6
7
8
9
10
11
12
13
// accept a line containing [1,max_char] characters from stdin
std::string get_line_n( std::size_t max_chars )
{
    static std::string line ;
    std::getline( std::cin, line ) ;

    if( line.empty() ) std::cout << "enter at least one character." ;
    else if( line.size() > max_chars ) std::cout << "enter no more than " << max_chars << " characters." ;
    else return line ;

    std::cout << " try again: " ;
    return get_line_n(max_chars) ;
}


And then:
1
2
std::cout << "Model Number (max 9 characters): ";
xPc[i].ModelNumber = get_line_n(9) ;
Topic archived. No new replies allowed.