Unable to get switch statement to properly call functions. Any assistance greatly appreciated!

closed account (N1Co216C)
Hello,

I'm trying to write a program for my programming class using Dev C++ on Windows 7 that displays several arithmetic operations in a table. The program streams data from a text file called 'math.txt' (see below). The character before the numbers in the .txt file is supposed to tell the program which function to call using a switch statement, and if the character doesn't match any of the characters in the switch statement, then it shows up as "invalid operation" in the data table. I've completed the program and it compiles, but for some reason when I run it, instead of showing lines of completed arithmetic operations, the table just displays multiple lines that say "invalid operation" or "1". Can anyone please take a look at my code and explain why this might be happening, and why my switch statement isn't properly calling my functions? Also, please excuse the length of my program, I put a lot of comments in my code because it is required for the assignment.



Here is the math.txt file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 + 23 34
- 9 8
+ 100 1
* 8 7
^ 2 5
/ 45 8
! 4
a 12 -9
! 0
^ 7 0
* -9 2
- -50 324
* -7 -3
/ 10 2
/ 1 2
+ 240 360
! 5
S -999 -1
f 8
^ 16 3
/ 9 5




Here is the code I've written:


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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/******************************************************
******************************************************/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <math.h>

using namespace std;




//Prototypes
void doAddition( ifstream & );
void doSubtraction( ifstream & );
void doMultiplicaton( ifstream & );
void doDivision( ifstream & );
void doExponent( ifstream & );
void doFactorial( ifstream & );


int main()

{


//input file

ifstream inFile;

char ch;

inFile.open ( "math.txt" );
if (inFile.fail() )
{
cout<< "The math.txt input file failed to open";
exit(-1);
}

//Read first operation type from the input file



cout<< "Arithmetic Operations";
cout<<endl<<endl;
cout<<"Operation"<<"		"<<"Number 1"<<"	"<<"Number2"<<"		"<<"Result"<<endl;
cout<<"-------------------------------------------------------------------------------"<<endl;

inFile>> ch;

        while(inFile)
	{
	//switch statement calling the various functions based on the operator contained in the ch variable
	switch( ch )
  	{
  	case '+':    cout << doAddition  << endl;
             break;

  	case '-':    cout << doSubtraction << endl;
             break;

 	case '*':    cout << doMultiplicaton << endl;
             break;

  	case '^':    cout << doExponent << endl;
             break;

  	case '/':    cout << doDivision << endl;
             break;

  	case '!':    cout << doFactorial << endl;
             break;


  	default:   cout << "Invalid operation"
  	<< inFile.ignore( 100, '\n' ) << endl;
 	}
	
//Get the next operation type from the input file
inFile>> ch;
        }

inFile.close();





return 0;

}
//code functions below this line



/***************************************************************
Function: void doAddition( ifstream &inFile )

Use: to add 2 numbers together if they are preceded by a 
+ character in the data file.

Arguments: a reference to an input file stream

Returns: None
***************************************************************/

void doAddition( ifstream &inFile )
{
int num1;
int num2;


inFile>> num1 >> num2;

cout<< "Addition	"
<<num1<<"			"<<num2<<"		"<<"Sum"<<"			"
<<num1+num2<< endl;

}


/***************************************************************
Function: Subtract

Use: to subtract one number from the other if they are preceded by a 
- character in the data file.

Arguments: a reference to an input file stream

Returns: None
***************************************************************/

void doSubtraction( ifstream &inFile )
{
int num1;
int num2;
inFile>> num1>> num2;

cout<< "Subtraction	"
<<num1<<"			"<<num2<<"		"<<"Difference"<<"			"
<<num1-num2 <<endl;	
}


/***************************************************************
Function: Multiply

Use: to multiply 2 numbers together if they are preceded by a 
* character in the data file.

Arguments: a reference to an input file stream

Returns: None
***************************************************************/

void doMultiplicaton( ifstream &inFile )
{
int num1;
int num2;
inFile>> num1>> num2;

cout<< "Multiplication	"<<num1<<"			"<<num2<<"		"<<"Product"<<"			"
<<num1*num2 <<endl;
	
}

/***************************************************************
Function: Divide

Use: to divide 2 numbers if they are preceded by a 
/ character in the data file.

Arguments: a reference to an input file stream

Returns: None
***************************************************************/

void doDivision( ifstream &inFile )
	
{
int num1;
int num2;
inFile>> num1>> num2;
	
cout<< "Division	"<<num1<<"			"<<num2<<"		"<<"Quotient"<<"	"
<<num1/num2<< "Remain     "<< num1%num2 <<endl;

}


/***************************************************************
Function: Exponent

Use: to raise the first number in the data file to the
 power of the second number if they are preceded by a 
^ character in the data file.

Arguments: a reference to an input file stream

Returns: None
***************************************************************/

void doExponent( ifstream &inFile )

{
int num1;
int num2;
inFile>> num1>> num2;
	
cout<< "Exponent	"<<num1<<"			"<<num2<<"		"<<"Product"<<"	
<< pow(num1, num2) <<endl;

}


/***************************************************************
Function: Factorial

Use: to find the factorial of a number in the data file if 
it is preceded by an !.

Arguments: a reference to an input file stream

Returns: None
***************************************************************/

void doFactorial( ifstream &inFile )

{
int num,factorial=1;
inFile>>num;

	for(int a=1;a<=num;a++)
	{
	 factorial=factorial*a;
    }

cout<<"Factorial		"<< num <<"			"<<"Product			"
<<factorial<<endl;
} 






This is what the final product should look like:


  Arithmatic Operations

