Question about char comparations

Pages: 12
Hi again, now I bring a question about characters. I'm working on a test with C++, but I cant find the way to successfully compare the P3 with RP3 characters, is there something wrong with the code?
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
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main(void)
{
   int P1,P2;
   char P3[13]={'b','u','e','n','o','s',' ','a','i','r','e','s','/0'},
   RP3[13]={'b','u','e','n','o','s',' ','a','i','r','e','s','/0'};
   float Z,T=0;
   printf("Cuestionario de 3 preguntas v1.0\nPor Pablo Cassinerio\n\n");
   printf("Pregunta 1:\n\n1-Si\n2-No\n\n2+2=4?");
   scanf("%i",&P1);
   if(P1==1) {
	     printf("Correcto!\n\n");
	     T=T++;
	     }
   else printf("Incorrecto\n\n");
   printf("Pregunta 2:\n\n1- 1942\n2- 1516\n3- 1492\n\nEn que a¤o fue descubierta America? ");
   scanf("%i",&P2);
   if(P2==3) {
	     printf("Correcto!\n\n");
	     T=T++;
	     }
   else printf("Incorrecto\n\n");
   printf("Cual es la capital de Argentina? ");
   cin>>P3[0,1,2,3,4,5,6,7,8,9,10,11,12];
   if(P3[0,1,2,3,4,5,6,7,8,9,10,11,12]==RP3[0,1,2,3,4,5,6,7,8,9,10,11,12]) {
	     printf("Correcto!\n\n");
	     T=T++;
	     }
   else printf("Incorrecto\n\n");
   Z=T*10/3;
   printf("Ha obtenido un puntaje de %.2f",Z);
   getche();
}


Still no cin and cout? ;)

I dont believe you can compare char[]'s that way, aldo i'm not sure. Use string instead would solve the problem, or you could use a function with a loop. Maybe there is an easier solution.

You can compare char[]'s that way
Last edited on
O, you have used cin. Good :), aldo it can be confusing to use several techniques for the same thing.

However, you have made a little mistake: it should be cin<<... instead of cin>>...
if i write cin<< instead of cin>> i get an "Illegal structure operation" :D
I'll try redoing all the program with cin and cout only, I'll post it in a while
this is how it looks 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
#include<conio.h>
#include<iostream.h>
void main(void)
{
   int P1,P2;
   char P3[13]={'b','u','e','n','o','s',' ','a','i','r','e','s','/0'},
   RP3[13]={'b','u','e','n','o','s',' ','a','i','r','e','s','/0'};
   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++;
	     }
   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++;
	     }
   else cout<<"Incorrecto\n\n";
   cout<<"Cual es la capital de Argentina? ";
   cin>>P3[0,1,2,3,4,5,6,7,8,9,10,11,12];
   if(P3[0,1,2,3,4,5,6,7,8,9,10,11,12]==RP3[0,1,2,3,4,5,6,7,8,9,10,11,12]) {
	     cout<<"Correcto!\n\n";
	     T=T++;
	     }
   else cout<<"Incorrecto\n\n";
   Z=T*10/3;
   cout<<"Ha obtenido un puntaje de "<<Z;
   getche();
}


However, i have the same error, and i'm kind of n00b :P, dunno how to use strings yet

The problem is not with comparing, but with the input:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
    char input[10];
    cin>>input[0,1,2,3,4];
    cout<<input;
    cout<<endl<<endl;
    system("pause");
    return 0;
}


I got to go, i will look at this later
I would use strings.

A string is nothig but a sequence of charachers. You can perform serveral operations with them (memberfunctions) etc., but you dont have to know that for now.
To use strings you have to include the standart lib <string>. You use double quotions (") and between those you can put as many chars as you want (well, everyting has his limits). Eg:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

using namespace std;

string sometext="This is some text.";

int main()
{
cout<<sometext;
return 0;
}

If you want to know more, check the reference part of this site.

For inputting a string that may exist out of several words (a string whit spaces in it) you need to use getline(cin,variablename) instead of cin>>. See http://www.cplusplus.com/reference/string/getline.html.

Hope this helps. If you still cant figure it out, please ask :)

Btw, i'm sure you can do this using char[], but if you want to learn c++ you will have to learn how to use strings.
Last edited on
knowledge is power :D i will learn strings now

I will let u know how it worked out
It's strange, it doesn't work

first of all, i need to declare the libraries as <iostream.h> and <string.h>, and i get 3 errors:

declaration syntax error in line 4
declaration syntax error in line 6
undefined symbol 'sometext'
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream.h>
#include <string.h>

using namespace std;

string sometext="This is some text.";

int main()
{
cout<<sometext;
return 0;
}



for some reason, i guess my compiler doesn't recognize the strings, is it possible?

I use the Turbo C++ btw
Last edited on
Turbo C++ is really old IIRC, you should probably upgrade to something like VC++ or the Bloodshed group
I see, u think that the problem is my compiler?
Yes it could be...especially since it doesn't understand what "using namespace std;" is supposed to do.
OK, i downloaded the dev-C++, i will check how that one works
seems that was it, the dev-C++ does recognize "using namespace std"

with every discovery comes new problems :P

when I write something with the spacebar, it only reads the first word, for example, if i write "hello world", it only recognizes "hello", is there a way to avoid this or will i have to write always with underscores?
Ah, thats something with cin...if you want to get the entire line, including spaces, use getline(cin, string_to_put_it_in);
great, that works great, just one more detail and my program will be ready :P

now it's getting the answer, but the comparation isn't working:
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
#include<conio.h>
#include<iostream.h>
#include<string.h>
using namespace std;

string P3
void 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++;
	     }
   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++;
	     }
   else cout<<"Incorrecto\n\n";
   cout<<"Cual es la capital de Argentina? "; 
   getline(cin,P3);                                           
   if(P3='buenos aires') {                          //the error is around here, I guess
	     cout<<"Correcto!\n\n";                     
	     T=T++;                                              
	     }
   else cout<<"Incorrecto\n\n";
   Z=T*10/3;
   cout<<"Ha obtenido un puntaje de "<<Z;
   getche();
}


I ask for P3, and if P3 is "buenos aires", it should be correct, but it's not!!, is there something wrong with the code or is it something else?
For string comparisons use " ", not ' '.
i replaced the ' ' for " ", but it's still the same
I'm using dev-cpp to, and i cant use type void for main and have to include <iostream> instead of <iostream.h> (same for string).

Besides that, it should be P3=="buenos aires" (2x=). The strange thing is that when you use 1x=, it should always return true, so it should be (always) correct, instead of the other way around.

The whole code acts strange when i run it, it doesnt ask for an answer by the third question but tells me its incorrecto immediately
well, I guess I will have to do make it a multiple choice too
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
#include<conio.h>
#include<iostream.h>
#include<string.h>
using namespace std;

int main(void)
{
   int P1,P2,P3;
   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<<"Pregunta 3:\n\n1- La Plata\n2- Tigre\n3- Buenos Aires\n\nCual es la capital de Argentina? "; 
   cin>>P3;                                           
   if(P3==3) {
	     cout<<"Correcto!\n\n";                     
	     T=T+1;                                              
	     }
   else cout<<"Incorrecto\n\n";
   Z=T*10/3;
   cout<<"Ha obtenido un puntaje de "<<Z;
   getche();
}


Pages: 12