Home > Enterprise >  C - Reading a line without getline
C - Reading a line without getline

Time:05-14

I am trying to read user entered data from the stream and then store it in a custom String class.

To my best knowledge, std::getline() can route data only to std::string , that is why I need to come up with something else, as my project is not allowed to use std::string class.

My code looks like this:

String street();
std::cout << "Street: "; std::cin >> std::noskipws;
char c='\0';
while(c!='\n'){
    std::cin >> c;
    street=street c;
}std::cin >> std::skipws;
    
int bal=0;
std::cout << "Balance: "; std::cin >> bal;

CodePudding user response:

You can use the C function "getchar()" to read a single character from standard input. This link describes it: https://www.ibm.com/docs/en/i/7.3?topic=functions-getc-getchar-read-character.

Here is my code:

String street=();
std::cout << "Street: ";
char c='\0';
while(c!='\n'){
    c = getchar();
    street=street c;
}

int bal=0;
std::cout << "Balance: "; std::cin >> bal;
cout << street << endl;

I hope this will help you, and I recommend you make an independent function that will read line from standard input and whose return type will be "String". You can declare it as:

String readLine();

And I also recommend you to pay attention to that while loop because string that is obtained from that loop will have character '\n' at the end of it.

CodePudding user response:

To my best knowledge, std::getline() can route data only to std::string , that is why I need to come up with something else, as my project is not allowed to use std::string class.

Note that std::getline and std::istream::getline are two separate functions. The former will work with std::string while the latter will work with C-style strings (i.e. sequences of characters that are terminated by a null character).

Therefore, if you are not allowed to use std::string, then you can still use std::istream::getline, for example like this:

char line[200];
String street;

std::cout << "Street: ";

if ( std::cin.getline( line, sizeof line ) )
{
    //the array "line" now contains the input, and can be assigned
    //to the custom String class
    street = line;
}
else
{
    //handle the error
}

This code assumes that your custom class String has defined the copy assignment operator for C-style strings.

If it is possible that the lines will be larger than a fixed number of characters and you want to support such lines, then you could also call std::istream::getline in a loop:

char line[200];
String street;

std::cout << "Street: ";

for (;;)
{
    std::cin.getline( line, sizeof line );

    street  = line;

    if ( std::cin.bad() )
    {
        //TODO: handle error
    }

    if ( !std::cin.fail() || std::cin.eof() )
        break;

    std::cin.clear();
}

This code assumes that operator = is defined for class String.

This loop will continue forever until

  1. getline succeeds (i.e. it is able to extract (but not store) the newline character), or

  2. end-of-file is reached (eofbit is set), or

  3. an error occurs (badbit is set).

  • Related