Operation         Number 1       Number 2          Result
----------------------------------------------------------------------------

Addition             23             34              Sum:             57

Subtraction           9              8              Difference:       1

Addition            100              1              Sum:            101

Multiplication        8              7              Product:         56

Exponent              2              5              Product:         32

Division             45              8              Quo:    5   Remain:    5

Factorial             4                             Product:         24

Invalid operation

Factorial             0                             Product:          1

Exponent              7              0              Product:          1

Multiplication       -9              2              Product:        -18

Subtraction         -50            324              Difference:    -374

Multiplication       -7             -3              Product:         21

Division             10              2              Quo:    5   Remain:    0

Division              1              2              Quo:    0   Remain:    1

Addition            240            360              Sum:            600

Factorial             5                             Product:        120

Invalid operation

Invalid operation

Exponent             16              3              Product:       4096

Division              9              5              Quo:    1   Remain:    4
Last edited on
I know you might be a beginner, but that code is absolutely atrocious...

Please use indentation that can be easily read. Also, for the love of god, please have the decency to multi-line long couts...

I'm also going to add: delete Dev C++, please, and use somthing that is not horribly outdated. You have a plethora of IDEs to choose from, please pick one (I'm not going to save you the trouble of googling it yourself until you save me the trouble of deciphering your terribly formated code).
Last edited on
closed account (N1Co216C)
My apologies. I edited the code a bit in my previous post. Is that any better? I multi-lined some of the cout statements. I'm not too sure how everything else is supposed to be formatted. Also for the indentation my professor told our class to only indent loops. As for the compiler, my professor suggested either Dev C++ or Quincy, but recommended Dev C++ since that's the one she uses in while teaching the class.
Last edited on
closed account (N1Co216C)
Also, to help clarify what I am trying to do here, this is the link to the assignment description.


https://webcourses.niu.edu/bbcswebdav/pid-3766561-dt-content-rid-22845320_2/courses/20152-CSCI-240-----1/240pgm6.htm
i think iwishiknew is being a bit melodramatic here jnegoda. just because he cant read your formatting doesnt mean its unreadable. i can read it just fine.
closed account (N1Co216C)
Ok. So can you tell why this program outputs this instead of what its supposed to output in the table above?


Arithmetic Operations

Operation               Number 1        Number2         Result
-------------------------------------------------------------------------------
1
Invalid operation0x22fd30
1
Invalid operation0x22fd30
1
Invalid operation0x22fd30
1
Invalid operation0x22fd30
1
Invalid operation0x22fd30
1
Invalid operation0x22fd30
1
Invalid operation0x22fd30
Invalid operation0x22fd30
1
Invalid operation0x22fd30
1
Invalid operation0x22fd30
1
1
Invalid operation0x22fd30
1
1
Invalid operation0x22fd30
1
1
Invalid operation0x22fd30
1
Invalid operation0x22fd30
1
Invalid operation0x22fd30
1
Invalid operation0x22fd30
1
Invalid operation0x22fd30
Invalid operation0x22fd30
Invalid operation0x22fd30
1
Invalid operation0x22fd30
1
Invalid operation0x22fd30

--------------------------------
Process exited after 0.1048 seconds with return value 0
Press any key to continue . . .




Last edited on
In your switch statement, instead of doing cout << doAddition << endl;, try doAddition(inFile);. Do that for all the cases.
closed account (N1Co216C)
It worked! Thanks! However, there's still a bunch of letters and numbers that appear after "Invalid Operation". How do I get those to go away?


                             Arithmetic Operations

Operation       Number 1        Number 2        Result
-------------------------------------------------------------------------------
Addition           23              34           Sum:            57

Subtraction         9               8           Difference:      1

Addition          100               1           Sum:           101

Multiplication      8               7           Product:        56

Exponent            2               5           Product:        32

Division           45               8           Quotient:  5   Remain:  5

Factorial           4                           Product:        24

Invalid Operation0x22fd30

Factorial           0                           Product:         1

Exponent            7               0           Product:         1

Multiplication     -9               2           Product:       -18

Subtraction       -50             324           Difference:   -374

Multiplication     -7              -3           Product:        21

Division           10               2           Quotient:  5   Remain:  0

Division            1               2           Quotient:  0   Remain:  1

Addition          240             360           Sum:           600

Factorial           5                           Product:       120

Invalid Operation0x22fd30

Invalid Operation0x22fd30

Exponent           16               3           Product:      4096

Division            9               5           Quotient:  1   Remain:  4


--------------------------------
Process exited after 0.08599 seconds with return value 0
Press any key to continue . . .
1
2
  	default:   cout << "Invalid operation"
  	<< inFile.ignore( 100, '\n' ) << endl;


1
2
default: cout << "Invalid operation" << endl;
         inFile.ignore(100, '\n');
As Little Bobby Tables said, it's not unreadable, but -- although I don't speak for everyone -- I would not expect anyone to read your code that way.
closed account (N1Co216C)
That worked too. Thanks for the help fg109.


I'll try to work on my formatting for future programs.
I would not expect anyone to read your code that way.

i reiterate, thats a very small view. there is nothing wrong with it and its very bad if you can only read code written in your style, and not other valid ones.

jnegoda: to repeat, there is nothing wrong with your style, if its comfortable for you keep it.
closed account (N1Co216C)
Ok! I'm really new to programming so I didn't know if there was a specific way it was supposed to be formatted or not. But if my style of coding is fine then I'll just keep coding the way I have been.
Hi,

You should check for division by 0. :+)

Hope all is well.
Topic archived. No new replies allowed.