beginner question

D

dougmmika

why does the following code not let me enter the phone number nor the address. You would think that if I would have an unwanted \n hanging around my input the first cin.get() would absorb it and at least it would ask me for the address to be input. Instead after entering the first and last name the program quits. Why?
Many thanks to all.

cout<<"Please Enter your first name: ";
cin>>firstStudent.firstName;
cout<<"Please Enter your last name: ";
cin>>firstStudent.lastName;
cout<<"Please enter your Phone Number: (XXX) XXX-XXXX: ";
cin.get(phoneNumber, 15);
cin.get(dummy); //eat the end of line marker
cout<<"Please enter your address: ";
cin.get(address,51);
cin.get(dummy); //eat the end of line marker
 
V

Victor Bazarov

why does the following code not let me enter the phone number nor the address. You would think that if I would have an unwanted \n hanging around my input the first cin.get() would absorb it and at least it would ask me for the address to be input. Instead after entering the first and last name the program quits. Why?
Many thanks to all.

cout<<"Please Enter your first name: ";
cin>>firstStudent.firstName;
cout<<"Please Enter your last name: ";
cin>>firstStudent.lastName;
cout<<"Please enter your Phone Number: (XXX) XXX-XXXX: ";
cin.get(phoneNumber, 15);
cin.get(dummy); //eat the end of line marker
cout<<"Please enter your address: ";
cin.get(address,51);
cin.get(dummy); //eat the end of line marker

It might be useful to get into the habit of posting *complete* code.

Also, to read out a line, consider using std::getline instead of >>.

V
 
D

dougmmika

Here's the complete code:


#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include "bool.h"



typedef char String10[11];
struct StudentRec{
String10 firstName;
String10 lastName;
};


int main()
{
//using namespace std;
char address[51];
char pause, newLine;
char fileName[30];
char phoneNumber[15];
char dummy;
int number=6;
StudentRec firstStudent;

cout<<"Please Enter your first name: ";
cin>>firstStudent.firstName;
cout<<"Please Enter your last name: ";
cin>>firstStudent.lastName;
cout<<"Please enter your Phone Number: (XXX) XXX-XXXX: ";
cin.get(phoneNumber, 15);
cin.get(dummy); //eat the end of line marker
cout<<"Please enter your address: ";
cin.get(address,51);
cin.get(dummy); //eat the end of line marker

cout<<"The information entered is:"<<endl;
cout<<" First Name: "<<firstStudent.firstName<<endl;
cout<<" Last Name: "<<firstStudent.lastName<<endl;
cout<<" Phone #: "<<phoneNumber<<endl;
cout<<" Address: "<<address<<endl;


cout<<"End of Program"<<endl;
cin.get(pause);


return 0;

}
 
V

Victor Bazarov

Here's the complete code:


#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include "bool.h"



typedef char String10[11];
struct StudentRec{
String10 firstName;
String10 lastName;
};


int main()
{
//using namespace std;
char address[51];
char pause, newLine;
char fileName[30];
char phoneNumber[15];
char dummy;
int number=6;
StudentRec firstStudent;

cout<<"Please Enter your first name: ";
cin>>firstStudent.firstName;
cout<<"Please Enter your last name: ";
cin>>firstStudent.lastName;

So, what happens if you [accidentally, wink-wink, nudge-nudge] enter
more than 10 characters?

Drop your char arrays and start using 'std::string', you're using C++ not C.
cout<<"Please enter your Phone Number: (XXX) XXX-XXXX: ";
cin.get(phoneNumber, 15);

Use std::getline here.
cin.get(dummy); //eat the end of line marker

Use std::cin.ignore for that. RTFM
cout<<"Please enter your address: ";
cin.get(address,51);
cin.get(dummy); //eat the end of line marker

cout<<"The information entered is:"<<endl;
cout<<" First Name: "<<firstStudent.firstName<<endl;
cout<<" Last Name: "<<firstStudent.lastName<<endl;
cout<<" Phone #: "<<phoneNumber<<endl;
cout<<" Address: "<<address<<endl;


cout<<"End of Program"<<endl;
cin.get(pause);


return 0;

}

What book on C++ are you reading that teaches you to use character
arrays like that?

V
 
D

dougmmika

what should I use instead of char arrays? Is there an online book to introduce me to C++. I'm reading Programming and Problem Solving with C++ by Dale, Weems, and Headington.

Any more info on std::getLine and the other recommended functions to input data?

Thanks
Mika
 
J

Jorgen Grahn

On 21/03/2013 20:41, (e-mail address removed) wrote: ....
My two checks on names are mine (I have two middle names) and this girl
I knew at Uni - she was Spanish. I won't post up her actual name, but it
had a format like this:

xxxxxxxxxx xxxxxxxxx-xxxxxxxxx

You might like to use Prince Charles as test data - Charles Philip
Arthur George Mountbatten-Windsor.

That's probably not even close to the most complex name ... add a few
"von", "de la" and ", Jr.". And that's just european names.

Or he might ask himself: what do I /really/ need to do with this name?
Probably nothing, in which case it's just one string.
Possibly, collating it according to some national conventions.

/Jorgen
 
D

Dombo

Op 22-Mar-13 0:56, (e-mail address removed) schreef:
what should I use instead of char arrays? Is there an online book to
introduce me to C++.

"Thinking in C++" springs to mind (just Google for it), I don't know if
it is good but it is freely downloadable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top