Hello everybody!
I'm trying to count the number of occurrences of each word in a text file. Problem situation requires to realize menu using "switch" statement (If file exist or not). There are following errors in my program ("case b" and "default" is not working):

[C++ Error] 03_int.cpp(61): E2126 Case bypasses initialization of a local variable
[C++ Error] 03_int.cpp(72): E2238 Multiple declaration for 'w'
[C++ Error] 03_int.cpp(50): E2344 Earlier declaration of 'w'
[C++ Error] 03_int.cpp(83): E2126 Case bypasses initialization of a local variable

#include <fstream.h>
#include <iostream.h>
#include <map.h>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <conio.h>

typedef std::map<std::string, int> StrIntMap;

void countWords(std::istream& in, StrIntMap& words)
{
    std::string s;

    while (in >> s)
    {
        ++words[s];
    }
}

using namespace std;

int main(void)
{
char str[80];
FILE *fp;
char input;

cout << "a. File exist and empty\n"
     << "b. File is not exist";
cin >> input;
cin.ignore(1000, '\n');

switch (input) {
  case 'a':
    fp = fopen("TEST.dat", "a");
    do {
  cout << "Enter a string (CR to quit):\n";
  gets(str);
  strcat(str, "\n"); /* add a newline */
  fputs(str, fp);
} while(*str!='\n');
  fclose(fp);

  ifstream in("TEST.dat");
  StrIntMap w;
  countWords(in, w);

    for (StrIntMap::iterator p = w.begin( ); p != w.end( ); ++p)
    {
        std::cout << setw(20) << left  << p->first  << " occurred "
                  << setw(5)  << right << p->second << " times.\n";
    }

  break;

  case 'b':
    fp = fopen("TEST.dat", "w");
    do {
  cout << "Enter a string (CR to quit):\n";
  gets(str);
  strcat(str, "\n"); /* add a newline */
  fputs(str, fp);
} while(*str!='\n');
  fclose(fp);
  ifstream inFile("TEST.dat");

  StrIntMap w;
  countWords(inFile, w);

    for (StrIntMap::iterator p = w.begin( ); p != w.end( ); ++p)
    {
        std::cout << setw(20) << left  << p->first  << " occurred "
                  << setw(5)  << right << p->second << " times.\n";
    }

  break;

  default:
    cout << "Enter a or b";

  }

return 0;
}

What should I search for in my code in order to find the problem? Is it possible to realize counting without "ifstream"?
Thanks in advance.

What should I search for in my code in order to find the problem?

You should look at the line number that the error messages tell you and use that to identify the line in the code that it doesn't like.

You're declaring the same variable, w, in more than one place in the same scope. Switch blocks are not separate scopes.

You've basically written:

StrIntMap w;
StrIntMap w;

Same variable, twice. Declare it once, before any of the switch cases.

I've realized it. What should I do with remaining errors?

[C++ Error] 03_int.cpp(35): E2126 Case bypasses initialization of a local variable
[C++ Error] 03_int.cpp(57): E2126 Case bypasses initialization of a local variable
[C++ Error] 03_int.cpp(79): E2126 Case bypasses initialization of a local variable

This are same errors in the case declaration. Maybe it should be to declare variables inside of case?

Move the declaration of w to before the switchyard, and put every set of case code inside its own block.

case 'a':
{
  ...
  ...
  break;
}
case 'b':
{
  ...
  ...
  break;
}
...
...

Moschops, Have you mind this?:

#include <fstream.h>
#include <iostream.h>
#include <map.h>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <conio.h>

typedef std::map<std::string, int> StrIntMap;

void showMenu();

void countWords(std::istream& in, StrIntMap& words)
{
    std::string s;

    while (in >> s)
    {
        ++words[s];
    }
}

using namespace std;

int main(void)
{
char str[80];
FILE *fp;
char input;

cout << "a. File exist and empty\n"
     << "b. File is not exist";
cin >> input;
cin.ignore(1000, '\n');

StrIntMap w;

switch (input) {
  case 'a':
    fp = fopen("TEST.dat", "a");
    do {
  cout << "Enter a string (CR to quit):\n";
  gets(str);
  strcat(str, "\n"); /* add a newline */
  fputs(str, fp);
} while(*str!='\n');
  fclose(fp);

  ifstream in("TEST.dat");

  countWords(in, w);

    for (StrIntMap::iterator p = w.begin( ); p != w.end( ); ++p)
    {
        std::cout << setw(20) << left  << p->first  << " occurred "
                  << setw(5)  << right << p->second << " times.\n";
    }

  break;

  case 'b':
    fp = fopen("TEST.dat", "w");
    do {
  cout << "Enter a string (CR to quit):\n";
  gets(str);
  strcat(str, "\n"); /* add a newline */
  fputs(str, fp);
} while(*str!='\n');
  fclose(fp);
  ifstream inFile("TEST.dat");


  countWords(inFile, w);

    for (StrIntMap::iterator p = w.begin( ); p != w.end( ); ++p)
    {
        std::cout << setw(20) << left  << p->first  << " occurred "
                  << setw(5)  << right << p->second << " times.\n";
    }

  break;

  default:
    cout << "Enter a or b";

  }

return 0;
}

[C++ Error] 03_int.cpp(61): E2126 Case bypasses initialization of a local variable
[C++ Error] 03_int.cpp(83): E2126 Case bypasses initialization of a local variable

ifstream in("TEST.dat"); on lines 49 and 70 needs to be just one decleration done before the switch statement.

Then program don't count occurrences of each inputed word if the file is not exist... Is it possible to realize counting without "ifstream"?

Well you need to do error checking in your program to make sure the input file opened. You could do this

ifstream inFile("TEST.dat");
if (!inFile)
{
    std::cout << "Error with input file!"
    return 0; // exit program
}

All right, thanks.

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.