switch and read text file

Hi Everyone , i am trying to read a file test.txt that has 2 columns. the 1st column serves as an intruction and the 2nd column serves as the value. but note that we will not know how many of these are inside the .txt file. so we will need to open and read the file

1
2
3
4
  addnum 2
  deletenum 3
  multiply 5
  divide  2


now i have 4 functions in c++ with the same name . add(), Multiply(), Minus(), Divide(). what i need to do is that read the file and recognize the 1st column as the instruction so if i detected that the 1st column is addnum i should do the add function that i have . so my question is, is it possible to use switch in this type of situation. if yes can someone please guide me in how to start of that part ? cos i know how to read and do the switch but separately thank you
Hello paulpv278,

Yes it is possible and most likely the best choice. I would also consider taking the names "addnum","eletetnum" etc. and put them in an enum to use in the case statements.

I understand the concept, but do not use it that often. I would have to work something up and test it before I could say how to do this. Since the first pat will most likely be read into a string I am not sure how to handle that.

I will see what I can come up with.

Hope that helps,

Andy

@paulpv278,
The cases in a switch should be "of integer type", or convertible to such, which includes ints, chars and enums. However, you would be trying to switch on the basis of a string.

You could use a set of if ... and else if ... instead of switch.

Alternatively, you could set up a mapping between the string name and the actual function. (In the example below my function names are slightly different from yours.) This allows you the flexibility to add more functions in the future.

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
using namespace std;


// Some binary functions
double set( double a = 1, double b = 1 ) { return b; }
double add( double a, double b ) { return a + b; }
double sub( double a, double b ) { return a - b; }
double mul( double a, double b ) { return a * b; }
double div( double a, double b ) { return a / b; }


// Map function name to function;
map<string,double(*)(double,double)> fmap = { { "set", set },
                                              { "add", add },
                                              { "sub", sub },
                                              { "mul", mul },
                                              { "div", div } };

int main()
{
   double result = 0.0;              

// ifstream in( "input.txt" );
   istringstream in( "set 0"
                     "add 2"
                     "sub 3"
                     "mul 5"
                     "div 2" );

   string op;
   double arg;
   while ( in >> op >> arg )
   {
      if ( fmap.count( op ) )          // If this operation is found
      {
         result = fmap[op]( result, arg );
         cout << op << " " << arg << " ---> " << result << '\n';
      }
      else
      {
         cout << "Operation " << op << " not understood\n";
      }
   }
}


set 0 ---> 0
add 2 ---> 2
sub 3 ---> -1
mul 5 ---> -5
div 2 ---> -2.5
Last edited on
Hello paulpv278,

Sorry for my over enthusiasm when I said a switch would work. It does, but it does not. As I set up a program I realized that the first part to read would be a string. This is fine, but you need some way to change the string into an int to use with the switch.

My first attempt was to use if/else if statements then I realized that you just need to expand the if/else if statements and the switch would not be needed. And the "enum" I set up is not really needed either.

lastchance has a good solution if you can use it otherwise I think I would stick with if/else if statements inside a while loop that is set up to read the whole file.

If you have any code written post it and we can work on it.

Hope that helps,

Andy
HI Everyone thanks for the suggestions,
I manage to read the set of instructions in the text file and the integer part of it. but i am not using switch anymore . I have use If and else statements instead
Topic archived. No new replies allowed.