Console Application Crashing on Start

Hello everyone. This is my first post here, and I'm kind of new to c++ programming, so please bear with me...thanks. Before I describe it, I guess I should say that I am using Dev C++ version 4.9.9.2 in Windows XP. Also, sorry if this post is sort of long, I'm going to post 2 different versions of the program's code.

Anyways, I'm trying to write a basic program to take a sentence that the user enters, find the ascii values of each character in the sentence, change the values, and reprint the sentence as a simple form of encryption.

The problem I was having was that I was trying to use a pointer to hold the memory address of the input from the MainMenu() function so that I could use the deference value of the pointer to send the main menu's input to the main() function. Here is the code before I changed it:

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
#include <iostream>
#include <cstdlib>
using namespace std;

//Pointer to hold the user's input from the MainMenu() function
unsigned int * MainMenuInput;

void MainMenu(){
     
     //Holds user input for the MainMenuInput pointer
     unsigned int input;
     
     //To progress the do...while loop below if the user enters an invalid choice
     int loop = 1;
     
     do { //begins do...while loop for input validation
        cout << endl << "\t\t +++++Basic Encrypt+++++";
        cout << endl << "\t 1) Encrypt a sentence";
        cout << endl << "\t 2) Exit";
        cout << endl << "\t What do you want to do? ";
        cin >> input;
        
        switch (input) { //input validation
               case 1: loop = 0;
                       break;
               case 2: loop = 0;
                       break;
               default: 
                        cout << "Pick a valid selection. " << endl;
        }; //ends switch statement
     } while (loop==1); //ends do...while loop
     
     //assigns memory address of input to the MainMenuInput pointer
     MainMenuInput = &input;
     
} //closes MainMenu() function

void EncryptSentence(){
     
} //ends EncryptSentence()

int main(){
    //what the user selected at the main menu
    unsigned int UserSelection = *MainMenuInput;
    
    switch (UserSelection){
           case 1: //insert call to encryption function here
                   break;
           
           case 2: //to exit the program
                   exit(0);
    }; //ends switch()
    
return 0;
}; //closes main() 


This code would crash to the desktop when I ran it, and gave me no compilation errors. However, I made a few changes to the code afterwards. I changed the return type of the MainMenu() function to integer, removed the global MainMenuInput pointer, and changed the statement on line 33 to return the value to main() instead of a pointer. Here's the updated code:

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
#include <iostream>
#include <cstdlib>
using namespace std;

int MainMenu(){
     
     //Holds user input to return to main()
     unsigned int input;
     
     //To progress the do...while loop below if the user enters an invalid choice
     int loop = 1;
     
     do { //begins do...while loop for input validation
        cout << endl << "\t\t +++++Basic Encrypt+++++";
        cout << endl << "\t 1) Encrypt a sentence";
        cout << endl << "\t 2) Exit";
        cout << endl << "\t What do you want to do? ";
        cin >> input;
        
        switch (input) { //input validation
               case 1: loop = 0;
                       break;
               case 2: loop = 0;
                       break;
               default: 
                        cout << "Pick a valid selection. " << endl;
        }; //ends switch statement
     } while (loop==1); //ends do...while loop
     
     return input;
     
} //closes MainMenu() function

void EncryptSentence(){
     
} //ends EncryptSentence()

int main(){
    
    //collects user input from the main menu
    unsigned int UserSelection = MainMenu();
    
    switch (UserSelection){
           case 1: //insert call to encryption function here
                   break;
           
           case 2: //to exit the program
                   exit(0);
    }; //ends switch()
    
return 0;
}; //closes main() 


I don't really have much more to say except that when I run the updated code, the program compiles and runs as I would expect it to. The thing I was hoping to know was what could have caused the first program to crash, but not the second...thank you, and I'm sorry for the long post
Line 44: This is quite likely a null pointer dereference.

You never set the value of MainMenuInput because you never called the MainMenu() function.
wow, I can't believe I forgot to do that...

Thank you very much. The first program works just fine now. Thanks
Topic archived. No new replies allowed.