I am relatively new to C/C++ and i was wondering how I could use for loop so I wouldn't have to make a bunch of switch statements I have already made the switch statement I just need help on integrating the for loop into it. Thank you.
#include <stdio.h>
#pragma warning(disable : 4996)
int main() {
char ch;
ch = getchar();
int f, a = 10, b = 20;
printf("ch = %c\n", ch);
switch (ch) {
case '+': f = a + b; printf("f = %d\n", f); break;
case '-': f = a - b; printf("f = %d\n", f); break;
case '*': f = a * b; printf("f = %d\n", f); break;
case '/': f = a / b; printf("f = %d\n", f); break;
default: printf("invalid operator\n");
}
}
the purpose of the program is to enter in either +,-,*,/ and then based on that input it will execute the case that was entered in so + would add a and be together.
Basically, you need to map operator characters to operations.
Your current code represents that mapping via execution flow control.
In C++ the standard library's map
collection is a good choice for instead representing it as data, so that you don't even need to use a loop. In C an array of structs where each contains a char
and a function pointer, can do the same job. However, you then have to define the functions yourself, because unlike the C++ standard library the C standard library doesn't provide convenient named functions for the arithmetic operations.
In a similar fashion, a Boolean state, whether something is true or false, can be represented as an execution position or as data, usually as a variable of type bool
. What to choose is mainly engineering gut feeling. Sometimes representation via flow control is simplest and most clear, sometimes representation as data is simplest and most clear.
C++ example, mostly reproducing the given example code's effect, but with the mapping as data:
#include <iostream>
#include <functional>
#include <map>
using namespace std;
auto main() -> int
{
const map<char, function<int(int,int)>> op =
{
{ '+', plus<int>() },
{ '-', minus<int>() },
{ '*', multiplies<int>() },
{ '/', divides<int>() }
};
char ch;
cout << "Operator? "; cin >> ch;
cout << "ch = '" << ch << "'\n";
if( op.count( ch ) == 0 )
{
cout << "invalid operator\n";
}
else
{
const int a = 10;
const int b = 20;
cout << "f = " << op.at( ch )( a, b ) << "\n";
}
}
Corresponding C example, which does include a for
loop as mentioned in the question:
#include <stdio.h>
int plus( int a, int b ) { return a+b; }
int minus( int a, int b ) { return a-b; }
int multiplies( int a, int b ) { return a*b; }
int divides( int a, int b ) { return a/b; }
typedef int(*Func_ptr)(int, int);
struct Mapping
{
char ch;
Func_ptr f;
};
const struct Mapping op[] =
{
{ '+', plus },
{ '-', minus },
{ '*', multiplies },
{ '/', divides }
};
const int n_ops = sizeof( op )/sizeof( *op );
Func_ptr op_at( char ch )
{
for( int i = 0; i < n_ops; ++i )
{
if( op[i].ch == ch ) { return op[i].f; }
}
return NULL;
}
int main()
{
int ch; // Note: type `int` to accommodate EOF value.
printf( "Operator? " ); ch = getchar();
printf( "ch = '%c'\n", ch );
if( op_at( ch ) == NULL )
{
printf( "invalid operator\n" );
}
else
{
const int a = 10;
const int b = 20;
printf( "f = %d\n", op_at( ch )( a, b ) );
}
}
C11, I think it was, introduced some machinery for effectively overloading functions so they can be used much like overloaded functions in C++. I don't remember much about and didn't use it here. I would suggest that if you need to handle different data types, just use different function names.
Note that the C example also compiles as C++, so both these examples are technically C++. However, the last example is in C style, using C idioms and C i/o, and does things that are unnecessary in C++. We usually just say that such code is C, not that it's C style; such code might not always compile as C++, because while C is largely a subset of C++ these are two different, separate languages: there is technically no such thing as C/C++.