c++ - my code results 0 when i attempt to enter input -


//============================================================================ // name        : lab02.cpp // author      : insert name // version     : // copyright   :  // description : hello world in c++, ansi-style //============================================================================ /*  * write program displays  * first , last name on 1 line  * address on nest line  * writes city, state, zip on next line  * writes telephone number on next line  *  * example: first last  *          123 street avenue  *          city, ca. zipcode  *          (925)555-5555  */ #include <iostream> #include <string> using namespace std;  int main() {      long double fullname;     unsigned int address;     string cityzip;     unsigned int phonenumber;      cout << "please enter full name" << endl;     cin >> fullname;     cout << fullname << endl;      cout << "enter address" << endl;     cin >> address;     cout << address << endl;      cout << "please enter city, zipzode, , state" << endl;     cin >> cityzip;     cout << cityzip << endl;      cout << "enter phone number" << endl;     cin >> phonenumber;     cout << phonenumber << endl;       return 0; } 

this console output

please enter full name first last 0 enter address 0 please enter city, zipzode, , state  enter phone number 0 

my code keeps outputting 0's , doesnt let me finish enter code prompts. can tell me doing wrong? should use getline instead of string?

long double fullname;  cout << "please enter full name" << endl; cin >> fullname; 

a long double numeric value. prompting input numeric value. can try hard enter name, here, since value long double, not going work.

the input parsing failure going put std::cin failed state, , subsequent input operations fail, resulting in ouput have observed.

in conclusion:

  1. fix type of variables. should std::strings.

  2. use std::getline() enter line of text. operator>> parses single whitespace-delimited work. intent here, every prompt, read entire line of text, possibly containing spaces. operator>> stop reading @ first space.


Comments