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



int main() {
    bool exit = true;
    bool wrongssn = false;

        string menu;
    cout << "To add a record, type add.\n";
    cout << "To find someone by SSN, type find by ssn\n";
    cout << "to display all records, type display all\n";



    while (exit == true) {
        cin >> menu;
        if (menu == "add") {
            cout << "ye";

        }
        if (menu == "find by ssn") {
            cout << "Type the SSN you wish to search by";
            int ssn;
            cin >> ssn;
            if (!cin) {
                cout << "invalid choice, retry.\n";
            

            }


        }

im trying to get my second if statement to print out, but when I run the program, if I type "find by ssn", it will not print anything and keep asking me for keyboard input until I manually close out of the program.

  • You may want to print the *value* of `ssn`. Does typing `\n` correspond to `ssn` being 0? A better idea may be to use `cin` inside a `getline()`, then you can test for empty input - see here: https://stackoverflow.com/a/4999719/1793968 – alle_meije Mar 07 '21 at 19:01

1 Answers1

3

This is because when you do cin>>menu, then menu stores find and not find by ssn because taking string input using cin stops after it encounters a whitespace. Instead use std::getline.

Syntax:

std::getline(std::cin, menu);
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108