Marky2019
Marky2019

Reputation: 47

Program to take in user input & display it Problems

I am trying to get this program to take in three values from the users at different lengths each so like W3Schools.com says to do I put the variablename.length(); in the code to get the whole line entered by the User but it only works for one of them.

I have three of them if not more why does it only work so many times is there something I'm missing in the #include files or something else. The code is pretty simple I thought I tried to go over it and make it as detailed as possible. Here is the code:

// CreateWriteDisplay.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <cstring>
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
    void OpeningMessage() {
           cout << "Enter Your First Name and Last name:";
           cout << "Enter Your Age: ";
           cout << "Enter Your Ocupation:";
           cout << " This is yout Ocupation:";
}

int main()
{
OpeningMessage(), "\n";
    int AccountAge;
    string FullName;
    string Ocupation;
    cin >> AccountAge;
    cin >> FullName;
    cin >> Ocupation;
    cout << FullName << FullName.length() << "\n";
    cout << AccountAge << "\n";
    cout << Ocupation << Ocupation.length();
    return 0;

Also if anyone knows of anymore Tutorials like the W3Schools website I could really use the practice if you can't tell I am a beginner with C++ Programming. I did really good one the test on the first tutorial. I really enjoy this Language its reminds me a little of JavaScript but so much more powerful. So any help would be greatly appreciated. Thank You in advance.

Upvotes: 1

Views: 426

Answers (1)

anastaciu
anastaciu

Reputation: 23802

I'm guessing your problem is that

cin >> FullName;

will stop reading at the first blank space, so if you input first and last name the first name will be read and the last name will remain in the buffer. It will then be read by the next cin

cin >> Ocupation;

You can solve this either by separating first and last name in two separate variables or using std::getline.

As you are beggining may it's best to use the first solution and later revisit getline. I suggest:

#include <iostream>

using std::cout;     //using namespace std is not a good practice **
using std::cin;      //it's best to use std:: scope or using only what you need
using std::string;   //not the whole namespace, C++17 allows for comma
using std::endl;     //separated usings

int main()
{
    int AccountAge;
    string LastName;
    string FirstName;
    string Ocupation;

    cout << "Enter Your Age: ";
    cin >> AccountAge;

    cout << "Enter Your First Name and Last name: ";
    cin >> FirstName >> LastName;

    cout << "Enter Your Ocupation: ";
    cin >> Ocupation;

    cout << "Full Name: " << FirstName << " " << LastName << ", Length: " << 
    FirstName.length() + LastName.length() << endl;

    cout << "Age: "<< AccountAge << endl;
    cout << "Occupation: " << Ocupation << ", Length: " << Ocupation.length();
    return 0;
}

** Why is "using namespace std;" considered bad practice?

Upvotes: 1

Related Questions