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!

cin problems (newbie)

Status
Not open for further replies.

trose178

Programmer
Nov 26, 2002
26
US
Hey guys im having a problem with my cin commands. I have a program that I wrote to tell me the taxes out of my paystub from my lame grocery job. But i have about 5-6 cin commands and it keeps skipping the rest of them after the first one. Here is my code:
// paystub_tax.cpp - Calculates percent of taxes taken out of
// Acme paystubs.
#include <iostream>
#include <iomanip>
using namespace std;

int WeekWage;
int OASDI;
int Medicare;
int Federal;
int State;
int UI;

int main()
{
cout << &quot;This program was created to help all Acme workers calculate the percentage of taxes taken out of their Acme Pay Stubs.&quot;
<< endl << endl
<< &quot;Enter your Week's Wages: &quot;;
cin >> WeekWage;
cout << endl << endl
<< &quot;Next enter your OASDI tax amount from your Acme Pay Stub&quot;;
cin >> OASDI;
cout << endl << endl
<< &quot;Third enter your Medicare tax amount from your Acme Pay Stub&quot;;
cin >> Medicare;
cout << endl << endl
<< &quot;Fourth enter your Federal tax amount from your Acme Pay Stub&quot;;
cin >> Federal;
cout << endl << endl
<< &quot;Fifth enter your State tax amount from your Acme Pay Stuf&quot;;
cin >> State;
cout << endl << endl
<< &quot;Sixth enter your UI/HC/WD NJ tax amount from your Acme Pay Stub&quot;;
cin >> UI;
cout << endl << endl
<< &quot;Acme Pay Stub Tax Percentages&quot;
<< endl
<< setw(6) << &quot;OASDI&quot; << setw(6) << ( OASDI / WeekWage )
<< setw(6) << &quot;Medicare&quot; << setw(6) << ( Medicare / WeekWage )
<< setw(6) << &quot;Federal&quot; << setw(6) << ( Federal / WeekWage )
<< setw(6) << &quot;State&quot; << setw(6) << ( State / WeekWage )
<< setw(6) << &quot;UI/HC/WD NJ&quot; << setw(6) << ( UI / WeekWage );
return 0;
}
 
Your problem is not in the cin, but rather the last cout statement. You cannot do computations while doing a cout. I rewrote the last part of the code and it works.


int OASDI_avg = OASDI / WeekWage;
int medicare_avg = Medicare / WeekWage;
int fed_avg = Federal / WeekWage;
int state_avg = State / WeekWage;
int UI_avg = UI / WeekWage;

cout << endl << endl
<< &quot;Acme Pay Stub Tax Percentages&quot;
<< endl
<< setw(6) << &quot;OASDI&quot; << setw(6) << OASDI_avg
<< setw(6) << &quot;Medicare&quot; << setw(6) <<medicare_avg
<< setw(6) << &quot;Federal&quot; << setw(6) << fed_avg
<< setw(6) << &quot;State&quot; << setw(6) << state_avg
<< setw(6) << &quot;UI/HC/WD NJ&quot; << setw(6) << UI_avg;


hope this helps.
 
The previous reply is incorrect; your cout statement is fine.

I suspect your problem is that you have the variables declared as int instead of float or double. The program will work fine if you enter numbers like 5, 8, or 35. As soon as you try something like 5.7 or 200.00, it reads the first integer, sees the decimal point, recognizes that it's not part of an integer, and stops reading. When you try to read the next integer, it sees the decimal point and discovers it can't convert it to an integer, so cin fails, and every read from it results in an empty operation, so it &quot;skips&quot; them all.
 
Ok thank you chipperMDW you were right that was a big help. Thanks for the help guys I'll hopefully be helping other people like me when i get a little better hehe
 
oops i was wrong heh. I misunderstood the question then made an assumption on cout. Sorry bout that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top