1

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.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Marky2019
  • 47
  • 6
  • Can you include a sample input and output? – Michael Apr 20 '20 at 16:13
  • w3schools is fine to get a taste of a language, but you should transition to a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) as soon as possible. Some of the suggested code snippets you find on sites like w3school, geeksforgeeks, etc don't follow best practices. – JohnFilleau Apr 20 '20 at 16:19
  • `cin >>` is whitespace based. This means input like `John Smith` will be treated as two inputs. Since second input expects `int`, `cin` state will be set to fail and no further inputs will be taken until `cin.clear()` is called. If you want full lines, use [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) (but [beware of mixing getline and cin inputs](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction)) – Yksisarvinen Apr 20 '20 at 16:20
  • Once you get this working how you want, consider bringing it over to [codereview.se] for peer review on ways to improve your code. Again, make sure it's working before you post it over there. – JohnFilleau Apr 20 '20 at 16:20

1 Answers1

1

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?

anastaciu
  • 23,467
  • 7
  • 28
  • 53