1

I am currently trying to learn C++ and I'm working on my first project. It is supposed to ask questions and get the user to provide the input for the answer then write the input to a file with some formatting. however, I keep getting an error with the description line input, it takes the first word in a sentence and nothing else. I've tried several things to get it to work and all of them throw a similar error or don't allow me to give any input...

cin >> description; allows me to give input of 1 word.

std::getline and getline dont allow me to give any input.

I just seem to get these errors:

Error (active)  E0304   no instance of overloaded function "std::basic_istream<_Elem, _Traits>::getline [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list

or this error:

std::basic_istream<char,std::char_traits<char>>::getline': non-standard syntax; use '&' to create a pointer to member

My current code looks like this:

int main()
{
    ofstream myfile;
    myfile.open("Output.txt");
    if (myfile.is_open())
    {
        cout << "Enter kit name\n";
        cin >> kit;
        myfile << "    \"" << kit << "\":{\n";
        cout << "Default Amount?\n";
        cin >> defaultamount;
        myfile << "      \"DefaultAmount\":" << defaultamount << ",\n";
        cout << "Price\n";
        cin >> price;
        myfile << "      \"Price\":" << price << ",\n";
        cout << "Enter a Description for the kit\n";
        cin >> description;
        myfile << "      \"Description\":\"" << description << "\",\n";
        myfile.close();
        cout << "output.txt has been updated with your results\n";
        system("pause");
    }
    else
        cout << "Unable to create or update text file";
    return 0;
}
  • This is what `std::getline` is for. If you're having a compilation error when you try to use it, that's what you need to show in your question. Nobody will be able to tell you what's wrong with the way you use it if you don't even give an example of how you're trying to use it. – Sam Varshavchik Mar 09 '20 at 01:31
  • @SamVarshavchik I think he is looking for a function that reads in a list of words - not a string containing multiple words. The error message says he is passing a parameter named "list" and I don't know what the type is but is isn't a string and the name of the variable implies it is a vector or list or struct or something. Struct seems likely since the questions imply different types for each input. – Jerry Jeremiah Mar 09 '20 at 01:36
  • Are you wanting to read a string of multiple words and then split it up into individual elements or are you trying to read all those different elements into one structure? It's easy to read a string containing all the elements and then split it up later. – Jerry Jeremiah Mar 09 '20 at 01:38
  • im trying to get it so that you can give a brief description for the "kit" but its only reading the first word of the description. – Jonathan Wilson Mar 09 '20 at 01:56
  • @SamVarshavchik `std::getline(std::cin, description);` puts me into an infinite text capture problem where pressing return doesn't end the input capture and update the text file. – Jonathan Wilson Mar 09 '20 at 02:10
  • Sorry to hear that. Unfortunately, nobody will be able to help you with a program with a program that's not even shown. – Sam Varshavchik Mar 09 '20 at 02:12
  • @SamVarshavchik I pasted the whole program other than the obvious string declarations which I used in the code, I'm not quite sure what more I can show you... Either way, I found a solution. had to use `std::cin.ignore(26, '\n');` – Jonathan Wilson Mar 09 '20 at 02:22
  • @JonathanWilson Don't use 26 - hardcoded magic numbers are bad news - what happens when someone types in a 27 character description? Use `cin.ignore(numeric_limits::max(), '\n')` https://stackoverflow.com/questions/25020129/cin-ignorenumeric-limitsstreamsizemax-n – Jerry Jeremiah Mar 09 '20 at 02:43
  • @JonathanWilson What Sam meant was you were asking for help on how to use getline (you said "std::getline(std::cin, description); puts me into an infinite text capture problem") and yet the code you posted doesn't even try to use getline so noone can tell you why it causes that problem. – Jerry Jeremiah Mar 09 '20 at 02:49
  • @JonathanWilson Maybe you could post an answer so that future people visiting the page can understand what your solution was and how it fixed your problem? – Jerry Jeremiah Mar 09 '20 at 02:52

1 Answers1

3

Assuming that you need to initialize variables and include some headers, you should include something like:

#include<fstream>
using std::ofstream;
#include<iostream>
using std::cout;
using std::cin;
#include<string>
using std::string;

string kit;
int defaultamount;
float price;
string description;

if description will have several words, you must change:

cin >> description;

for:

cin.ignore();
getline(cin,description);
4lrdyD
  • 433
  • 2
  • 10