473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with getline

1 New Member
I'm trying to get an array of names stored into a data structure, but I keep getting this odd error of C2661

(error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
)

I can't figure out how to fix this error, any help would be appreciated.(code included for reference)
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct StudentRecords
  5. {
  6.    char name;
  7. };
  8.  
  9. void main()
  10. {
  11. StudentRecords student[5];
  12.    for (int s = 0; s < 2; s++)
  13.    {
  14.    cout << "Enter student name ----> ";
  15.    cin.getline(student[s].name );
  16.    cout << endl;
  17. }
Apr 9 '08 #1
5 2389
sicarie
4,677 Recognized Expert Moderator Specialist
So you are using C++, why are you using getline()?

Have you attempted to read the name in with an intermediary value and then assign it afterwards to the struct value?
Apr 9 '08 #2
Ganon11
3,652 Recognized Expert Specialist
A single char cannot hold a name like "Joe", it can only hold a single character like 'J'. So you don't want your struct to be a plain char - at the very least, make it a char* or char[20] or something.

But, as sicarie mentioned, you are using C++, so why not use std::string? Then you can use getline like so:

Expand|Select|Wrap|Line Numbers
  1. string myName;
  2. getline(cin, myName);
Apr 9 '08 #3
sicarie
4,677 Recognized Expert Moderator Specialist
A single char cannot hold a name like "Joe", it can only hold a single character like 'J'. So you don't want your struct to be a plain char - at the very least, make it a char* or char[20] or something.

But, as sicarie mentioned, you are using C++, so why not use std::string? Then you can use getline like so:

Expand|Select|Wrap|Line Numbers
  1. string myName;
  2. getline(cin, myName);
Dang, I looked right over that one.

Thanks, Ganon
Apr 9 '08 #4
jrhitokiri
35 New Member
just watch how you use getline 'cuz it will also include your return keypress.
Apr 11 '08 #5
Ganon11
3,652 Recognized Expert Specialist
Erm...no, it doesn't. The string returned with getline does not include the newline character '\n' - in fact, according to http://www.cplusplus.com/reference/string/getline.html, "If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it."

cin >> operations will leave the newline character in the stream, but still skips it when getting actual input into a variable.
Apr 11 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
1887
by: Shane | last post by:
Hi, Thanks in advance for the help. I have been to many websites and tried several solutions to my problem, but have fixed part of it. It's time to come humbly to the newsgroups for help :-) ...
18
8222
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
9
1740
by: Jordan Tiona | last post by:
I can't get this code to work right. It seems to be skipping some of the cin functions. Can someone help me with this? ClassTrack.cpp: #include <iostream> #include "ClassTrack.h" using...
4
4829
by: wqyuwss | last post by:
Hi, We have several core dumps in our product. These core dump can be reproduced in the same place. That is system function call std::basic_istream<char,std::char_traits<char>>::getline. The...
5
1800
by: ComicCaper | last post by:
Hi all, I use a quiz program that accepts a text file for questions and answers in this format: Question Answer1 <----is the correct answer. Quiz randomizes answers. Answer2 Answer3...
18
3096
by: vermarajeev | last post by:
Hello everybody, This is my second query in this post. Firstly thankx to Banfa, for helping me solve my first query. Here is the code which I have written. #include<iostream>...
31
3102
by: Martin Jørgensen | last post by:
Hi, I've had a introductory C++ course in the spring and haven't programmed in C++ for a couple of months now (but I have been programmed in C since january). So I decided to do my conversion...
1
1343
by: cecil258 | last post by:
Hey everyone... I'm in a beginning C++ class right now and below is the code I've written for my current assignment... What I need to do is put all of the input code (basically the huge chunk of...
4
1550
by: T.Crane | last post by:
Hi, I'm new to C++, and I want to open a file, go through it line by line and pull out specific things on each line. I've written a test bit of code to try to do this. For some reason what I'm...
6
2847
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.