skip to Main Content

I have been struggling with a certain error that doesn’t make sense to me. Whenever I try to compile this program, it tells me that I’m missing a semicolon when I am not.

It seems the error is linked to a specific block of code, that being the if statement that checks stock. Since I know c++ can be platform specific, I’m running debian 9 and the atom ide if that’s any help.

Here is the specifc error:

error: expected primary-expression before ‘,’ token

getline(string,line);//gets string`

and the code:

#include <iostream>
#include <fstream>


using namespace std;

int main ()
{
    cout << "store stocking system: n"; // yadda yadda yadda UX
    cout << "commands: n";
    cout << "  help: shows available commandsn  check stock: checks store stockn  enter stock: enter new stock itemsn";
    cout << "  exit: terminates the programn  clean house: deletes all stockn";
    home: // main loop in program
    string output;
    output = ">> ";
    cout << output;


    string stock;
    string item; // this whole block just defines things and gets input
    int itemNumber;
    string userInput;
    getline(cin,userInput);

    if (userInput == "exit")
    {
      return 0;
    }

    if (userInput == "enter stock")
    { // enters new stock
      cout << "enter itemn>> "; //item name
      cin >> item;
      cout << "enter amountn>> "; //amount of item
      cin >> itemNumber;
      ofstream myfile; //makes file
      myfile.open("stock.txt"); //opens myfile
      myfile << "n" << item << "," << itemNumber << "n"; //writes to file
      myfile.close();// closes file
      cout << "done";
      goto home; //finishes and goes to main loop
    }

    if (userInput == "check stock") // where the problem is
    {
      string line;
      ifstream file("stock.txt");//checks fo file
      file.open("stock.txt");//opens file
      getline(string,line);//gets string
      file.close();//closes it
      cout << line << "n";
      goto home;
    }

    if (userInput == ""){
      goto home;
    }

    else
    {
      cout << "33[1;31mplease use a proper command:33[0mn";
      goto home;

    }
    return 0;
}    

3

Answers


  1. Are you missing this by any chance?

    #include <string>
    
    Login or Signup to reply.
  2. I believe it simply needs to be getline(file,line) rather than getline(string,line) and then you should be sorted.

    Login or Signup to reply.
  3. string is recognized as a type name, of type std::string which you generously exposed by line using namespace std;. This particular error message is caused by fact that string isn’t an expression which can be evaluated . In context of your code it should be

    getline(file,line);
    

    PS. Standard would say that you have to include <string> header to use component std::string. Your code compiles thanks to an implementation-defined feature, was imported with <iostream> in this version of header.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search