Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

getline not working -c++-

+1 vote
asked Sep 29, 2020 by Ike Roland (320 points)
so i am just learning how to program and i am making this to see if i can make some program by myself so far ineeded help with 2 things one of the was answed but i need help with this one

so in function question 5 i am trying to use getline but for some reson it never worked...

*******************************************************************

#include <iostream>

#include <string>

using namespace std;

static int incorrect;

void

question_1 ()

{

  cout << "what is (5+5) + 10? :" << endl;

  double answer_1;

  cin >> answer_1;

  if (answer_1 == 1)

    {

      cout <<

"correct!!! But that was an easy one so that is what is to be expected,now onto the next question."

<< endl;

    }

  else

    {

      cout << "\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a";

      cout <<

"ha stupid human the answer was 1 that was sooooooooo obvious how did you get that wrong well you can only get 2 more wrong before you die\n";

      incorrect = 1 + incorrect;

    }

}

void

question_2 ()

{

  cout << "now,second question what is 5!\n";

  double answer_2;

  cin >> answer_2;

  if (answer_2 == 120)

    {

      cout << "that is correct thank you for not being stupid\n";

    }

  else

    {

      cout << "\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a";

      cout <<

"if you didn't know ,moron, 5! is equal to 5 factorial witch is 2x3x4x5=120 duuh\n";

      incorrect = 1 + incorrect;

    }

}

void question_3 ()

{

  cout<<"this is an easy one but i bet you will still get it wrong.what is (1,543,59,584 x 9834) - (1,543,59,584 x 9834)?\n";

  int answer_3;

  cin >> answer_3;

  if(answer_3 == 0)

  {

    cout<<"i was about to say if you were wrong that was probablly the easiest question i asked you so far but some how the stupid human got it right ,so on to the next question"<<endl<<endl;

  }else{

      cout<<"\a\a\a\a\a\a\a\a\a\a\a\a\a\a"

    cout<<"that was probablly the easiest question i asked you so far how did you get this wrong"<<endl;

    incorrect = incorrect + 1;

  }

  

}

void question_4 ()

{

    double answer_4;

    cout<<"now we are gonna do somthing  a little different,word problems"<<endl;

    cout<<"a bat and ball cost one dollar and ten cents combined. Now if the bat costs a full dollar more than the ball, what is the cost of the ball?(you dont have to write the unit)\n";

    cin>>answer_4;

    if(answer_4 == 5){

        cout<<"wow you actually got that right well your gonna get the next one wrong i will make sure of it\n";

    }else{

        cout<<"\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a"

        cout<<"this isn't even a hard word problem and somehow you still got it wrong if you want a hard one just look for the last question\n";

        incorrect = 1 + incorrect;

    }

}

void question_5 ()

{

    cout<<"this will be vary hard i bet you will get this wrong so here it goes"<<endl;

    cout<<"finish the sentence\n";

    cout<<"yesterday i read a book while the wind blew while eating an ________.\n";

    string orange;

    orange ="orange";

    string answer_5;

    getline (cin,answer_5);

    if(answer_5 == orange)

    {

        cout<<"**does not compute****error error**you must be hacking or cheating how did you get that correct well hope fully you got at least 3 wrong lets go count\n";

    }else{

        cot<<"\a\a\a\a\a\a\a\a\a\a\a\a\a\"

        cout<<"hahahhah you got this one wrong now you probably answered more than 3 incorrect let me go count\n";

        incorrect=1+incorrect;

    }

}

int main ()

{

  cout <<

    "greetings,I am T.E.S.T. witch stands for 'Termanating Every Student that cant pass this Test'"

    << endl;

  cout <<

    "you will be asked 5 questions if you get 3 wrong you will be termenated"

    << endl << endl << endl << endl << endl;

  incorrect = 0;

  question_1 ();

  question_2 ();

  question_3 ();

  question_4 ();

  question_5 ();

  //just for now to see how many are incorrect

  cout << incorrect << endl;

}

***********************************

thank you in advanced

1 Answer

0 votes
answered Sep 30, 2020 by Peter Minarik (84,720 points)
selected Sep 30, 2020 by Ike Roland
 
Best answer

When you compile your code and try to run it, you have to look at the output (bottom of the window) to see if there are any errors (or warnings).

I just tried to run (and compile) your code and I see plenty of errors. You have to fix them first.

Compilation failed due to following error(s).

main.cpp:169:14: warning: missing terminating " character
         cot<<"\a\a\a\a\a\a\a\a\a\a\a\a\a\"
              ^
main.cpp:169:14: error: missing terminating " character
         cot<<"\a\a\a\a\a\a\a\a\a\a\a\a\a\"
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function ‘void question_3()’:
main.cpp:105:5: error: expected ‘;’ before ‘cout’
     cout<<"that was probablly the easiest question i asked you so far how did you get this wrong"<<endl;
     ^~~~
main.cpp: In function ‘void question_4()’:
main.cpp:135:9: error: expected ‘;’ before ‘cout’
         cout<<"this isn't even a hard word problem and somehow you still got it wrong if you want a hard one just look for the last question\n";
         ^~~~
main.cpp: In function ‘void question_5()’:
main.cpp:169:9: error: ‘cot’ was not declared in this scope
         cot<<"\a\a\a\a\a\a\a\a\a\a\a\a\a\"
         ^~~
main.cpp:169:9: note: suggested alternative: ‘gcvt’
         cot<<"\a\a\a\a\a\a\a\a\a\a\a\a\a\"
         ^~~
         gcvt

It tells you what line has the problem and what it is.

The first problem is that you have not terminated your string: "\a\a\a\a\a\a\a\a\a\a\a\a\a\" The problem here is that you put a backslash (\) before the closing quote. This is an escape sequence, meaning do not terminate the string with the quote, but actually include the quote character in the string. And it's still waiting for the string to finish.

So just remove the backslash from before the closing quote. :)

The same line has more problems.

Every instruction has to be terminated with semicolon (;) in C and C++. So put  that at the end of the line. Similarly the declaration of orange ("string orange") should be terminated with a semicolon too. And so do question 3's and question 4's couts;

The 3rd problem with this line is the instruction cot. It is now know. You wanted to type cout, right?

As for getline, you can just google your problem. You would probably find this solution.

As described there, getline will read the enter (\n) from your last input and terminate. To fight this, call std::ws(std::cin) to skip white space characters.

ws(cin);
getline (cin,answer_5);
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...