Skip a line of code?? How??
Could someone please help me...
I wrote some code for an assignment. Everything works great except for this one line of code. (Below with note)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, address, city, state;
int zip;
cout << "Please enter sender's information.";
cout << "\n\nFirst and last name: ";
getline( cin, name );
cout << "Street address: ";
getline( cin, address );
cout << "City: ";
getline( cin, city );
cout << "State: ";
getline( cin, state );
cout << "Zip Code: ";
cin >> zip;
cout << endl << endl;
cout << name << endl << address << endl << city << ", " << state << " " << zip;
cout << endl << endl;
cout << "Please enter receiver's information.";
cout << "\n\nFirst and last name: "; //This is where I'm having issues
getline( cin, name ); // <-- Why does it skip this line of code?
cout << "Street address: ";
getline( cin, address );
cout << "City: ";
getline( cin, city );
cout << "State: ";
getline( cin, state );
cout << "Zip Code: ";
cin >> zip;
cout << endl << endl;
cout << name << endl << address << endl << city << ", " << state << " " << zip;
cout << endl << endl;
system("PAUSE");
system("cls");
}
Let me know if anyone needs me to explain.
[1870 byte] By [
manOcode] at [2007-11-11 10:31:02]

# 1 Re: Skip a line of code?? How??
Could someone please help me...
I wrote some code for an assignment. Everything works great except for this one line of code. (Below with note)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, address, city, state;
int zip;
cout << "Please enter sender's information.";
cout << "\n\nFirst and last name: ";
getline( cin, name );
cout << "Street address: ";
getline( cin, address );
cout << "City: ";
getline( cin, city );
cout << "State: ";
getline( cin, state );
cout << "Zip Code: ";
cin >> zip;
cout << endl << endl;
cout << name << endl << address << endl << city << ", " << state << " " << zip;
cout << endl << endl;
cout << "Please enter receiver's information.";
cout << "\n\nFirst and last name: "; // :confused: This is where I'm having issues
getline( cin, name ); // <-- :confused: Why does it skip this line of code?
cout << "Street address: ";
getline( cin, address );
cout << "City: ";
getline( cin, city );
cout << "State: ";
getline( cin, state );
cout << "Zip Code: ";
cin >> zip;
cout << endl << endl;
cout << name << endl << address << endl << city << ", " << state << " " << zip;
cout << endl << endl;
system("PAUSE");
system("cls");
}
Let me know if anyone needs me to explain.
# 6 Re: Skip a line of code?? How??
I went ahead and changed the type from int to string for the variable zip and replaced the user inputs with a call to a function that stores the inputs.
int main()
{
//objects of various classes to be used in main()
Package from, to;
TwoDayPackage twoDay;
OvernightPackage over;
//local variables
int lbs, oz, ship;
//sender's info
cout << "Please enter sender's information.";
from.setInfo(); //call member function from class Package and store info in from object
cout << endl << endl;
//receivers info
cout << "Please enter receiver's information.";
to.setInfo(); //call member function from class Package and store info in to object
The function looks like this:
//set information of sender/receiver
void Package::setInfo()
{
string name, address, city, state, zip;
cout << "\n\nFirst and last name: ";
getline( cin, name );
cout << "Street address: ";
getline( cin, address );
cout << "City: ";
getline( cin, city );
cout << "State: ";
getline( cin, state );
cout << "Zip code: ";
getline( cin, zip );
Name = name;
Address = address;
City = city;
State = state;
Zip = zip;
}
All in all...now it works fine. Thanks for the help :WAVE: