Home > database >  One line user input assignment in C
One line user input assignment in C

Time:11-16

I am learning C and was wondering if there is an equivalent to assigning a user input to a variable in one line, like you can do in C# for example:

string foo = Console.ReadLine();

I was hoping that one of these would work, but they don't.

const string foo = cin >> foo;

cin >> const string foo;

Ideally the variable should be a constant, but that's not necessarily a requirement.

Are there ways of one lining it in C or will I just have to learn to live with this?

double foo = 0;
cin >> foo;

CodePudding user response:

As apple apple says you can write your own functions

double console_read_double()
{
    double x = 0.0;
    cin >> x;
    return x;
}

int main()
{
    double y = console_read_double();
    double z = console_read_double();
    ...
}

However one advantage of the C way is that you can chain calls to input functions

int main()
{
    double y, z;
    cin >> y >> z;
    ...
}

You can't do that with a function call that returns the value read.

Another advantage is that you can test the success or failure of the operation with a single line of code

int main()
{
    double y, z;
    if (cin >> y >> z)
    {
        // success
    }
    else
    {
        // handle error
    }
}

CodePudding user response:

Sorry but there is no way in C to do this in one line. Unless you do this when you are using an if statement:

if(double b = 0; std::cin >> b)
{
  // Code
}

otherwise you will have to live with:

double b = 0;
std::cin >> b;

Or you can just make a function like this:

auto init_and_input()
{
  double b = 0;
  std::cin >> b;
  return b;
}

and then:

int main()
{
  auto x = init_and_input();
}

So your answer is not really if you include the function.

CodePudding user response:

You can introduce your own named function:

std::string getInput()
{
    std::string result;
    std::cin >> result;
    // ...
    return result;
}

And then you can use it (with type deduction included):

auto input = getInput(); // input is a string

This may look like a lot more movement than needed but due to some compiler magic (like RVO) we'll probably have the same approach in the result as in your C# oneliner.

Also note, there are times when we can substitute an operator with a named function call alike Console.ReadLine();, but not in the case of the operator>> in the std::cin.

CodePudding user response:

In addition to what has been stated, and in the ultra specific scenario where you need to get only one character, standard library provide the function get() that reads and return one character in a stream. This allows you to initialize your variable with the read value, therefore you could also define it as a const.

#include <iostream>

int main()
{
   const char some_char = std::cin.get();
   // Do whatever with some_char
   return 0;
}

Note that even if it is theoretically valid C , this doesn't looks like any kind of good practice, and it might be preferable in almost every case to just stick to another, most common, way to read your stream.

  • Related