0

For my assignment, I have to make a customer card with some info required. I am able to run the program just fine, but when I re-run it with the "Do you wish to run again? (Y/N)", the first 2 questions appear on the same line and it messes up the program. Could anyone help me fix this 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 this program you will be creating  ***" << endl;
cout << "***      a Customer Contact card!         ***" << endl;
cout << "*********************************************" << endl;
cout << "*********************************************" << endl;
system("pause");

do

{

string name;
string city;
string address;
string state;
string phone_number;
string zip;




system("CLS");

cout << endl;
cout << "Enter the name of your contact : ";
getline(cin,name);
cout << "Enter your contact's phone number : ";
getline(cin,phone_number);
cout << "Enter your contact's address : ";
getline(cin,address);
cout << "Enter the city your contact lives in : ";
getline(cin,city);
cout << "Enter the state your contact lives in (Enter the abbreviation)  : ";
getline(cin,state);
cout << "Enter your 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 i = 0; i < 3; i++) {
cout << phone_number[i];
}
cout << ")";
for(int i = 3; i < 6; i++) {
cout << phone_number[i];
}
cout << "-";
for(int i = 6; i < 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 you want to create another contact card? (Y/N)" << endl;
cin >> answer;

}

while (answer == 'Y' || answer == 'y');


return 0;


}
Bnstates
  • 13
  • 1
  • 1
    `system("CLS")` makes me all sorts of sad. There are [better ways to do this](http://stackoverflow.com/questions/17335816/clear-screen-using-c) that don't involve running system calls. – tadman Apr 19 '16 at 18:23

1 Answers1

1

You are mixing cin >> and getline(cin,. So the newline was still stuck in cin by the time you wanted to read the first question's answer on the second run.

Stick to one or the other and this confusing behavior shouldn't present itself.

As mentioned in this answer: you could also add the following after cin >> answer to clear cin up to and including the newline:

cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
Community
  • 1
  • 1
wally
  • 10,717
  • 5
  • 39
  • 72
  • I tried converting all the cin to getline(cin, but getline(cin, answer) isn't working. – Bnstates Apr 19 '16 at 18:44
  • True, you would have to change the type of `answer`. Perhaps the `ignore` alternative I've just referenced in the last edit would be easier at this point. – wally Apr 19 '16 at 18:47
  • Could you show how it would be written in my circumstance? sorry, I'm terribly new to this stuff and this is a beginner course so I'm still pretty bad. – Bnstates Apr 19 '16 at 18:59
  • Just add the line exactly as written above. You might also need to add `#include ` at the top. – wally Apr 19 '16 at 19:19