user input

prompt does not wait for user input

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 3534 Views - Last Post: 04 November 2010 - 04:58 PM Rate Topic: -----

#1 lobabyg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-October 10

user input

Posted 03 November 2010 - 05:55 PM

Hi,

I just have a quick question. Why would the user prompt not wait for the input? I am using visual studio 2008. It is a quick program as I am learning. I wondered if there were preferences set so that it automatically comes up and says "hit any key to continue". The problem is that the message is before the prompt and so hit a key and you are out of the program. Any thoughts on the subject would be appreciated.

thank you,

lobabyg

Is This A Good Question/Topic? 0
  • +

Replies To: user input

#2 SarumanTheWhite  Icon User is offline

  • D.I.C Regular

Reputation: 72
  • View blog
  • Posts: 352
  • Joined: 04-November 08

Re: user input

Posted 03 November 2010 - 05:58 PM

This is for the window vanishing too quickly, right? Calling

cin.getline();



Should do the trick.

This post has been edited by SarumanTheWhite: 03 November 2010 - 05:59 PM

Was This Post Helpful? 0
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: user input

Posted 03 November 2010 - 06:21 PM

Holding the execution window open
http://www.dreaminco...on-window-open/
Was This Post Helpful? 0
  • +
  • -

#4 lobabyg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-October 10

Re: user input

Posted 03 November 2010 - 07:02 PM

View PostSarumanTheWhite, on 03 November 2010 - 04:58 PM, said:

This is for the window vanishing too quickly, right? Calling





cin.getline();



Should do the trick.


Hi,

Thank you for the quick reply. However, it is not for the window vanishing too quickly but rather for "Hit any key to continue" getting in front of the user prompt. The program prompts for the user to input a number there is no way to put in the input. The message pops up before the cursor. So if the user hits any key then out of the program they go--with no input. Is this set automatically somehow?

Thanks

lobabyg
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: user input

Posted 03 November 2010 - 07:16 PM

Are you sure you don't get any input? Have you, for example, tried printing out the input right after asking for it? A newline (hit the enter key) is an example of input by the way. If a newline is sitting in the stream, you may be picking up that newline.
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: user input

Posted 03 November 2010 - 08:38 PM

The most likely cause of what you seem to be describing is that your code is bad.

Please post the code that has this behaviour between code tags, like this :code:
Was This Post Helpful? 0
  • +
  • -

#7 lobabyg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-October 10

Re: user input

Posted 03 November 2010 - 10:26 PM

View Postjanotte, on 03 November 2010 - 07:38 PM, said:

The most likely cause of what you seem to be describing is that your code is bad.

Please post the code that has this behaviour between code tags, like this :code:


Hi,

I'm sorry but I was not exactly sure what you meant by between the tags. On yours or what? Just remember, I do not know anything or very little about coding.

/cout << "\n\nEnter a number = "; /


I want to say that every other part of the code is fine. This only happens on this line. Before that I have user input for about three or four lines and it works perfect. I like to figure things out on my own but since I am using the same code for the other inputs and they work I just could not understand what it could be. Oh, and thank you for your patience and time.

lobabyg
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: user input

Posted 04 November 2010 - 12:43 AM

View Postlobabyg, on 04 November 2010 - 02:26 PM, said:

I'm sorry but I was not exactly sure what you meant by between the tags. On yours or what? Just remember, I do not know anything or very little about coding.

This is not coding.
This is just how to show us your code.
Look at this :code:
Do you see the first tag? The text in the first square brackets? Type that including the square brackets.
Then copy and paste your code
Then type the closing tag The text in the second square brackets? Type that including the square brackets.
Click the "Preview Post" button.
Does your code show up in the preview nicely laid out with line numbers running down the left hand side?
If it does you have got it right. Click the "Add Reply" button.
If not, keep trying until you get it right.

View Postlobabyg, on 04 November 2010 - 02:26 PM, said:

I want to say that every other part of the code is fine.

And I want to say that, as much as I would like to believe you on that point, the evidence is against you.
Your program is not behaving as you would like it to. There is a 99.99% probability is that the reason for that misbehaviour is in your code.
Unless you show us all your code so we can look at the problem in context and give you suggestions as to how you might fix this problem we aren't able to help you further.
The decision is yours.
Was This Post Helpful? 0
  • +
  • -

#9 lobabyg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-October 10

Re: user input

Posted 04 November 2010 - 07:10 AM


#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

                                                                                                                 //Global Declarations of Variables
double iovertime_hours=0, iovertime_pay=0, iovertime_extra;
int ihours, iwage ;
string cname ;

