C/C++ When Should You Use cin.getline in C++ Over cin>>?

  • Thread starter Thread starter lifesfun
  • Start date Start date
AI Thread Summary
The discussion centers around the use of `cin.getline` in C++ for reading input. It highlights that `cin >>` reads only one token at a time, making it unsuitable for inputs containing spaces, while `getline` captures an entire line. The conversation also addresses a programming task where the user needs to create a program that prompts for and stores 5 integers, 4 floats, and 3 characters in arrays. The user shares their initial code but encounters errors and seeks assistance. The community encourages sharing the code for better help and provides feedback on the structure of the program. Additionally, the user asks for guidance on writing a program to convert a string to uppercase using a character array, with suggestions to utilize ASCII values or built-in functions like `std::transform`. The discussion emphasizes understanding input methods and proper array handling in C++.
lifesfun
Messages
10
Reaction score
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
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.
 
ic.
thanks.
can anybody help me how to get this output?
output:
5 integers numbers are?
4 float numbers are?
3 characters are?
 
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?
 
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?
 
So what have you done so far with the question?
 
i was a programme but some error occur.
 
Well post your program here, and we'll try to help. You can't expect us to give you a ready made program.
 
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().
 
Back
Top