Counter skipped by case error

Every time I try to run my program I get and error that says "initialization of 'counter' is skipped by 'case' label" in line 113. I don't understand how to fix it.

//Name: Amanda Glavin
//Assignment: Converting Miles to Kilometers
//Date: July 6, 2015
//Lab: Midterm

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

ofstream ofs ("glavin_midterm.txt");

string hdr = "Amanda Glavin ENGR CIS 2485 Converting Miles to Kilometers";
string fot = "-----------------End of Data-----------------";
string eof = "EOF MESSAGE - Amanda Glavin, July 6 2015, Midterm, All Done";
string fin = "Following Instructions";
string twt = "20%";
string ten = "10%";
string hun = "100%";
string scr = "your score is ----------";
string pts = "100";
string dit = "Does It Run";
string doc = "Correct Doc's";
string frm = "Format";
string str = "Structure";
string nam = "Correct files/names";
string tot = "Total Scores";
string pon = "Midterm Points";

void header()
{
ofs << endl;
ofs << hdr << endl;
ofs << endl;
}

void footer()
{
ofs << endl;
ofs << fot << endl;
ofs << endl;
}

void grading()
{
ofs << endl;
ofs << fin << setw(8) << twt << setw(25) << scr << endl;
ofs << dit << setw(19) << twt << setw(25) << scr << endl;
ofs << doc << setw(17) << ten << setw(25) << scr << endl;
ofs << frm << setw(24) << ten << setw(25) << scr << endl;
ofs << str << setw(21) << twt << setw(25) << scr << endl;
ofs << nam << setw(11) << twt << setw(25) << scr << endl;
ofs << tot << setw(18) << hun << setw(25) << scr << endl;
ofs << pon << setw(18) << pts << setw(26) << scr << endl;
ofs << endl;
}

void eofmsg()
{
ofs << endl;
ofs << eof << endl;
ofs << endl;
}

int main()
{
header();

int makeMilesKmTable;
double miles, kilometers;
int start, stop, quit;
int split = (start + stop)/2;

while (quit != 1)
{
// Display the menu.
cout << "1. Run one conversion of miles to kilometers." << endl;
cout << "2. Run many conversions of miles to kilometers." << endl;
cout << "3. Quit." << endl;
cout << endl;

// Promt the user for a selection
cout << "Enter your selection. " << endl;
cin >> makeMilesKmTable;

while (makeMilesKmTable < 1 || makeMilesKmTable > 4)
{
cout << "That is an invalid selection. " << endl;
cout << "Enter 1, 2, or 3." << endl;
cin >> makeMilesKmTable;
}

switch (makeMilesKmTable)
{
case 1:
cout << "Enter the number of miles. " << endl;
cin >> miles;
kilometers = miles * 1.61;
ofs << miles << "=" << kilometers << endl;
break;

case 2:
int counter = 0;
while(counter < 301)
cout << "Enter the number of miles." << endl;
cin >> miles;
kilometers = miles * 1.61;
ofs << miles << "=" << kilometers << endl;
counter++;
break;

case 3:
quit = 1;
break;
}
}
footer();

grading();

eofmsg();

ofs.close();
return 0;
}
Encase case part in block:
1
2
3
4
5
6
7
8
9
10
11
case 2:
{
    int counter = 0;
    while(counter < 301)
        cout << "Enter the number of miles." << endl;
    cin >> miles;
    kilometers = miles * 1.61;
    ofs << miles << "=" << kilometers << endl;
    counter++;
    break;
}
(Your code here has bug BTW. I hope proper identation will help you to locate it.)
Topic archived. No new replies allowed.