Exploring cin.getline in C++: Usage & Benefits

  • C/C++
  • Thread starter lifesfun
  • Start date
In summary, you can use the >> operator to read one word at a time, and use the getline function to read input lines that may contain spaces.
  • #1
lifesfun
10
0
hello,
i wish to ask about the c++:
cin.getline
when we should use this and why we should use this?
why we can't direct use cin>>?
 
Technology news on Phys.org
  • #2
The >> operator only reads one token at a time so it will read one word (or number or other element for which it is defined) and return. The getline function reads a complete line. So use >> to read single words, use getline to read input lines that may contain spaces.
 
  • #3
ic.
thanks.
can anybody help me how to get this output?
output:
5 integers numbers are?
4 float numbers are?
3 characters are?
 
  • #4
You need to give us more information about what you are trying to do here. Are there specific integers, float numbers, and characters that you want to print where you have the question marks? Or do you simply want to "cout <<" those three lines with question marks at the end? Or are you trying to prompt the user of your program to enter something?
 
  • #5
the question:
write prgramme that will ask user to enter 5 integer numbers, 4 float numbers and 3 characters. then store them in arrays.
output:
5 integers numbers are?
4 float numbers are?
3 characters are?
 
  • #6
So what have you done so far with the question?
 
  • #7
i was a programme but some error occur.
 
  • #8
Well post your program here, and we'll try to help. You can't expect us to give you a ready made program.
 
  • #9
haha...
i get it already!
thanks for everybody.

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

{
int a[5], i;
float b[4];
char c[3];

cout<<"please enter a integer"<<endl;

for (i=0; i<8; i++){
cin>>a;
}

cout<<"\n 5 integer are: ";
for (i=0; i<5; i++){
cout<<a<<" ";
}

cout<<endl;

cout<<"please enter a float"<<endl;

for (i=0; i<4; i++){
cin>>b;
}

cout<<"\n 4 float are: ";
for (i=0; i<4; i++){
cout<<b<<" ";
}

cout<<endl;

cout<<"please enter a character"<<endl;

for (i=0; i<3; i++){
cin>>c;
}

cout<<"\n 3 integer are: ";
for (i=0; i<3; i++){
cout<<c<<" ";
}

cout<<endl;

return 0;
}
 
  • #10
now i don't know how to answer this question:
write a programme that prompt the user to enter a string and output the string in uppercase letters.(use a character array to store the string)
can anybody help me?
 
  • #11
Although there are built in functions to do this in ctype.h, I think you should try using the ASCII equivalent values of each letter of the string, so as to change it to corresponding uppercase value. Do you know how to do this?
 
  • #12
In C++ with std::string and that.
Code:
std::transform(str.begin(), str.end(), str.begin(), std::toupper);

Similar with C character strings, except you'd have to manually loop through each character yourself and call toupper().
 

1. What is cin.getline in C++?

cin.getline is a function in the C++ programming language that allows the user to input a string or a line of characters from the standard input stream.

2. How is cin.getline different from cin?

While cin only reads a single word or character from the input stream, cin.getline allows the user to input a string or a line of characters, including spaces, until a delimiter is reached.

3. What is the syntax for using cin.getline in C++?

The syntax for using cin.getline is: cin.getline(string_name, size, delimiter);

4. What are the benefits of using cin.getline in C++?

Cin.getline allows the user to input a line of characters without having to worry about the input exceeding the size of the string. It also allows for more flexibility in user input, as it can handle spaces and special characters.

5. Can cin.getline be used multiple times in a single program?

Yes, cin.getline can be used multiple times in a single program. However, it is important to clear the input buffer before using it again to avoid any unexpected behavior.

Similar threads

  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
Replies
10
Views
951
Back
Top