Strange

Hello. I have got a strange error in my programm. It's compiling well, and runs too. But when it must to perform 'cin >> where;' , it simply doesn't do that, and fast misses all cins and doesn't ask user to enter anything. Full code (don't laugh) :
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
60
61
62
63
#include <iostream>
#include <windows.h>
#include <time.h>
using namespace std;
int year;
char character;
char where;
char how;
void sleep(unsigned int mseconds)
{
        clock_t goal = mseconds + clock();
        while (goal > clock());
}
int main () {
system("color c");
cout << "Half-life 3 Chat v.1.0" <<endl;
sleep(1000);
cout << "Launching, wait..." <<endl;
sleep(3000);
cout << "Hello, and welcome to Half-life 3 chat. Here, you will have fun !" <<endl;
sleep(4000);
cout << "Let's make a little poll." <<endl;
sleep(4000);
cout << "How do you think ? In what year Half-life 3 will come out ?" <<endl;
cin >> year;
sleep(4000);
cout << "Oh my God ! Do you really think that Half-life 3 will be released in " << year << " year ?" <<endl;
sleep(4000);
cout << "Are you crazy ? I have an idea ! I will call you 'Crazy', it can be your username in this chat too ;)" <<endl;
sleep(7000);
cout << "Alright Crazy, second question. Who will be the main character of Half-life 3 ?" <<endl;
cin >> character;
sleep(2000);
cout << "Possible." <<endl;
sleep(4000);
cout << "So, Crazy, 2 questions left." <<endl;
sleep(4000);
cout << "Where the action of Half-life 3 will happen ?" <<endl;
cin >> where;
sleep(5000);
cout << "Never heard about this place, may be it came from your crazy brain ? ;)" <<endl;
sleep(4000);
cout << "Last question on today, it's about our real situation. Do you belive in a Half-life 3 'screenshots' which are appearing in the internet ?" <<endl;
cin >> how;
sleep(2000);
cout << "Alright, now i have got your characteristic." <<endl;
sleep(3000);
cout << "You're male, 17-19 years old, which plays a lot of games and wait for Half-life 3. You also like this programm ;)." <<endl;
sleep(4000);
cout << "Don't forget the next version of this programm, when it's released !" <<endl;
sleep(2000);
cout << "Titles :" <<endl;
sleep(500);
cout << "Programmer : Seer" <<endl;
sleep(500);
cout << "Idea : Seer" <<endl;
sleep(4000);
cout << "Now, you can surely close the programm" <<endl;
cout << "*** THE END ***" <<endl;
sleep(10000);
system("pause");
return 0;
}
Try to remove all Sleep's. Or try to remove each Sleep that is locatede before cin>>
1
2
3
4
5
6
7
8
9
10
11
cout << "Half-life 3 Chat v.1.0" <<endl;
sleep(1000); //Keep it
cout << "Launching, wait..." <<endl;
sleep(3000);//Keep it
cout << "Hello, and welcome to Half-life 3 chat. Here, you will have fun !" <<endl;
sleep(4000);//Keep it
cout << "Let's make a little poll." <<endl;
sleep(4000);//Remove it
cout << "How do you think ? In what year Half-life 3 will come out ?" <<endl;
cin >> year;
sleep(4000);//Keep it 

Or you can just keep your code in this condition. And try to use another compiler(Like Dev-Cpp).
I'am using DEV and your code compiles an executes just fine.
Last edited on
Just add cin.sync(); after the cin statements. The problem is that there are too many letters in a name to fit in a single character, so the other characters get left in the stream.
Last edited on
Also you can't store an entire name in one char! try using char* (character arrays as you may know them) or better yet a basic_string<char> (AKA "string"). go typedef!

Or you can just keep your code in this condition. And try to use another compiler(Like Dev-Cpp).
I'am using DEV and your code compiles an executes just fine.


NO NO NO!!! What ever you do don't use Dev-Cpp, and you XanderMax should stop it right now! Seriously though that IDE is very outdated, and the compiler it comes with is also. If you want the full argument than I believe that there is an article in the article section of this forum that has the full list of "why not to use Dev-Cpp

here: http://cplusplus.com/forum/articles/36896/
Last edited on
king214 got it.

char where;

when the user inputs a place, "Mexico" for example (why HL3 would be in Mexico, I don't know), it would assign M to "where" because it is a single character, then 'e' would be passed to "how".

A simple solution would be character arrays.

1
2
char where [];
char how [];


I forget where, but I think (not sure if I was dreaming or not, maybe I'm crazy) that I read somewhere that char *identifier; is something bad.

And seeing as all of your global variables are only being used in the main function, you really only need local variables.
cin.sync(); works ! Thank you L B, and anyway thank you all for trying to help me.
Topic archived. No new replies allowed.