uninitialized local variable 'm' used

Hi

when i run the below code i get the error uninitialized local variable 'm' used.

int m;
const int MAXNUM = m; (error in this line)
cout << "Enter The Value For The 2D Table: ";
cin >> m;

How to solve the above problem?
What value do you want MAXNUM to have?
How to solve the above problem?

Perhaps you need to supply a value to m before you try to use it?

But since it seems that this may be used to "size" an array you should probably use a constant value instead const int MAXNUM = 100; as array sizes must be compile time constants in C++.


Thanks Alot For The Help,

One More thing

How do i check whether the user has input a value, if the use has not input a value the program should show a message.(i am using cin to input)
How do i check whether the user has input a value, if the use has not input a value the program should show a message.(i am using cin to input)


I mean. The program will stop until the user does input something if you have a cin statement.

Perhaps what you meant to say was if the user failed to input a specific value.

In that case, it's simple.

1
2
3
4
5
6
7
8
int userInput;
cin >> userInput;

if (userInput != 5) // If the value the user inputs is not equal to 5. Send a message
{
    // Your message
    // More on if statements - https://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm
}
Last edited on
Hi

How exit the program if user forget to input a value?
If the program asks for user input the user can't forget to input a value. They may input some horribly bad value but they must input a value.

Hi

how to show a message when user inputs a String value instead of an Integer Value? (using cin for keyboard input)
If the user tries to enter something other than a digit, plus, or minus as the first character into a int variable, or any other numeric type, the stream will be put into an error state. You can test the stream state to determine it's state and if it is in a fail state you can clear the error state then clear the input buffer, and then ask the user to re-enter a correct value or whatever you deem appropriate.
1
2
3
4
5
6
7
8
9
10
11
12
int number;
cout << "Please enter a number: ";
cin >> number;

// Check for an error in the stream.
while(!cin)
{
    cout << "Error you didn't enter a number!\nPlease retry: ";
    cin.clear(); // Clear the error flags.
    cin.ignore(2000, '\n');  // Clear the input buffer.
}


Something along these lines:

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

int get_int( std::string prompt = "type in an integer and press enter" )
{
    std::cout << prompt << ": " ;
    std::string input ;

    if( std::cin >> input )
    {
        try
        {
            std::size_t pos = 0 ;
            const int value = std::stoi( input, std::addressof(pos) ) ;
            if( pos == input.size() ) return value ;

            std::cerr << "error: input '" << input << "' was not fully parsed\n" ;
        }

        catch( const std::invalid_argument& )
        {
            std::cerr << "error: input '" << input << "' is not a number\n" ;
        }

        catch( const std::out_of_range& )
        {
            std::cerr << "error: converted value of input '" << input
                      << "' would fall outside the range of int\n" ;
        }
    }

    else std::cerr << "eof on stdin: attempting to recover, press enter once.\n" ;

    std::cin.clear() ; // clear the error state, and attempt to recover from eof
    std::cin.ignore( 1000000, '\n' ) ; // throw the junk away
    return get_int(prompt) ;
}

int main()
{
    int value = get_int() ;
    std::cout << "the value is " << value << '\n' ;
}
Topic archived. No new replies allowed.