illegal else if statement

Write your question here.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
 #include <iostream>
#include <stdlib.h>
#include <fstream>
#include <iomanip>

using namespace std;

int write(void);
int write(void)
{
    ofstream outfile;
    char firstname[4],
         lastname[4],
         contact[8],
         Datetime[8];
    
	
    outfile.open("textfile2.txt.txt",ios::app);

    cout<<"Enter your First Name: ";
    cin>>firstname;
    cin.ignore();
    cout<<"Enter your Last Name: ";
    cin>>lastname;
    cin.ignore();
    cout<<"Enter your Contact: ";
    cin.getline(contact, sizeof(contact));
    cin.ignore();
    cout<<"Enter your Date and Time: ";
    cin.getline(Datetime, sizeof(Datetime));
    cin.ignore();
   

    outfile<<firstname<<setw(2)<<lastname<<setw(2)<<contact<<setw(2)<<Datetime<<endl;
    outfile.close();
    cout<<"Writing Completed."<<endl;
    return 0;
}

int read(void);
int read(void)
{
    ifstream input;
    input.open("textfile2.txt.txt",ios::in);
	{
    char firstname[4],
         lastname[4],
         contact[8],
         Datetime[8];

    
    while(!input.eof())
        input>>firstname>>lastname>>contact>>Datetime;
        cout<<"Information Required. "<<endl;
        cout<<firstname<<setw(1)<<lastname<<endl;
        cout<<contact<<endl;
        cout<<Datetime<<endl;
  
        cout<<endl;
    }
    cout<<"Reading Completed. "<<endl;
    input.close();
    return 0;
}

int main()
{
      int choice;
      char ch;

      while (ch == 'y' || ch == 'Y')
	  {
          cout<<endl;
          cout<<"Please choose from the following: \n";
          cout<<"\t1. Read Data. \n";
          cout<<"\t2. Write Data. \n";
          cout<<"\t3. Quit. \n";
          cout<<endl;
          cout<<"Your choice: ";
          cin>>choice;
      
          {
              if (choice == 1)
                     read();
                   break;
             else if (choice == 2)
                     write();
                   break;
             case 3:

                   break;
             default:
                   cout<<"\tInvalid entry!"<<endl;
           }
           cout<<"\nWould you like to try again (y/n): ";
           cin>>ch;

     }
	  while(ch == 'y' || ch == 'Y');

      system("PAUSE");
      return 0;
}
Looks like you've combined syntax for if ... else statements and switch statements. If you mean to use if... else, you can remove the breaks and remove the case 3 and default. ("invalid entry" can be a final else statement)

If you're going to have multiple statements running as part of a condition like if.. else (or for loop, etc), you need to enclose them in {}, otherwise only the first statement will be executed due to the condition.

Last edited on
Line 83: This if statement terminates with the read() statement. If you want to include multiple statements in the then part of the statement, you need to use {}.
1
2
3
4
5
6
7
8
         if (choice == 1)
         {
                     read();
                   break;  // This break is going to terminate your while loop
         }
         else if (choice == 2)
                     write();  
         break;  // Is this unconditional or part of choice 2? 


It looks like you had a switch statement that you removed. Lines 88 and 92 are only valid in a switch statement. The break statements at 85 and 88 are going to terminate your loop, where they would have terminated a case clause as part of a switch statement.


The keywords "case" and "default" are used with a "switch", which normally goes with the "break". So you do not have to bring the case and default parts, and the break is not necessary, since the if statement onle reads one line after it, if it is met.

Aceix.
Thanks guys now i get an different error , " read" identifier not found and write too


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
      int choice;
      char ch;

      while (ch == 'y' || ch == 'Y')
	  {
          cout<<endl;
          cout<<"Please choose from the following: \n";
          cout<<"\t1. Read Data. \n";
          cout<<"\t2. Write Data. \n";
          cout<<"\t3. Quit. \n";
          cout<<endl;
          cout<<"Your choice: ";
          cin>>choice;
      
          {
              if (choice == 1)
                     read();
                  
             else if (choice == 2)
                     write();
                   
			 else 
                   cout<<"\tInvalid entry!"<<endl;
           }
           cout<<"\nWould you like to try again (y/n): ";
           cin>>ch;

     }
	  while(ch == 'y' || ch == 'Y');

      system("PAUSE");
      return 0;
}



int write(void);
int write(void)
{
    ofstream outfile;
    char firstname[4],
         lastname[4],
         contact[8],
         Datetime[8];
    
	
    outfile.open("textfile2.txt.txt",ios::app);

    cout<<"Enter your First Name: ";
    cin>>firstname;
    cin.ignore();
    cout<<"Enter your Last Name: ";
    cin>>lastname;
    cin.ignore();
    cout<<"Enter your Contact: ";
    cin.getline(contact, sizeof(contact));
    cin.ignore();
    cout<<"Enter your Date and Time: ";
    cin.getline(Datetime, sizeof(Datetime));
    cin.ignore();
   

    outfile<<firstname<<setw(2)<<lastname<<setw(2)<<contact<<setw(2)<<Datetime<<endl;
    outfile.close();
    cout<<"Writing Completed."<<endl;
    return 0;
}

int read(void);
int read(void)
{
    ifstream input;
    input.open("textfile2.txt.txt",ios::in);
	{
    char firstname[4],
         lastname[4],
         contact[8],
         Datetime[8];

    
    while(!input.eof())
        input>>firstname>>lastname>>contact>>Datetime;
        cout<<"Information Required. "<<endl;
        cout<<firstname<<setw(1)<<lastname<<endl;
        cout<<contact<<endl;
        cout<<Datetime<<endl;
  
        cout<<endl;
    }
    cout<<"Reading Completed. "<<endl;
    input.close();
    return 0;
}
Move the prototypes at line 46 and line 78 to before the main function. The prototypes give the compiler basic information about the function to be defined later so the compiler recognizes the function name when you call it in main.
Topic archived. No new replies allowed.