error pinpoint problem

Regardless of what i input, even if its the correct answer. i don't know why it goes directly to incorrect

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <conio.h>
#include <cmath>
using namespace std;
main()
{
      
      
      string ans2,ans3,ans4,ans5;
      int score=0; 




           
{                      
  //1                             
                            
system("cls");
                                                      cout<<"1.This is the Modern Age.";
                                                      Sleep(3000);
system("cls");
                                                      cout<<"What is your answer?\nAnswer:";
                                                      cin>>ans2;
                                                      if (( ans2 == "this is the modern age." )||( ans2 == "THIS IS THE MODERN AGE."))
                                                                              {
                                                                                  
                                                                                  cout<<"YOU ARE CORRECT!\n\n\n\n\nCurrent Score:";
                                                                                  score = score+20; 
                                                                                  Sleep(2000);
                                                                                  
                                                                                 
                                                                              }
                      
                                                     
                                                     else 
                                                                               {
system("cls");                                                                  
                                                                                score= 0; 
                                                                                cout<<"YOU ARE INCORRECT!\nSubtracting 15 Points from your Score\n\n\n\n\nCurrent Score:["<<score<<"].";
                                                                                
                                                                                Sleep(3000);
                                                                                }
                                                                                
                                              
                                                                              
                                                     
                        
                                  
}



system("PAUSE>>NULL");
return 0;     
}              
Line 26: cin>>ans2;
It will only read first word. Use getline()
Try replacing line 26 with this: getline(cin, ans2);

Reference wrote:
Notice that the istream extraction operations use whitespaces as separators; Therefore, this operation will only extract what can be considered a word from the stream. To extract entire lines of text, see the string overload of global function getline.


http://www.cplusplus.com/reference/string/string/operator%3E%3E/

Edit:
The ninja strikes again -_-
Last edited on


//SELECTION AREA FOR DIFFICULTY


{
menu:
system("cls");
cout<<"\n\n\n\n\n PLEASE SELECT YOUR DIFFICULTY\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~[EASY/HARD]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nSelection:";
cin>>select;

if ((select == "easy")||(select == "EASY"))
{
system("cls");
cout<<"YOU'VE SELECTED [EASY] MODE. GOODLUCK!";
Sleep(400);
goto Easy1;
}
else if (select == "hard")
{
system("cls");
cout<<"YOU'VE SELECTED [HARD] MODE. BEST OF LUCK TO YOU!";
Sleep(400);
goto Hard1;

}
else
system("cls");
cout<<"Invalid Input";
Sleep(400);
return 0;


}

//this is where the hard questions should be


{




{
//1
Hard1:
system("cls");
cout<<"1.This is the Modern Age.";
Sleep(3000);
system("cls");
cout<<"What is your answer?\nAnswer:";
getline(cin, ans2);

{
if ( ans2 == "This is the modern age.")
{
system("cls"); score = +20;
cout<<"YOU ARE CORRECT!\n\n\n\n\nCurrent Score:["<<score<<"].";
Sleep(2000);
goto Hard2;
}


else
{
system("cls"); score= 0;
cout<<"YOU ARE INCORRECT!\nSubtracting 15 Points from your Score\n\n\n\n\nCurrent Score:["<<score<<"";

Sleep(3000);


}
}


}


}




{
//2
Hard2:
system("cls");
cout<<"1.The measure of intelligence is the ability to change.";
Sleep(3000);
system("cls");
cout<<"What is your answer?\nAnswer:";
getline(cin, ans3);

if ( ans3 == "The measure of intelligence is the ability to change.")
{
system("cls"); score = +20;
cout<<"YOU ARE CORRECT!\n\n\n\n\nCurrent Score:["<<score<<"].";
Sleep(2000);
goto Hard3;
}


else
{
system("cls"); score = 0;
cout<<"YOU ARE INCORRECT!\nSubtracting 2 Points from your Score\n\n\n\n\nCurrent Score:["<<score<<"].";
Sleep(3000);


}


}



{
//3
Hard3:
system("cls");
cout<<"1. Quotation is a serviceable substitute for wit.\n\n\n\n\nCurrent Score:["<<score<<"].";
Sleep(3000);
system("cls");
cout<<"What is your answer?\nAnswer:";
cin>>ans4;

if ( ans4 == "Quotation is a serviceable substitute for wit.")
{
system("cls"); score = +20;
cout<<"YOU ARE CORRECT!\n\n\n\n\nCurrent Score:["<<score<<"].";
Sleep(2000);
goto Hard1;
}


else
{
system("cls"); score = -15;
cout<<"YOU ARE INCORRECT!\nSubtracting 2 Points from your Score\n\n\n\n\nCurrent Score:["<<score<<"].";
Sleep(3000);


}


}




my problem now is that it won't allow me to answer anything. it skips directly to incorrect
Ah, good old "mix stream extraction operator and getline and break everything" error.
You see, when you write something and console and press enter, >> operator will read up untill endline symbor and stop before that, leaving it in input buffer.
Then when you invoke getline, it will encounter it immideatly and think that line has ended, so it will actually read empty string.

Use
1
2
3
#include <limits>
//...
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
before getline.
Last edited on
dev c++ won't allow me to use that std statement :\

EDIT: Hold on, got it to work.
Last edited on
you either have an outdated compiler or didn't included limits at the beginning of your file.
try cin.ignore(100, '\n');
dev c++ (which is what my school is forcing me to use) is out dated. :| but then i got it to work. thanks MiiNiPaa!
Topic archived. No new replies allowed.