int main()
{
	                                                                                                        //Enter Employee Information
	cout << "\n\nEnter the employee name = ";
	cin >> cname ;
	cout << "Enter the hours worked  = ";
	cin >> ihours;
	cout << "Enter his or her hourly wage = ";
	cin >> iwage; 

	                                                                                                        // Determine if hours are greater than 40 
	if (ihours > 40) 
	{
		                                                                                               //Do Calculations
		iovertime_hours=ihours-40;
		iovertime_extra=iovertime_hours*iwage+iovertime_hours*iwage*.5;
        iovertime_pay = iovertime_extra*iovertime_hours>40;


		                                                                                               // Display Employee Details
		cout << "\n\n";
		cout << "Employee Name ............. = " << cname << endl ;
		cout << "Base Pay .................. = " << iwage*40 << endl; 
		cout << "Hours in Overtime ......... = " << iovertime_hours << endl ;
		cout << "Overtime Pay Amout......... = " << iovertime_extra << endl ;
		cout << "Total Pay ................. = " << iovertime_extra+(40*iwage) << endl;
	}    
	else                                                                                                   // Else hours are less than 40 hours
	{
		cout << "\n\n";
		cout << "Employee Name ............. = " << cname << endl ;
		cout << "Base Pay .................. = " << iwage*40 << endl ;
		cout << "Hours in Overtime ......... = " << iovertime_hours << endl ;
		cout << "Overtime Pay Amout......... = " << iovertime_extra << endl ;
		cout << "Total Pay ................. = " << iovertime_extra+(40*iwage) << endl;
	                                                                                                       // End of the primary if statement 
                                                                                                                //End of Int Main
	}
                   
 int cnum;                                                                                                      //Part 2 enter a set of numbers; use a while loop   
cnum = int ("");                                                                                              //use one variable to count the number of inputs
{                                                                                                     //Use the switch statement to count inputs under 100 
cout << "\n\nEnter a number = "; 
cin >> cnum ;



I did not start the while statement because I could not get the user input correct. Like I said everything works fine it just won't let me put in the input for the "Enter a number"


Thank you again for your time,

lobabyg
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: user input

Posted 04 November 2010 - 07:31 AM

The code you posted omits two closing braces at the end. I assume you accidentally snipped them off (please don't do this in the future). If I compile and run your code, I can enter a number just fine...
Was This Post Helpful? 0
  • +
  • -

#11 lobabyg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-October 10

Re: user input

Posted 04 November 2010 - 07:56 AM

Hi,

That is my point. It will not let me put a number in user input. Is there a setting on visual studio 2008 that makes the "press any key to continue" come up? That is what I am having a problem with. The code runs great until the "enter a number" and then it says"Press any key to continue." If you press a key then out of the program you go. No number gets entered.

Thank you,

lobabyg
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: user input

Posted 04 November 2010 - 08:23 AM

I am not able to reproduce the problem with your code. And I compiled with the VC compiler, which is what you are using.

I mean, if you take your code, dump into a new project, and then compile and run that code do you still get the same problem?
Was This Post Helpful? 0
  • +
  • -

#13 lobabyg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-October 10

Re: user input

Posted 04 November 2010 - 09:25 AM

Hi,

Thanks again for trying to help me. I did as you suggested but it still happens. I wonder if I am setting up the projects incorrectly or something? But then why would it be ok up until that point?
I also wonder why I get that "Press any key to continue" and you do not.

Thank you,

lobabyg
Was This Post Helpful? 0
  • +
  • -

#14 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: user input

Posted 04 November 2010 - 09:41 AM

What are you typing for input? You have to answer three questions (employee name, hours worked, wage). Copy paste the exact input you give for each...

EDIT: Also, right before you ask for a number, please copy paste this code:

if(!cin) std::cout << "Failbits set on cin.";



If the request for input is being skipped over, then it means the stream had a failbit set. Failbits get set because of previously failed I/O operations. That's why I asked for your previous input. I want to ensure that you aren't typing in something that would cause failure.

EDIT 2: In particular, your wage is an integral value. If you type in a number like 8.50 for the wage, which is not an integer, you will have a problem...

This post has been edited by Oler1s: 04 November 2010 - 09:44 AM

Was This Post Helpful? 0
  • +
  • -

#15 lobabyg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 13-October 10

Re: user input

Posted 04 November 2010 - 09:50 AM

View PostOler1s, on 04 November 2010 - 08:41 AM, said:

What are you typing for input? You have to answer three questions (employee name, hours worked, wage). Copy paste the exact input you give for each...

EDIT: Also, right before you ask for a number, please copy paste this code:

if(!cin) std::cout << "Failbits set on cin.";



If the request for input is being skipped over, then it means the stream had a failbit set. Failbits get set because of previously failed I/O operations. That's why I asked for your previous input. I want to ensure that you aren't typing in something that would cause failure.

EDIT 2: In particular, your wage is an integral value. If you type in a number like 8.50 for the wage, which is not an integer, you will have a problem...

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2