Question about char comparations

Pages: 12
Dont give up so easily! :)

Post what you got now. It isnt so hard, and a good way to learn.
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
#include<conio.h>
#include<iostream.h>
#include<string.h>
using namespace std;

string P3;

int main(void)
{
   int P1,P2;
   float Z,T=0;
   cout<<"Cuestionario de 3 preguntas v1.0\nPor Pablo Cassinerio\n\n";
   cout<<"Pregunta 1:\n\n1-Si\n2-No\n\n2+2=4?";
   cin>>P1;
   if(P1==1) {
	     cout<<"Correcto!\n\n";
	     T=T+1;
	     }
   else cout<<"Incorrecto\n\n";
   cout<<"Pregunta 2:\n\n1- 1942\n2- 1516\n3- 1492\n\nEn que a¤o fue descubierta America? ";
   cin>>P2;
   if(P2==3) {
	     cout<<"Correcto!\n\n";
	     T=T+1;
	     }
   else cout<<"Incorrecto\n\n";
   cout<<"Cual es la capital de Argentina? "; 
   getline(cin,P3);                                           
   if(P3=="buenos aires") {
	     cout<<"Correcto!\n\n";                     
	     T=T+1;                                              
	     }
   else cout<<"Incorrecto\n\n";
   Z=T*10/3;
   cout<<"Ha obtenido un puntaje de "<<Z;
   getche();
}


For some reason, when it gets to the third question, it only pops incorrect, without giving me the chance to write the answer, I think it's something about the getline
It doesnt work because your using several techniques (cin & getline). That causes problems with the memory or someting. I dont know how this works excactly either.

I have replaced the two inputs for 1 and 2 with a function i already got from someting else. It works great now:
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
#include<conio.h>
#include<iostream>
#include<string>

using namespace std;

string P3;

int getint(int min,int max)
{
  int input;
  char temp[100];
  while (input<min||input>max||(input==0 && temp[0]!='0'))
  {
  std::cin.getline(temp, 100);
  input=strtol(temp,0,10);
  if (input<min||input>max||(input==0 && temp[0]!='0'))
  std::cout<<"Not an option!\n";
  }
}

int main(void)
{
   int P1,P2;
   float Z,T=0;
   cout<<"Cuestionario de 3 preguntas v1.0\nPor Pablo Cassinerio\n\n";
   cout<<"Pregunta 1:\n\n1-Si\n2-No\n\n2+2=4?";
   P1=getint(1,2);
   if(P1==1) {
	     cout<<"Correcto!\n\n";
	     T=T+1;
	     }
   else cout<<"Incorrecto\n\n";
   cout<<"Pregunta 2:\n\n1- 1942\n2- 1516\n3- 1492\n\nEn que a¤o fue descubierta America? ";
   P2=getint(1,3);
   if(P2==3) {
	     cout<<"Correcto!\n\n";
	     T=T+1;
	     }
   else cout<<"Incorrecto\n\n";
   cout<<"Cual es la capital de Argentina? "; 
   getline(cin,P3);                                           
   if(P3=="buenos aires") {
	     cout<<"Correcto!\n\n";                     
	     T=T+1;                                              
	     }
   else cout<<"Incorrecto\n\n";
   Z=T*10/3;
   cout<<"Ha obtenido un puntaje de "<<Z;
   getch();
}
great!! another perfecty well working program, got 17 already :D

however, I like to fully understand what my programs are made of, can u explain to me how u did the getint? I don't fully get it hehehe
Good question. Always try to understand everyting you use :)

I wrote it for a simple game. The problem with cin is that when you use it to store input directly into a integer variable, the program will crash when the user inputs a character. Thats wy in 'real' programs you'll never see cin>>integer.

Solution? Store the input in a variable of type char[], and then store that value into a variable of type string.
As you may know, a character has an integer value of type 0, so if the user would type a character, the inputvalue would become 0. Thats wy i used input==0 && temp[0]!=0 in the condition for the while loop: in this case the loop should go on asking the user for input.
I guess you understand the purpose of max and min.

The reason i used this function in this program is because getint() also uses getline(). Using different things often causes problems, especially when its about interaction. So when i saw your code i rememberd my old getint() function :P

I didnt come up with that function all by myself, aldo i have changed some things. I got it orginially from this site: http://www.augustcouncil.com/~tgibson/tutorial/iotips.html. Really usefull stuff, i recommend you to read it.
Last edited on
I see, thanks! I really don't like keeping a C++ program unless i fully understand it and can modify it successfully :D
1
2
int i;
cin >> i;


User enters "12<CR>" (without quotes, and <CR> meaning ENTER key).

cin reads the characters 1 and 2 and stops at <CR> since it doesn't match an integer.

Next you do
1
2
string str;
getline( cin, str );


getline reads up to but excluding the next newline (ie, <CR>). But the first character in the input stream is the <CR> the user typed on previous question, so it returns immediately with an empty string.

You need to flush the input stream between questions.

 
cin.flush();

When i use cin.flush() it wount compile, it doesnt recognize flush as a memberfunction from std::cin. He does recognize the memberfunction std::cout.flush.
Are you confused with cin.clear() or am i doing someting wrong?
Yeah, it was off the top of my head. cin.clear() or cin.ignore( ... ) is right.
Topic archived. No new replies allowed.
Pages: 12