a PROBLEM WITH POINTERS

How to solve this run time error?
thx..

SOURCE 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>
#include<cstdlib>

using namespace std;

void Square (int &x,int &y);
void Cube (int &x,int &y);
void Swap (int &x,int &y);
void ChangeValues (int &x,int &y);
void TwoStrings (string &x,string &y);
void DisplayValues (int,int);

void Example4();
void DisplayValuesWithFunctionPointer(void (*) (int &,int &),int &,int &);

int num1;
int num2;
const int arraySize=4;
char choice[10];

void (*AfunctionPointer1)(int &,int &);

int main()
{

Example4();
            cout<<"\n\n\tExiting.....\n\n\t";
            return 0;
}


void DisplayValues(int x,int y){
    cout<<"\tx: "<<x<<endl<<"\ty: "<<y<<endl;
    }

void TwoStrings (string &x,string &y){
    cout<<"\n\tstring 1 = "<<x<<"\n\tstring 2 = "<<y;
    }

void Square (int &rx,int &ry){
    rx*=rx;
    ry*=ry;
    }

void Cube (int &rx,int &ry){
    int temp=rx;
    rx*=rx;
    rx=rx *temp;

    temp=ry;
    ry*=ry;
    ry=ry *temp;
    }

void Swap (int &rx,int &ry){
    int temp=rx;
    rx=ry;
    ry=temp;
    }

void ChangeValues (int &rx,int &ry){
    char choice[10]="";
    cout<<"\n\tnew value for num1: ";
    cin>>choice;
    if(!isdigit(choice[0])){
        choice[0]='1';}
        rx=atoi(choice);
    cout<<"\n\tnew value for num2: ";
    cin>>choice;

    if(!isdigit(choice[0])){
        choice[0]='1';
        }
        ry=atoi(choice);
    }



void Example4(){
    num1=3;
    num2=9;

    cout<<"\n\tExample 4 is on autopilot. We will use an array of function.";
    cout<<"\n\tPointers to call each function as we iterate in a for() loop:\n";
    for (int x=0;x<arraySize;x++){
        switch(x){
        case '0' : AfunctionPointer1=ChangeValues;break;
        case '1' : AfunctionPointer1=Square;break;
        case '2' : AfunctionPointer1=Cube;break;
        case '3' : AfunctionPointer1=Swap;break;
        default  : cout<<"\n\tInvalid choice";break;
        }

        cout<<"\n\nLoop iteration # "<<x+1<<": ";
        cout<<"\n\n----------------------------------------";
        DisplayValuesWithFunctionPointer(AfunctionPointer1,num1,num2);
        cout<<"\n\n----------------------------------------";
        system("pause");
        system("cls");
        }


    }


void DisplayValuesWithFunctionPointer(void (*functionPointer)(int &,int &),int & x,int & y){
   cout<<"\n\nBefore:\n  x = "<<x<<endl<<" y = "<<y<<endl;
   functionPointer(x,y);
   cout<<"\n\nAfter:\n  x = "<<x<<endl<<" y = "<<y<<endl;

    }
Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

Use your IDE to run the program in the debugger, and find which line it crashes on.
How to solve this run time error?

What run-time error? Please be specific and provide the text of the error when asking for help.

Lines 88-91: Your case labels are ASCII values, while your switch variable is an integer.

Line 92: You detected that x was not valid, but you continued inline as if nothing had happened. You should have exited the function at this point or called exit() to exit the program.

Had you checked the output of yoor program, you would have seen that "Invalid choice" was displayed at line 92. Because you had not set AfunctionPointer, you trapped at line 109 when you tried to make a call using AFunctionPointer.
Last edited on
You are right. The problem was in line 88-91
I changed them to integers..thx
But what's wrong with line 92? instead of exit ( ) function
I have used break..anyhow thnx for your help.
@omer123 break exits the switch statement, it does not exit the program or function. You could write return to exit the function or exit() to exit the program. Though I wouldn't recommend the later.
Last edited on
Thanks guys ... Iappreciate it.
Topic archived. No new replies allowed.