Hey guys, the following code is part of a program I am working on and as a newbie to c++ I have come across a problem. As you can see when the case is J the int J is increased by 1 but how do I go about doing this if for example instead of J I want to use JJ?

Thanks in advance for any help.

ifstream file("c:\file.txt");

    if (file.is_open())
  {
    while (! file.eof() )
    {
        while (file.get(ch))
         {
		switch (ch) {
                             case 'j':
                             J++;
                            }
           }
     }
  }

switch only operate on a single integer value... however since a char is smaller than an integer I suppose you could stack it in one value and then do the switch

value = ((value << 8) & 0xff) |file.getch() ;
   switch( value )
   {
      case ('j' << 8 | 'j'):
   }

Assuming the old value is saved in value between each loop, this _should_ work since 'j'<<8 | 'j' is a constant expression that the compiler should be able to figure out (else you would have to write it down yourself to get the integer value.
This wont work if you want to use both one and two letter combinations in the same switch thought, unless you are willing to list _all_ possible combinations with the single char you want to match (ie case 'a'<<8 | 'j': case 'b' <<8 | 'j': ... ...
And simpler way would probably be to store the last value read and go from there.

Hi thanks for your reply, is there also a way to do this using IF statements?

Thanks

ofcourse, there is plenty of ways to do this... you could for example store the previous value...

current_value = file.getc() ;
if( current_value == 'j' && previous_value == 'j') {
    // We got our value...
}
previous_value = current_value ;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.