18

screenshot My teacher just gave me an assignment in c++ and I am trying to get a string with scanf but it only get the last characters typed. Can anyone help me please? I am looking for the equivalent of console.readline() in c++.

edit : I must also be able to store the value through a pointer.

so the picture show the code currently runnign in the background and it should have stoped at No assurance maladie : and waited for an input but it skipped it.

getline(cin, ptrav->nam); works but it skip a line for some reason...

ESD
  • 675
  • 2
  • 12
  • 35
  • 2
    in C/C++ => [fgets](http://www.cplusplus.com/reference/clibrary/cstdio/fgets/), in C++ => [std::getline](http://www.cplusplus.com/reference/string/getline/) – rubber boots Oct 09 '12 at 18:49
  • 2
    Don't use screenshots of code, make a sample that only includes **relevant** code and describe your input into it, and the desired and actual output. Basically, do some work on isolating / diagnosing your problem before posting a question beyond "I have this and it's broken". – millimoose Oct 09 '12 at 19:29

3 Answers3

37

You are looking for std::getline(). For example:

#include <string>
std::string str;
std::getline(std::cin, str);

I've little idea what you mean when you say I must also be able to store the value through a pointer.

Update: Looking at your updated question, I can imagine what is happening. The code that reads the choice, i.e. the number 1, 2, etc. is not reading the newline. Then you call getline which consumes the newline. And then you call getline again which fetches the string.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Ya, scanf() reads words as strings I believe. fgets(..., stdin) may also work. –  Oct 09 '12 at 18:57
  • 1
    Those are C functions. That deal with C strings. C++ strings are what we want here. – David Heffernan Oct 09 '12 at 18:59
  • "string with scanf" I read as char *foo; not std::string foo; I'm guessing "through a pointer" means null terminated C style string as well. –  Oct 09 '12 at 19:03
  • @user1732694 : What does that question _mean_? – ildjarn Oct 09 '12 at 19:05
  • You need to be precise. You said you wanted a C++ equivalent of `Console.ReadLine()`. The analogue of C# string is C++ `std::string` (subject to encoding). What data type do you want to use? If you want to use C strings (null-terminated strings) then you are most likely programming in C rather than C++. – David Heffernan Oct 09 '12 at 19:06
  • what i want to do is to take a string typed by the user in a c++ console program and assing it in this variable : &ptrav->nam ptrav being a pointer to a struct and nam being a string in the struct. – ESD Oct 09 '12 at 19:10
  • 1
    OK, you can do this: `getline(cin, ptrav->nam)`. That's because the string parameter is passed by reference to `getline`. – David Heffernan Oct 09 '12 at 19:12
  • dosen't get any error but it seem to be passing over it and instantly skipping to the next line do i need to put a pause or something? – ESD Oct 09 '12 at 19:15
  • 2
    Screenshots don't allow us to run your code. Create the smallest possible program that illustrates your problem and post it. I think that would be a new question. I think the question you asked here has been answered. – David Heffernan Oct 09 '12 at 19:32
  • Looks like `getline()` doesn't accept enter until a non-space character is entered. That is *hardly* the same as `Console.ReadLine()`. – Jonathan Wood Mar 19 '17 at 20:17
  • `#include "string"` must be added or else you might get `identifier "getline" is undefined` – Junior Mayhé Jul 21 '17 at 21:09
7

According to MSDN, Console::ReadLine:

Reads the next line of characters from the standard input stream.

The C++-Variant (no pointers involved):

#include <iostream>
#include <string>

 int main()
{
 std::cout << "Enter string:" << flush;
 std::string s;
 std::getline(std::cin, s);
 std::cout << "the string was: " << s << std::endl;
}


The C-Variant (with buffers and pointers), also works in with C++ compilers but should not be used:
 #include <stdio.h>
 #define BUFLEN 256

 int main()
{
 char buffer[BUFLEN];   /* the string is stored through pointer to this buffer */
 printf("Enter string:");
 fflush(stdout);
 fgets(buffer, BUFLEN, stdin); /* buffer is sent as a pointer to fgets */
 printf( "the string was: %s", buffer);
}


According to your code example, if you have a struct patient (corrected after David hefferman's remark):
struct patient {
   std::string nam, nom, prenom, adresse;
};

Then, the following should work (added ios::ignore after additional problem has been solved by DavidHeffernan by logical thinking). Please DO NOT use scanf in your code AT ALL.

...
std::cin.ignore(256); // clear the input buffer

patient *ptrav = new patient;

std::cout << "No assurance maladie : " << std::flush;
std::getline(std::cin, ptrav->nam);
std::cout << "Nom : " << std::flush;
std::getline(std::cin, ptrav->nom);
std::cout << "Prenom : " << std::flush;
std::getline(std::cin, ptrav->prenom);
std::cout << "Adresse : " << std::flush;
std::getline(std::cin, ptrav->adresse);
...
Community
  • 1
  • 1
rubber boots
  • 14,924
  • 5
  • 33
  • 44
0

new c++ supported cin and cout keywords. You can use those

Ex