Function definition: Trouble with if/else statement blocks.

Working on this simple function and getting errors. Any help is greatly appreciated.

Edit: Errors occurring on line 26.

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
void displayCity()
{//declare variables
string input = " ";
string city = " ";
string zip = " ";
char found = 'N';
//create file object and open the file
ifstream inFile;
inFile.open("Advanced26.txt");
//determine whether the file was opened
if (inFile.is_open())
{
   //get the zip code from the user
   cout<< "Enter the ZIP code";
   getline(cin,input);

   //read a record
   getline(inFile,zip,'#');
   getline(inFile,city);
}
   while (!inFile.eof() && found == 'N')
 {  
//if the zip code matches, display the city name  
if (zip == input)
  { 
   cout<<endl<<"City name"<<city<<endl<<endl;
   found = 'Y';
   } 
else
   {
   getline(inFile,zip,'#');
   getline(inFile,city); 
    }//end if     
}//end while

//close the file
inFile.close();
//display error message if ZIP code is not in the file
if (found == 'N')
cout<<endl<<"ZIP code"<<input<<"is not on file"<<endl;
}
Last edited on
You are missing all the includes
Do I need the #includes for a function definition?
You use ifstream so you need to include <fstream>. cout and getline is in <iostream>. You use string so you might have to include <string> as well
Ive used all of them. I just didnt want to post the entire program but here goes...

Error: line 95

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
104
105
106
107
108
109
110
#include<iostream>
#include<fstream>
#include<string>

using namespace std;
int displayMenu();
void addRecords();
void displayCity();

int main(){
//declare variables
int menuChoice = 0;
//display menu and get choice
menuChoice = displayMenu();
//all appropriate function or
//
while (menuChoice != 3)
{
      if (menuChoice == 1)
        addRecords();
      else if (menuChoice == 2)
        displayCity();
      else
        cout<<"Invalid menu choice"<<endl<<endl;
        
        menuChoice = displayMenu();
        }
      

system("pause");
return 0;}

int displayMenu()
{
    int choice = 0;
    
    cout<<"Options"<<endl;
    cout<<"1 Add City/ZIP Code"<<endl;
}

void addRecords()
{    //declare variables
     string city = " ";
     string zip = " ";
     //create file object and open the file
     ofstream outFile;
     outFile.open("Advanced26.txt");
     //determine whether the file was opened
     if (outFile.is_open());
     {  //get the zip code
        cout<< "Enter the ZIP code (X to stop)";
        getline(cin,zip);
        while (zip != "X" && zip != "x")
        {     //get city name
              cout<< "Enter the city name";
              getline(cin, city);
              //write the records
              outFile<<zip<<'#'<<city<<endl;
              
              cout<< "Enter the ZIP code (X to stop):";
              getline(cin,zip);
              }//end while
              //close the file
              outFile.close();}}}
              else
              cout<<"File could not be opened"<<endl<<endl;
             } //end if
              }//end addRecords function

void displayCity()
{//declare variables
string input = " ";
string city = " ";
string zip = " ";
char found = 'N';
//create file object and open the file
ifstream inFile;
inFile.open("Advanced26.txt");
//determine whether the file was opened
if (inFile.is_open())
{
   //get the zip code from the user
   cout<< "Enter the ZIP code";
   getline(cin,input);

   //read a record
   getline(inFile,zip,'#');
   getline(inFile,city);
}
   while (!inFile.eof() && found == 'N')
 {  
//if the zip code matches, display the city name  
if (zip == input)
  { 
   cout<<endl<<"City name"<<city<<endl<<endl;
   found = 'Y';
   } 
else
   {
   getline(inFile,zip,'#');
   getline(inFile,city); 
    }//end if     
}//end while

//close the file
inFile.close();
//display error message if ZIP code is not in the file
if (found == 'N')
cout<<endl<<"ZIP code"<<input<<"is not on file"<<endl;
}
You mean the compiler emit errors or the program doesn't do what you want? Post your errors anyway.
Last edited on
Topic archived. No new replies allowed.