cin not working even with proper header files

Pages: 12
Guys .. I've got a prob here..

cin isn't working.. see code below..

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

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<dos.h>

  void ins()
{
clrscr();
int d=1,e=2,f;
cout<<"Insert Tray                            Single - "<<d;
cout<<"\nLoad Drum                              Double - "<<e;
cin>>f;
if(f == d)
cout<<"Single";
else if(f == e)
cout<<"Double";
else
cout<<"Invalid Entry";
getch();
}


void main()
{
ins();
}



Well ... The program isn't taking input from me.. The screen appears that

Insert Tray Single - 1
Load Drum Double - 2Invalid Entry

The program shows invalid entry without taking input from me...Help pls
//Using Turbo C++
Last edited on
To read from cin you should use >>
@Peter87

Yet not working
Are you sure this is the code? If you are using cin before calling ins() the problem could be there.
Yes this is the code ..Fully sure..
Well .. Lemme show full 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<dos.h>

void wlcscrn()
{
clrscr();
cout<<"                      Universal Lab           ";
cout<<"\n    ULF VER : 01/01                   15/09/2015";
delay(3000);
}

void login()
{
clrscr();
char a, b, c;
cout<<"Username - ";
cin>>a;
cout<<"Password - ";
b = getch();
while(b != 13){
cout<<'*';
b = getch();
}
clrscr();
cout<<"Login Successful";
delay(2000);
}

void ins()
{
clrscr();
int d=1,e=2,f;
cout<<"Insert Tray                            Single - "<<d;
cout<<"\nLoad Drum                              Double - "<<e;
cin>>f;
if(f == d)
cout<<"Single";
else if(f == e)
cout<<"Double";
else
cout<<"Invalid Entry";
getch();
}

void main()
{
wlcscrn();
login();
ins();
}
Last edited on
Using Turbo C++


Maybe try a compiler that isn't 25 years old?
I can't run it because I don't have Turbo C++ but one thing I spotted is that you only allow the username to be one character (a is a char). If you input more than one character the rest of them will be read as the password.
You're mixing stream input and stdio input in function login(). This causes unexpected behavior. To see what's going on I changed ins:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void ins()
{
    clrscr();
    int d=1,e=2,f = -1;
    cout<<"Insert Tray                            Single - "<<d;
    cout<<"\nLoad Drum                              Double - "<<e << '\n';

    string str;
    getline(cin, str);
    cout << "cin has \"" << str << "\"\n";
    if (!(cin>>f)) {
	cout << "Can't read number\n";
    } else if (f == d)
	cout<<"Single";
    else if(f == e)
	cout<<"Double";
    else
	cout<<"Invalid Entry " << f << '\n';;
    getch();
}

When I enter UserName Dave and password hello, it responds with
cin has "ave"
Looking at line 19, you input only a single character for the username instead of a string. So the rest of the string is in the input buffer and that's what you get when trying to read the username.

Well .. guys .. @Disch - Turbo C++ must be 25 years old but the college uses the software and hence i'm bind to use it .. else i'd had obviously used codeblocks..

@Peter87 and @dhayden ... I have no problem whatever happens in the login function... I just had prob in the ins function in which the cin isn't reading... Thats why i don't usually post my whole code in here.. Okay .. If anybody knows what should I do Please Reply

@dhayden I don't know why .. I've Put the header file as #include<string.h> still when i type string in the code .. it says thats invalid... and when i put #include<string> then one more error rises saying that no header file like that..

Even need help in that situation... Sorry if I annoyed someone... I'm just a beginner , newbie, and maybe noob.. Please reply..
closed account (48T7M4Gy)
How old are the PC's?
Are your names and passwords a single character? Entering more that a single character for these variables will cause problems because you defined the variables as single characters. If you enter multiple characters for these variables you'll probably have problems later in your program because you'll probably have characters left in the input buffer when you leave the login function. By the way why does the login function always succeed no matter what you try to enter for the password.

Since you're using such an outdated compiler you probably shouldn't try to use a C++ string since that compiler's version of the string class is quite different than the standard C++ string class. You'll be better off using C-strings instead.

Where is your
using namespace std;

?
MannedTooth wrote:
Where is your using namespace std; ?

Turbo C++ doesn't have namespaces.
Oh, my mistake. :P
I have no problem whatever happens in the login function

Just because it appears to work doesn't mean it is problem free. At line 19 you do this:
cin>>a;
Which reads a single character, leaving whatever else you typed for the username in the input buffer. So when you do cin>>f in inp(), it fails because you're asking it to interpret the 2nd-nth characters of the username as a number.
Ahh guys... I said i don't care whatever the login function does.. I just have problem in ins fuction ... Thats where the cin is not reading.. I don't mean to be rude but you guys are stuck at login function yet and i need solution to ins function... Login function doesn't matter to me at all and that is just for a output .. Means just for show.. Showpiece some like that... Please reply for ins function...
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<string>

void ins()
{
    int d=1, e=2, f;
    std::cout << "Insert Tray                            Single - " << d;
    std::cout << "\nLoad Drum                              Double - " << e;
    std::cin >> f;

    if(f == d)
        std::cout<<"Single";
    else if(f == e)
            std::cout<<"Double";
        else
            std::cout<<"Invalid Entry";
}

int main()
{
    ins();
    
    return 0;
}
i don't care whatever the login function does
Even if it doesn't work?
I just have problem in ins fuction
No, you have a symptom in the ins function. You have a problem in the login function.

If your problem is in the ins() function, then why is it that when I fix login(), your ins() function works properly?

I have already explained the problem. I've explained why the symptom shows up in ins(). Your refusal to take the help really baffles me.

Often the symptom of a programming bug doesn't show up until later on in the code. Here's a trivial example:
1
2
3
4
5
6
7
8
9
10
#include<iostream>
int add(int a, int b)
{
    return a - b;		// bug is here
}

void main()
{
    std::cout << add(3,1) << '\n'; // bug shows up here.
}

Ok sorry .. So you mean i need to correct the login function too .. Oh hell .. Coding's getting tougher and tougher ... Ok what should i do for the login function? Please reply @dhayden

// I Apologize for any rude behaviour by me
Last edited on
closed account (48T7M4Gy)
Get your code to run as I have shown and then have a look at the login later. You seem to have a problem related to using crappy Turbo C or whatever it is. You can do real C++ online for free.

Press the gear wheel in the right hand top corner of the code boxes here.
Last edited on
Pages: 12