Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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.
manOcode at 2007-11-11 20:58:41 >
# 2 Re: Skip a line of code?? How??
try cin.getline(buffer, size) instead.
jonnin at 2007-11-11 20:59:41 >
# 3 Re: Skip a line of code?? How??
Does it work in the first place? You didnt declare and initialize the strings, instead you had the user input the information twice.

is this from the introduction to computer science using C++ book?
gtrexe at 2007-11-11 21:00:40 >
# 4 Re: Skip a line of code?? How??
Probably <Enter> key, pressed for previous "cin >> zip" operation, affects the getline operation. I think you should replace all of getline operations with ">>", for example:
cin >> name;
I hope this helps.
Viorel at 2007-11-11 21:01:46 >
# 5 Re: Skip a line of code?? How??
Probably <Enter> key, pressed for previous "cin >> zip" operation, affects the getline operation. I think you should replace all of getline operations with ">>", for example:
cin >> name;
I hope this helps.
Viorel at 2007-11-11 21:02:45 >
# 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:
manOcode at 2007-11-11 21:03:38 >