switch cases

Hello
I am new to using switch cases and im trying to build a program that saves to a text file i looked up how to use switch cases on several sites and my code looks the same as what they have for switch cases so here is my code and can someone reply to tell me what i did wrong. My errors are illegal switch case on case 2 and 3. Thanks a lot. =)

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>

using namespace std;
// globals
int whereto;
int b = 1;
int j;
int w = 1;
int l;
int i = 0;
int go = 1;
string str;
struct node
{
node* link;
string data;
};
int main()
{
ofstream SaveFile("ToDoList.txt", ios::app);
while(go == 1)
{
cout << " Do you want to create a list of check your old list? " << endl;
cout << " 1 = create new list " << endl;
cout << " 2 = check old list " << endl;
cout << " 3 = delete old list " << endl;
cout << " 4 = exit program " << endl;
cin >> whereto;
while ( whereto != '1' && whereto != '2' && whereto != '3' && whereto != '4' )
{
cout << "Bad number ( 1,2,3,4 ) , enter number : ";
cin >> whereto;
}
switch (whereto)
case '1':
{
// creating the nodes
cout << "how many jobs?" << endl;
cin >> j;
cout << endl;
node* root = NULL;
for( i=1; i<=j+1;)
{
node* temp = new node;
temp->link = NULL;
cout << " Insert a to do to add to list: " << endl;
//cin >> k;
getline(cin,str);
SaveFile << str << endl;
temp->data = str;
do
{
system("CLS");
b = 3;
}while(b == 1);

if(!root)root=temp;
else
{
node* end = root;
while(end->link)end = end->link;
end->link = temp;
}i++;}
// shows how many nodes were created
node* print = root;
cout << " These are your jobs you entered ... " << endl;
while(print)
{
SaveFile << " " << print->data;
SaveFile.close();
cout << print->data << endl;
print = print->link;
}

// checks to see if user wants to delete the nodes
if (w == 1)
{
// deletes nodes
while(root)
{
node* save = root->link;
delete root;

root = save;
}}
else{
// inserts new nodes
cout << "how many?" << endl;
cin >> l;
// checks how many user wants to add
for(int q=0; q <l; q++)
{
node* temp = new node;
temp->link = NULL;
temp->data = q;
// shows that new nodes were added
cout << "push" << endl;
if(!root)root=temp;
else
{
node* end = root;
while(end->link)end = end->link;
end->link = temp;
}}
node* print = root;
while(print)
{
// prints out the data in the nodes



//cout << print->data << endl;
//cout << print->data << endl;
print = print->link;
}
// starts to delete the nodes
while(root)
{
node* save = root->link;
delete root;
// shows that the nodes have been deleted

root = save;
}}
system("pause");
}
break;

case '2':
{
string line;
fstream SaveFile ("ToDoList.txt");
if (SaveFile.is_open())
{
while (! SaveFile.eof() )
{
getline (SaveFile,line);
cout << line << endl << " ";
}
SaveFile.close();
}

else cout << "Unable to open file";

}
break;

case '3':
{
ofstream outfile("ToDoList.txt", ios::trunc);
outfile.close();
}
break;
};
while (whereto != '4')
{
return 0;
}
}
Last edited on
You're using the switch case-statement incorrectly.

1
2
3
4
5
6
7
8
9
10
11
12
//Here is how you use it.
  switch (expression)
case constant:
  {
    //your statements here...
    break;
  }
case constant:
  {
    //your statements here...
    break;
  }


1
2
3
4
5
6
7
8
9
10
//Here is how you should use it.
switch (expression)
{
  case constant:
    //your statements here...
    break;
  case constant:
    //your statements here...
    break;
}


And a on a related note, keep in mind that whereto is a variable of the data type int. In your switch case-statement, putting numbers between apostrophes makes them a char type value. Look up an ASCII chart on google to see what int values the characters 1 and 2 have (note: the characters 1 and to are not the same as the values 1 and 2).
Last edited on
Topic archived. No new replies allowed.