Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to c++ and have a question on setting a form of constant

Status
Not open for further replies.

trnrez

Programmer
Apr 3, 2003
6
US
Not sure If I am really trying to make a constant but I am wanting to set up some characters on the keyboard so that I can press them inside the program and it adds them up somewhat like a cash register and keeps a running sub-total the entire time. And I simply press a key for it to total the sub-total with the taxes. Not sure If I am presenting the question very clear but any assitance would be helpful.
 
Hello trnrez,

I see your new to Tek-Tips, welcome. Take a read through the following FAQ for general question posting tips. Particularly item #7 should be helpful, click here: faq333-2924

-pete

 
Well since my question wasn't very clear and simplified I guess that I could start with I want to make a program where I can press a letter on the keyboard and it function as a decimal integer like I could press c and it would enter the number 2.35. I hope this has cleared up portions of the confusion.
 
Still not very clear.

You're still asking a very broad question. Simplify it.


For example, part of your problem seems to involve associating keyboard characters with floating-point numbers.

If you want to map characters to floating-point numbers, you can use std::map.

Say:

Code:
std::map<char,double> m;

m[ 'c' ] = 2.35;
m[ 'D' ] = 24.68;
m[ '4' ] = 3.67;
m[ '@' ] = 4.09;

That may or may not take care of a piece of your problem. If not, ask again and say why it doesn't work. If it does, ask about another part of the problem.
 
I'm not sure if this is what you want but...

Let's say you wanted character 'a' to be $1, 'b' to be $5, 'c' to be $10, and 's' to be subtotal.

The simplest way to implement this would be to define a character variable like this:

char input;

and an integer variable like this:

int subtotal = 0;

You can then use the input in a control structure for processing like this:

cout << &quot;Enter a char: &quot;; //ask for input
cin >> input; //get a character from the user

while( input != 's' ) //while subtotal key isn't pressed
{
//process input
cout << &quot;Enter a char: &quot;;
cin >> input; //get new input
}



To process the input, you can simply anylize which key has been pressed, then make necessary adjustments to the subtotal:

//process input code
switch( input )
{
case 'a':
subtotal += 1;
break;
case 'b':
subtotal += 5;
break;
case 'c':
subtotal += 10;
break;
default:
cout << &quot;Incorrect character entered&quot; << endl;
}


Eventually, you will hit 's' and control will exit the while loop. At this point you can print out the total:

cout << &quot;Subtotal: &quot; << subtotal << &quot;.00&quot; << endl;



It sounds like you may also be looking for a way to collect keypresses without having to hit enter, am I right?


 
I would like to keep the keypresses without pressing enter.
 
This is going to sound dumb but could you structure that code a little more I am really new at this and not really understanding....Thanks for all of the help.
 
To collect keypresses without pressing enter replace
&quot;cin >> input;&quot; with &quot;input = getch();&quot;
 
A &quot;while&quot; loop performs an action while a condition is true, thus:

while( input != 's' )
{
//execute code
}

executes code while character &quot;input&quot; is not the lower case letter 's'.

A &quot;switch&quot; structure looks at a given integral value, and makes choices depending on what the choice is, thus:

switch( input )
{
//process integral value
}

looks at the character &quot;input&quot; and executes code depending on what &quot;input&quot; is equal to. For example:

case 'a':

says &quot;if input is an 'a', do the following&quot;, and

subtotal += 1;
break;

says &quot;add 1 to variable 'subtotal' (i.e. add a dollar), and don't execute any of the code that follows.

The switch structure continues to check for given inputs, and if no matching &quot;case&quot; is found, the &quot;default&quot; case is executed, i.e.

cout << &quot;Incorrect character entered&quot; << endl;

So basically, once the program begins execution, this is what the code should do:

get user input
while the input is not 's' for subtotal
see what to do based on input (switch logic)
get user input again

print the subtotal
end program
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top