0

i have a program with 2 cases.

  • argc=1 = interactive mode
  • argc=3 = input output file
if(argc == 1){
    bool finish= false;
    while (!finish){
        try{
            std::cout<<"Gcalc>";
            std::string line;
            std::getline(std::cin,line);
            if(line==""){
                finish= true;
                continue;
            }

and

if(argc == 3){

    std::ifstream input_file(argv[1]);
    std::vector<std::string> lines;
    std::string line;
    while (!(input_file.eof())&&std::getline(input_file, line))
    {
        lines.push_back(line);
    }
    input_file.close();
    std::ofstream out(argv[2]);
    /*if(!out.is_open()){
        throw CouldNotOpenFile();
    }*/
    std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
    std::cout.rdbuf(out.rdbuf());
    bool finish= false;

    std::cout.rdbuf(coutbuf);
    out.close();

All of the logic behind my project works fine, the only thing is that when I std::cout the string above: Gcalc it works only in the terminal but when I send it to an auto checker of my project (I'm a student) it recognizes as a fail that the words gcam that needs to be after every command doesn't show up. Any suggstions pls ?

Just to make sure - the logic works fine, the cout prints fine every single thing just not the gcalc.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Ido
  • 55
  • 7
  • why is this tagged with `python`? – lmiguelvargasf Aug 10 '20 at 16:00
  • Not sure if it's the problem, but see https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Barmar Aug 10 '20 at 16:12
  • 1
    I don't see anything that outputs `gcam` in your code. – Barmar Aug 10 '20 at 16:12
  • 1
    You can simplify `while (!(input_file.eof())&&std::getline(input_file, line))` to `while (std::getline(input_file, line))`. – molbdnilo Aug 10 '20 at 16:13
  • Is the problem just that you're not seeing the `Gcalc>` prompt in the interactive case? try flushing output. – Barmar Aug 10 '20 at 16:16
  • What's supposed to happen with `argc == 3`? It looks like you're reading the file whose name is given by `argv[1]`, not doing anything with the data you read, and then copying the file whose name is given by `argv[2]` to standard output. That doesn't seem very useful to me. – molbdnilo Aug 10 '20 at 16:28
  • Thanks for the answers guys as I expected the question was asked unclearly but I managed to work my way around it after I found a command that tells the pc to be the argv 1 and I managed to see the output I'm sending. (Couldn't do that before and the problem was as stupid as adding a space after the gcalc. I feel so dumb but it works. – Ido Aug 10 '20 at 16:34
  • And yeah I'm aware that the code is written terrible buy it's fine for now. – Ido Aug 10 '20 at 16:36
  • 1
    Not sure if someone is in the same class (because its the same assignment) but the code is very much improved in the answer to this question: [https://stackoverflow.com/questions/63341240/read-and-write-from-files-or-from-cin-and-cout#comment112004230_63341240](https://stackoverflow.com/questions/63341240/read-and-write-from-files-or-from-cin-and-cout#comment112004230_63341240) – drescherjm Aug 10 '20 at 16:37
  • @drescherjm I didn't notice your comment before I closed this. :-) Perhaps I should remove my answer since this looks like homework? – Ted Lyngmo Aug 10 '20 at 16:44
  • 1
    I would keep it at this point. It's a good answer for future readers. – drescherjm Aug 10 '20 at 16:52

0 Answers0