1

I have to prompt the user to enter their name and their height using two separate inputs, one for feet and one for inches. Then I have to display what they entered. Ex: Name, you are x feet y inches tall. The program I wrote runs but it's not working as intended. After I write an input for the first question which is the name, the program skips the other questions and ends the program.

#include <iostream>
#include <string>

using namespace std;

int main ()
{
    int name;
    int f;
    int i; 
    // Start: Enter Name
    cout << "What is your name?" << endl;
    cin >> name; 
    // Enter height 
    cout << "How many feet tall are you tall?" << endl;
    cin >> f;
    // Enter inches
    cout << "How many inches tall are you after feet?" << endl;
    cin >> i;
    // End: All info entered
    cout << name << " you are " << (f) << " feet " << (i) << " inches tall." << endl;

    system("pause");

    return 0;
}
8
  • 2
    Remember that with cin >> name; you can only enter the first name. If you enter a space the part after will go to the next cin Commented Feb 4, 2020 at 2:16
  • You also don't close out your program. Missing the brace for int main at the end. Commented Feb 4, 2020 at 2:17
  • Related: https://stackoverflow.com/questions/39881604/how-to-read-names-with-space Commented Feb 4, 2020 at 2:18
  • 7
    Are you sure you don't want std::string name; as the name variable? Commented Feb 4, 2020 at 2:19
  • 1
    If you number everyone and keep the population under (typically) 2.2 billion you could have a go of using int Commented Feb 4, 2020 at 2:21

1 Answer 1

4

First and most importantly, you should change the type of name from int to string. Your current program tries to read your input to name as a number and not text.

Now, if your program is still not working as intended, make sure when you are typing in input for name there is no white-space (spaces, tabs, etc.) in between the name you are trying to input. For example, the input of John would be fine, but John Smith would not. This is because the >> operator when used with cin values all white-space the same. This means that pressing the space bar is the same as pressing the enter key to your program. If you are trying to input a full name, look into using getline to read input up until the enter key is pressed.

Getline Example:

cout << "What is your name?";
getline (std::cin,name);

Summary: Change int name; to string name;. If you need to read multiple words for the name use getline.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.