What am I Not Understanding About Getline+Strings

What am I not understanding about getline+strings?

The reason it's appearing to skip the first iteration is because when you do

cin >> size1;

You enter a number and hit the Enter key. cin reads the integer and leaves the newline character unread on the buffer, so that when you call getline, it's as if you immediately hit the enter key, and getline reads nothing (because it stops before reading the newline character), discards the newline, and puts the empty string in quest1[0]. And that's why the rest of the getlines work "correctly".

Add cin.ignore('\n') above your loop to get rid of the lingering '\n', and that should make it work, barring other errors in your code.

And don't forget to change x = x++ to just x++ to avoid UB.

I am having trouble with getline() function in C++

The problem with getline() is that you are trying to assign a string stream to an int variable houseNum, hence the error.

no matching function for call to 'getline(std::istream&, int&)'

Also, the line of code:

std::cin >> firstName;"\n";

Should give you:

warning: statement has no effect [-Wunused-value]

For the "\n"

Enable compiler warnings, they save you a lot of time.

getline() does not work if used after some inputs

Characters are extracted until either (n - 1) characters have been
extracted or the delimiting character is found (which is delimiter if this
parameter is specified, or '\n' otherwise). The extraction also stops
if the end of the file is reached in the input sequence or if an error
occurs during the input operation.

When cin.getline() reads from the input, there is a newline character left in the input stream, so it doesn't read your c-string. Use cin.ignore() before calling getline().

cout<<"Journal Entry:\t";
cin.ignore();
cin.getline(journal,23);

How do I use getline to output a string n times using getline?

When you enter a number to assign a value to n, do you then enter the string after a line break? If so, str is assigned a newline character instead of the corresponding string.

#include <iostream>
#include <string>
using namespace std;

int main()
{
int n;
cin >> n;
string str;
// getline(cin, str);//Filter newline character
// getline(cin, str);
getline(cin.ignore(1,'\n'), str);
for (int i = 0; i < n; i++) {
cout << str <<endl;
}
return 0;
}

cin.Getline returns nothing

you are first reading string to std::string with cin >> string; and then read again something from cin with cin.getline(string, STREAMSIZE);
it is not necessary, read it once and return:

string getString(char string[]){
cout << "Please enter a string to process ";
cin >> string;
cout << "String in getString before process: " << string << "\n";
// process this, do whatever you describe as processing it
cout << "String after processing: " << string << "\n"; // string is printed
return string;
}

otherwise, if you want to use getline, do:

  std::string name;

std::cout << "Please, enter your full name: ";
std::getline (std::cin,name); // or std::getline(std::cin,string, 'r'); to read
//only to delimiter character 'r'
std::cout << "Hello, " << name << "!\n";

so thing to remember is use getline OR cin, not both simultaneously unless there is really some special reason

C++ - Reading a line without getline

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 and break loop, for example by
//throwing an exception
}

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 Topics



Leave a reply



Submit