Problem with comparing

Hey everyone!

For learning purposes, im trying to imitate Dharma's computer terminal from "lost" in c++

heres 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
#include <cstdlib>
#include <iostream>
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <unistd.h>
using namespace std;

int main(int argc, char *argv[])
{
system("@echo off");
system("color 2");
cout<<">:";
char numbers[15];
cin>> numbers;
cout<<"\n";
if(numbers != "4 8 15 16 23 42")
{
           do
           {
                         cout<< "system failure\n";
                         Sleep(1000);
                         
    
            }
             while(numbers != "4 8 15 16 23 42");
    
}    
  
}



The problem is that whatever i tipe i go to system failure, even if i type 4 8 15 16 23 42"...

What is the problem then?

thanks in advance.
1. operator >> uses whitespaces as separator characters, so if you input "4 8 15 16 23 42", numbers will only contain "4". You wanna use std::getline();
http://www.cplusplus.com/reference/string/getline/
2. You are comparing char pointers, not strings :
either use strcmp(), or use std::string s, they have overloaded operator==.
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
http://www.cplusplus.com/reference/string/string/
3. Why are you including all these unnecessary headers? Only use which you need.

EDIT: (100th comment woo!:D)
Last edited on
1. Could you please rewrite my source becouse i want to know what it would look like.?
2. Im including all these unnecesarry headers becouse i just started and those headers use the functions i use verry often.
I reconstructed your code, because you had flaws in your logic too, this is how I would have done it :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[]) {

    string numbers; //string instead of char[]
    while(true) {
        cout << ">:";

        getline(cin, numbers);//reads input until the next line (basically until an enter is typed)

        if ( numbers == "4 8 15 16 23 42" ) { //now we can compare them with == as 'numbers' is a string
            break;
        }
        cout << "system failure\n";
    }

}

Edit: R0mai beat me to it.

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
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

int main()

{
	string numbers;

system ("color 2");

cout << " >: ";

	getline (cin, numbers);

cout << "\n";

if (numbers == "4_8_15_16_23_42")

	{

	cout << " Avoided ";
	}

else

	{

	cout << " WRONG. \n\n";

	}

system ("PAUSE");

return 0;

}
Last edited on
Thanks guys for your help ! i feel so stupid now :D i mean i have no logic at all lolz

Heres my finished script:

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
#include <cstdlib>
#include <iostream>
#include <string>
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <unistd.h>

using namespace std;

int main(int argc, char *argv[]) {
system("color 2");
int i = 1;
while(i == 1){
    string numbers; //string instead of char[]
    while(true) {
        cout << ">:";

        getline(cin, numbers);//reads input until the next line (basically until an enter is typed)
Beep(2750,100);
        if ( numbers == "4 8 15 16 23 42" ) { //now we can compare them with == as 'numbers' is a string
            break;
            
        }
        
        
               int failurecountdown = 55;
               
                   do
                   {
                   Beep(270,500);
                       cout << "| system failure  ";
                       Sleep(500);
                       failurecountdown--;
                    }
                    while(failurecountdown >= 1);
             system("cls");       
             do { 
                 Beep(1000,200);
                 Beep(2000,200);      
                 cout<< "|Use emergency over-ride key";
                 Sleep(500);}
                 
             while(true);    
    }

}
}
Last edited on
Topic archived. No new replies allowed.