-1

I am trying to ask the user to enter the names of 3 of their friends, however, it only asks one question and writes the answer from my first one in the second and third.

#include <iostream>
using namespace std;
int main()
{

    char first_name;
    cout << "Please enter a name: ";
    cin >> first_name;
    cout << first_name << endl;

    char second_name;
    cout << "Please enter a name: ";
    cin >> second_name;
    cout << second_name << endl;

    char third_name;
    cout << "Please enter a name: ";
    cin >> third_name;
    cout << third_name << endl;

    return 0;
}
cigien
  • 57,834
  • 11
  • 73
  • 112
  • Why are you only taking a single `char` for a name? – UnholySheep May 30 '20 at 18:53
  • Did you mean to use a `char *`? 'C-Style' strings are a bit outdated in c++, you can use the `String` library instead – Jay May 30 '20 at 18:59
  • [Why using namespace std is considered bad practise](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) could be an interesting read aswell if you are getting into c++. – darclander May 30 '20 at 19:01

2 Answers2

0

You should probably be using string in your code to take the input of names. In names you are probably passing more than one character. The first one is read by first_name and any further character will be read by the following character, specifically cin>>second_name and cin>>third_name would read the 2nd and 3rd character of your input.

char a;
char b;
cin>>a;            //will only read one character and stop
cin>>b;            //will read the second character of the input...
                   //be that after pressing enter(a Enter b) or continuous input (ab)
cout<<a<<" "<<b;   //will output 1st and 2nd character only

This will happen even if you don't press the Enter key explicitly and this is why your program uses the answer of first question(which is probably more than 1 character since it is a name) in your code as the answer to 2nd and 3rd questions as well.

So for your purpose, you are better of using string to take input from the users.

Hope this clears your doubt !

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
0

You tried to hold a lot of chars (one word) in one char who can hold only one char.

#include <iostream>
#include <string> // We need a string, container to hold a chars. Something like array of chars but have a few difference.

using namespace std; // You should avoid using this but in that short code this doesn't matter

int main()
{
    // You don't need separate container for this code
    // Then we create one container to holds all of inputs
    string input; 

    cout << "Please enter a name: ";
    cin >> input; // Put input from user in our input(string)
    cout << input << endl; // Print input

    // Next code is the same as above
    cout << "Please enter a name: ";
    cin >> input;
    cout << input << endl;

    cout << "Please enter a name: ";
    cin >> input;
    cout << input << endl;

    return 0;
}

I special avoided a few elements like using function because this must be simple as possible.