Cin gets ignored?

So my cin gets ignored in the void "CReturn()" I don't know to solve this one... Please can someone explain that to me ? Thank u :)





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
64
65
66
67
68
69
70
71
72
73
74
75
76
void choice(){

	char *choice;
	choice = new char [1];
	

	cout <<"So... What do you want to know? " << endl;
				cin >> *choice;


if(*choice == 'time'){*choice == 't';}
		

				switch (*choice){
																
				case 't' : 	time();
																
				}

				CReturn();

		
}
void time(){

	time_t rawtime; //creates and object of the built in time function
	struct tm * timeinfo; //no idea what this do

	time ( &rawtime ); //gets the time from the computer
	timeinfo = localtime ( &rawtime ); //store that time here

	//it displays current date and time except time is frozen and not real time
	cout << "Current local time and date is : "<<asctime (timeinfo)<< endl; 
	Sleep(4000);
	return;

}
void Return()
{return choice();}
void CReturn(){

		char *c;
	c = new char [0];


	cout <<"Anything else ?(y/n): ";
		cin >> *c;

				switch (*c){

				case 'y':	system("CLS");
							cout << "Redirecting" << endl;
							Sleep(1000);
							system("CLS");
							cout << "Redirecting." << endl;
							Sleep(1000);
							system("CLS");
							cout << "Redirecting.." << endl;
							Sleep(1000);
							system("CLS");
							cout << "Redirecting..." << endl;
							Sleep(3000);
							system("CLS");
							return chocie();
							

				case 'n':	cout << "Have a good Day!" << endl;
							Sleep(3000);
							system("CLS");
							cout << "NPcreations" << endl;
							Sleep(1000);
							exit(0);		


}
}
Last edited on
closed account (zb0S216C)
Please format your code with code-tags (see the "<>" button to the right when editing your post).

First and foremost, you're abusing dynamic memory allocation. You only need to dynamically allocate an object if its size is large. Allocating built-in types, such as "int", "char", and "float", is unnecessary and should be placed on the stack.

Devilnp123 wrote:
c = new char [0];

Never do this. Any references to this memory will cause a segmentation-fault because you haven't allocated any memory. Instead, use "std::string" to handle any strings you have. Also, make sure you free any memory you allocate before allocating more.

As for the skipping input, what are you giving as input?

Wazzak
Last edited on
Nothing... It just skips the cin line, so I can't give an input.
And then it goes further and the program closes because nothing is given.
The "*c" needs to be cleared from input everytime it goes back to the beginning. Else it has the info and goes in a loop.
closed account (zb0S216C)
Devilnp123 wrote:
if(*choice == 'time')

First of all, you're comparing only the first character of "choice" and nothing else. Second, 'time' has an identity crisis; it doesn't know if it's a string or a character constant. In C++, there are two ways to represent characters: a string and character constant. A character constant is a single character which is enclosed in single quotes. A string is an array of characters with a null-character at the end and are enclosed in double-quotes.

Please, "std::string" is around for a reason.

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

int main( )
{
    std::string Input_;
    std::cin >> Input_;
  
    if(Input_ == "Time") // "Time" is a string; 'Time' is not.
        // ...
}

See here for more: http://www.cplusplus.com/reference/string/string/

Now back to the original code. When "time" is entered as input, "time" is broken down into individual characters. When you extract data from the input stream and attempt to store the data in a "char", only one character from the input stream is extract because "char" can only hold one character at a time. After an extraction, "ime" will still remain in the input stream. Any subsequent extraction operations with "choice" will use those remaining characters as their input. Here's an example:

This is the current input stream's state: Input Stream: T, I, M, E, New-Line.

1
2
3
4
5
int main( )
{
    char Input_(0);
    std::cin >> Input_;
}

'T' will be removed from the input stream and placed in "Input_". This leaves the input stream in this state: Input Stream: I, M, E, New-Line. Each extraction operation with "Input_" will extract one character from the input stream until there's not data left. If the input stream already contains data before a user enters more data, the data that's already inside the stream will be used automatically as input.

See here: http://www.cplusplus.com/doc/tutorial/basic_io/

Wazzak
Last edited on
Thanks alot man. I'm finally over that one. Up to the next mistake
Topic archived. No new replies allowed.