C++ contact card, re-running program trouble -


for assignment, have make customer card info required. able run program fine, when re-run "do wish run again? (y/n)", first 2 questions appear on same line , messes program. me fix issue?

#include <iostream> #include <cstdlib> #include <iomanip> int main() {  char answer;   system("cls"); cout << "*********************************************" << endl; cout << "*********************************************" << endl; cout << "***            w e l c o m e !            ***" << endl; cout << "*** in program creating  ***" << endl; cout << "***      customer contact card!         ***" << endl; cout << "*********************************************" << endl; cout << "*********************************************" << endl; system("pause");   {  string name; string city; string address; string state; string phone_number; string zip;     system("cls");  cout << endl; cout << "enter name of contact : "; getline(cin,name); cout << "enter contact's phone number : "; getline(cin,phone_number); cout << "enter contact's address : "; getline(cin,address); cout << "enter city contact lives in : "; getline(cin,city); cout << "enter state contact lives in (enter abbreviation)  : "; getline(cin,state); cout << "enter contact's zip code : "; cin >> zip; system("pause");  system("cls");  cout << "*********************************************" << endl; cout << "***                                       ***" << endl; cout << "*********************************************" << endl; cout << "*** " << name <<  setw(41- name.length()) << "***" << endl; cout << "*** " << address << setw(41- address.length()) << "***" << endl; cout << "*** " << city << " " << state << " , " << zip << setw(30- city.length()) << "***" << endl; cout << "*** " << state << setw(41- state.length()) << "***" << endl; cout << "*** "; cout<<"("; for(int = 0; < 3; i++) { cout << phone_number[i]; } cout << ")"; for(int = 3; < 6; i++) { cout << phone_number[i]; } cout << "-"; for(int = 6; < 10; i++) { cout << phone_number[i]; } cout << setw(38- phone_number.length()) << "***" << endl;   cout << "*** " << zip << setw(41- zip.length()) << "***" << endl; cout << "*********************************************" << endl; cout << "*********************************************" << endl; cout << endl; cout << "do want create contact card? (y/n)" << endl; cin >> answer;  }  while (answer == 'y' || answer == 'y');   return 0;   } 

you mixing cin >> , getline(cin,. newline still stuck in cin time wanted read first question's answer on second run.

stick 1 or other , confusing behavior shouldn't present itself.

as mentioned in this answer: add following after cin >> answer clear cin , including newline:

cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -