I'm having trouble with my C++ assignment because when I run the program I can't type more than one word on the "Enter your street name" line or it won't work. My teacher said something about "getline" but he really lost me. Here is what I have. Please help!

#include <iostream>
#include <string>
using namespace std;
int main () {
    string fname, lname;
    int age;
    char gender;
    string snumber, sname;
    string city;
    string state;
    int zip;
    
    
    cout << "Enter your first name : ";
    cin >> fname ;
    cout << "Enter your last name : ";
    cin >> lname ;
    
    cout << "Enter your gender <M/F> : ";
    cin >> gender ;
    
    cout << "Enter your age : ";
    cin >> age ;
    
    cout << "Enter your street number : ";
    cin >> snumber ;
    cout << "Enter your street name : ";
    cin >> sname ;
    
    
    cout << "Enter the name of your city : ";
    cin >> city ;
    
    cout << "Enter the name of your state : ";
    cin >> state ;
    
    cout << "Enter your zip code : ";
    cin >> zip ;
    
    
    
    cout << "       NAME  :  " << lname << ", " << fname << endl;
    cout << "        AGE  :  " << age << endl;
    cout << "    ADDRESS  :  " << snumber <<  "   " << sname << endl;
    cout << "       CITY  :  " << city << endl;
    cout << "      STATE  :  " << state << endl;
    cout << "        ZIP  :  " << zip << endl;
    system("pause");
}

Recommended Answers

All 7 Replies

Where ever your want to cin a string that requires spaces in them or integers as well, use getline instead like this:

cout << "Enter your address: " << endl;
cin.ignore();
getline(cin, address);

Use code tags. And for the question, you getline(), just like you specified in the title. Getline(), is a function, it is part of istream (iostream include), and this is the prototype:

istream& getline (char* s, streamsize n );

You can find more info here.

EDIT: You got to learn how to use Google. I learn most of the programming from searching on internet (and reading books).

# include <iostream>
#include <string>
using namespace std;
int main () 
{
    string fname,lname;
    int age;
    char gender[20];
    string snumber, sname;
    string city;
    string state;
    int zip;
    
    
    cout << "Enter your first name : ";
    cin >> fname ;

    cout << "Enter your last name : ";
    cin >> lname ;
    cout<<endl;

    cout << "Enter your gender <M/F> : ";
    cin >> gender ;
    
    cout << "Enter your age : ";
    cin >> age ;
    
    cout << "Enter your street number : ";
    cin >> snumber ;


    cout << "Enter your street name : ";
    cin >> sname ;
    
    
    cout << "Enter the name of your city : ";
    cin >> city ;
    
    cout << "Enter the name of your state : ";
    cin >> state ;
    
    cout << "Enter your zip code : ";
    cin >> zip ;
    
    
    
    cout << "       NAME  :  " << lname << ", " << fname << endl;
    cout << "        AGE  :  " << age << endl;
    cout << "    ADDRESS  :  " << snumber <<  "   " << sname << endl;
    cout << "       CITY  :  " << city << endl;
    cout << "      STATE  :  " << state << endl;
    cout << "        ZIP  :  " << zip << endl;
    system("pause");
	return 0;
}

//i put a size for your array 20 so the it will accept a words w/ less 20 letters. and for your first name. if your first name compose of two words just like for example jhon clark your going to provide another variable for another words.

I think you want this....

#include <iostream>
#include <string>
#include<conio.h>
using namespace std;
int main () {
    char fname[100], lname[100];
    int age;
    char gender[2];
    string snumber, sname;
    string city;
    string state;
    int zip;
    
    
    cout << "Enter your first name : ";
   cin.getline( fname,100) ;
    cout << "Enter your last name : ";
    cin >> lname ;
    
    cout << "Enter your gender <M/F> : ";
    cin .getline( gender,2 );
    
    cout << "Enter your age : ";
    cin >> age ;
    
    cout << "Enter your street number : ";
    cin >> snumber ;
    cout << "Enter your street name : ";
    cin >> sname ;
    
    
    cout << "Enter the name of your city : ";
    cin >> city ;
    
    cout << "Enter the name of your state : ";
    cin >> state ;
    
    cout << "Enter your zip code : ";
    cin >> zip ;
    
    
    
    cout << "       NAME  :  " << lname << ", " << fname << endl;
    cout << "        AGE  :  " << age << endl;
    cout << "    ADDRESS  :  " << snumber <<  "   " << sname << endl;
    cout << "       CITY  :  " << city << endl;
    cout << "      STATE  :  " << state << endl;
    cout << "        ZIP  :  " << zip << endl;
    system("pause");
    getch();
	return 0;
}

I have change it at line 15 and 17.
You can do some checking options-It is not in your post .

I think you want this....

Why would you add conio.h and getch() , yet not remove system("pause"); -- both being unacceptable?
How about correcting it properly with cin.get() or cin.getline() ?

WaltP

Sorry I forgot to remove them. Actually my compiler ( Dev c++) do not support code without conio.h , getch(). I have use cin.getline() because ibthevivin want to use in his or her actual post if I am not wrong.

Actually my compiler ( Dev c++) do not support code without conio.h , getch().

Unless you typed "without" while meaning "with", that's total bullshit. If you did mean "with", it's still incorrect because Dev-C++ does support conio.h to a small extent, though a little tweaking is required.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.