0

I'm working on this as a function for a class project, but I can't figure out why the loop won't break when the user enters "N". It just keeps looping no matter what the user enters when asked if they want to continue.

Can someone help me get the loop to break properly? That's the only part of the code that isn't working.


#include<iostream>
#include<fstream>
#include<string>

using namespace std;

const string ADDRESS_FILE_NAME = "Address.csv";   

int main()
{
    string name;
    string street;
    string city;
    string state;
    string zip;
    char input;
    //TODO: Declare variables for name and address fields
    
    ofstream inputAddress(ADDRESS_FILE_NAME);
    
    //TODO: Open address file for output using ofstream()
    
    do{
    cout << "Enter name: " << endl;
        getline(cin, name);
        cin.ignore();
    cout << "Enter street name: " << endl;
        getline(cin, street);
        cin.ignore();
    cout << "Enter city: " << endl;
        getline(cin, city);
        cin.ignore();
    cout << "Enter state: " << endl;
        getline(cin, state);
        cin.ignore();
    cout << "Enter zip code: " << endl;
        getline(cin, zip);
        cin.ignore();
    cout << "Enter another address? (Y/N)" << endl;
        cin >> input;
        cin.ignore();
    }while(input != 'N' && 'n');
    return 0;
    
    
    //TODO: Loop to accept and write addresses 
    //    Prompt user to enter name and address fields
    //    Write entered data to address file separated by commas
    //    Ask user if more addresses to enter
    //    Repeat loop while user responds 'Y' or 'y'
    
    inputAddress.close();
    //TODO: Close address file

}
  • 1
    `input != 'N' && 'n'` needs to be `input != 'N' && input != 'n'` – cigien Aug 26 '21 at 02:38
  • 1
    `&&` just says: Take the logical and of the right and left sides. The left side says that `input != 'N'`, which is dependent on the input, and the right side states `'n'`, which always evaluates to `true` – Lala5th Aug 26 '21 at 02:40
  • That did it, thank you. I've been struggling with this for hours and now I feel dumb – Nick Ronkartz Aug 26 '21 at 02:42

0 Answers0